Repository: triggermesh/aws-event-sources
Pull request #159 (2020-06-15 13:07:28)

AWS CodeCommit source - avoid "for" loops
- `preparePullRequests` doesn't have `for` loop anymore:
  it's not explicitly documented, but it looks like `nextToken` in response is set only when we either specify list limit or there are more then 1000 items in list. However, our current loop break [condition](https://github.com/triggermesh/aws-event-sources/blob/b39f3d67459a8b0e6fdae3f5e5d773211df0ba47/pkg/adapter/awscodecommitsource/adapter.go#L241) doesn't seem to do a [correct](https://github.com/awsdocs/aws-doc-sdk-examples/blob/master/go/example_code/cloudwatch/CloudWatchGetLogEvents.go#L66) check anyway and we're not hanging there forever only because `nextToken` is not used.
- `sendPushEvent` and `sendPREvent` functions merged into a single `sendEvent` with event type assertion

_mentioned in #58_ 


Patch:
diff --git a/pkg/adapter/awscodecommitsource/adapter.go b/pkg/adapter/awscodecommitsource/adapter.go
index 9ec5fb5..76f0a88 100644
--- a/pkg/adapter/awscodecommitsource/adapter.go
+++ b/pkg/adapter/awscodecommitsource/adapter.go
@@ -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
@@ -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)
 	}
@@ -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)
 	}
 


Test patch:
diff --git a/pkg/adapter/awscodecommitsource/adapter_test.go b/pkg/adapter/awscodecommitsource/adapter_test.go
index 746a7e5..6e8d628 100644
--- a/pkg/adapter/awscodecommitsource/adapter_test.go
+++ b/pkg/adapter/awscodecommitsource/adapter_test.go
@@ -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()
@@ -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()