Skip to content

AWS CodeCommit source - avoid "for" loops #159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 26 additions & 48 deletions pkg/adapter/awscodecommitsource/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ func (a *adapter) Start(stopCh <-chan struct{}) error {

for _, pr := range pullRequests {
resetBackoff = true
err = a.sendPREvent(pr)
err = a.sendEvent(pr)
if err != nil {
a.logger.Errorw("Failed to send PR event", "error", err)
return resetBackoff, nil
Expand Down Expand Up @@ -201,7 +201,7 @@ func (a *adapter) processCommits() error {

lastCommit = *commitOutput.Commit.CommitId

err = a.sendPushEvent(commitOutput.Commit)
err = a.sendEvent(commitOutput.Commit)
if err != nil {
return fmt.Errorf("failed to send push event: %w", err)
}
Expand All @@ -216,67 +216,45 @@ func (a *adapter) preparePullRequests() ([]*codecommit.PullRequest, error) {
RepositoryName: &a.arn.Resource,
}

for {
//Get pull request IDs
pullRequestsOutput, err := a.ccClient.ListPullRequests(&input)
if err != nil {
return pullRequests, fmt.Errorf("failed to list PRs: %w", err)
}

prIDs := []*string{}

prIDs = append(prIDs, pullRequestsOutput.PullRequestIds...)

for _, id := range prIDs {
pri := codecommit.GetPullRequestInput{PullRequestId: id}

prInfo, err := a.ccClient.GetPullRequest(&pri)
if err != nil {
return pullRequests, fmt.Errorf("failed to get PR info: %w", err)
}
// Get pull request IDs
pullRequestsOutput, err := a.ccClient.ListPullRequests(&input)
if err != nil {
return pullRequests, fmt.Errorf("failed to list PRs: %w", err)
}

pullRequests = append(pullRequests, prInfo.PullRequest)
}
for _, id := range pullRequestsOutput.PullRequestIds {
pri := codecommit.GetPullRequestInput{PullRequestId: id}

if pullRequestsOutput.NextToken == nil {
break
prInfo, err := a.ccClient.GetPullRequest(&pri)
if err != nil {
return pullRequests, fmt.Errorf("failed to get PR info: %w", err)
}

input.NextToken = pullRequestsOutput.NextToken
pullRequests = append(pullRequests, prInfo.PullRequest)
}

return pullRequests, nil
}

// sendPushEvent sends an event containing data about a git commit that was
// pushed to a branch.
func (a *adapter) sendPushEvent(commit *codecommit.Commit) error {
a.logger.Info("Sending Push event")
// sendEvent sends an event containing data about a git commit or PR
func (a *adapter) sendEvent(codeCommitEvent interface{}) error {
a.logger.Info("Sending CodeCommit event")

event := cloudevents.NewEvent(cloudevents.VersionV1)
event.SetType(v1alpha1.AWSEventType(a.arn.Service, pushEventType))
event.SetSubject(fmt.Sprintf("%s/%s", a.arn.Resource, a.branch))
event.SetSubject(a.branch)
event.SetSource(a.arn.String())
event.SetID(*commit.CommitId)
if err := event.SetData(cloudevents.ApplicationJSON, commit); err != nil {
return fmt.Errorf("failed to set event data: %w", err)
}

if result := a.ceClient.Send(context.Background(), event); !cloudevents.IsACK(result) {
return result
switch codeCommitEvent.(type) {
case *codecommit.PullRequest:
event.SetType(v1alpha1.AWSEventType(a.arn.Service, prEventType))
case *codecommit.Commit:
event.SetType(v1alpha1.AWSEventType(a.arn.Service, pushEventType))
default:
return fmt.Errorf("unknown CodeCommit event")
}
return nil
}

func (a *adapter) sendPREvent(pullRequest *codecommit.PullRequest) error {
a.logger.Info("Sending Pull Request event")

event := cloudevents.NewEvent(cloudevents.VersionV1)
event.SetType(v1alpha1.AWSEventType(a.arn.Service, prEventType))
event.SetSubject(a.branch)
event.SetSource(a.arn.String())
event.SetID(*pullRequest.PullRequestId)
if err := event.SetData(cloudevents.ApplicationJSON, pullRequest); err != nil {
err := event.SetData(cloudevents.ApplicationJSON, codeCommitEvent)
if err != nil {
return fmt.Errorf("failed to set event data: %w", err)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/adapter/awscodecommitsource/adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func TestSendPREvent(t *testing.T) {
pr := &codecommit.PullRequest{}
pr.SetPullRequestId("12345")

err := a.sendPREvent(pr)
err := a.sendEvent(pr)
assert.NoError(t, err)

gotEvents := ceClient.Sent()
Expand All @@ -96,7 +96,7 @@ func TestSendPushEvent(t *testing.T) {
commit := &codecommit.Commit{}
commit.SetCommitId("12345")

err := a.sendPushEvent(commit)
err := a.sendEvent(commit)
assert.NoError(t, err)

gotEvents := ceClient.Sent()
Expand Down