Skip to content

Commit 4cbf8e2

Browse files
committed
Get rid of "for" loop
1 parent fa283ab commit 4cbf8e2

File tree

2 files changed

+34
-53
lines changed

2 files changed

+34
-53
lines changed

Diff for: pkg/adapter/awscodecommitsource/adapter.go

+32-51
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func (a *adapter) Start(stopCh <-chan struct{}) error {
164164

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

202202
lastCommit = *commitOutput.Commit.CommitId
203203

204-
err = a.sendPushEvent(commitOutput.Commit)
204+
err = a.sendEvent(commitOutput.Commit)
205205
if err != nil {
206206
return fmt.Errorf("failed to send push event: %w", err)
207207
}
@@ -216,68 +216,49 @@ func (a *adapter) preparePullRequests() ([]*codecommit.PullRequest, error) {
216216
RepositoryName: &a.arn.Resource,
217217
}
218218

219-
for {
220-
//Get pull request IDs
221-
pullRequestsOutput, err := a.ccClient.ListPullRequests(&input)
222-
if err != nil {
223-
return pullRequests, fmt.Errorf("failed to list PRs: %w", err)
224-
}
225-
226-
prIDs := []*string{}
227-
228-
prIDs = append(prIDs, pullRequestsOutput.PullRequestIds...)
229-
230-
for _, id := range prIDs {
231-
pri := codecommit.GetPullRequestInput{PullRequestId: id}
232-
233-
prInfo, err := a.ccClient.GetPullRequest(&pri)
234-
if err != nil {
235-
return pullRequests, fmt.Errorf("failed to get PR info: %w", err)
236-
}
219+
//Get pull request IDs
220+
pullRequestsOutput, err := a.ccClient.ListPullRequests(&input)
221+
if err != nil {
222+
return pullRequests, fmt.Errorf("failed to list PRs: %w", err)
223+
}
237224

238-
pullRequests = append(pullRequests, prInfo.PullRequest)
239-
}
225+
for _, id := range pullRequestsOutput.PullRequestIds {
226+
pri := codecommit.GetPullRequestInput{PullRequestId: id}
240227

241-
if pullRequestsOutput.NextToken == nil {
242-
break
228+
prInfo, err := a.ccClient.GetPullRequest(&pri)
229+
if err != nil {
230+
return pullRequests, fmt.Errorf("failed to get PR info: %w", err)
243231
}
244232

245-
input.NextToken = pullRequestsOutput.NextToken
233+
pullRequests = append(pullRequests, prInfo.PullRequest)
246234
}
247235

248236
return pullRequests, nil
249237
}
250238

251-
// sendPushEvent sends an event containing data about a git commit that was
252-
// pushed to a branch.
253-
func (a *adapter) sendPushEvent(commit *codecommit.Commit) error {
254-
a.logger.Info("Sending Push event")
255-
256-
event := cloudevents.NewEvent(cloudevents.VersionV1)
257-
event.SetType(v1alpha1.AWSEventType(a.arn.Service, pushEventType))
258-
event.SetSubject(fmt.Sprintf("%s/%s", a.arn.Resource, a.branch))
259-
event.SetSource(a.arn.String())
260-
event.SetID(*commit.CommitId)
261-
if err := event.SetData(cloudevents.ApplicationJSON, commit); err != nil {
262-
return fmt.Errorf("failed to set event data: %w", err)
263-
}
264-
265-
if result := a.ceClient.Send(context.Background(), event); !cloudevents.IsACK(result) {
266-
return result
267-
}
268-
return nil
269-
}
270-
271-
func (a *adapter) sendPREvent(pullRequest *codecommit.PullRequest) error {
272-
a.logger.Info("Sending Pull Request event")
239+
// sendEvent sends an event containing data about a git commit or PR
240+
func (a *adapter) sendEvent(codeCommitEvent interface{}) error {
241+
a.logger.Info("Sending CodeCommit event")
273242

274243
event := cloudevents.NewEvent(cloudevents.VersionV1)
275-
event.SetType(v1alpha1.AWSEventType(a.arn.Service, prEventType))
276244
event.SetSubject(a.branch)
277245
event.SetSource(a.arn.String())
278-
event.SetID(*pullRequest.PullRequestId)
279-
if err := event.SetData(cloudevents.ApplicationJSON, pullRequest); err != nil {
280-
return fmt.Errorf("failed to set event data: %w", err)
246+
247+
switch v := codeCommitEvent.(type) {
248+
case *codecommit.PullRequest:
249+
event.SetType(v1alpha1.AWSEventType(a.arn.Service, prEventType))
250+
err := event.SetData(cloudevents.ApplicationJSON, v)
251+
if err != nil {
252+
return fmt.Errorf("failed to set event data: %w", err)
253+
}
254+
case *codecommit.Commit:
255+
event.SetType(v1alpha1.AWSEventType(a.arn.Service, pushEventType))
256+
err := event.SetData(cloudevents.ApplicationJSON, v)
257+
if err != nil {
258+
return fmt.Errorf("failed to set event data: %w", err)
259+
}
260+
default:
261+
return fmt.Errorf("unknown CodeCommit event")
281262
}
282263

283264
if result := a.ceClient.Send(context.Background(), event); !cloudevents.IsACK(result) {

Diff for: pkg/adapter/awscodecommitsource/adapter_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func TestSendPREvent(t *testing.T) {
7373
pr := &codecommit.PullRequest{}
7474
pr.SetPullRequestId("12345")
7575

76-
err := a.sendPREvent(pr)
76+
err := a.sendEvent(pr)
7777
assert.NoError(t, err)
7878

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

99-
err := a.sendPushEvent(commit)
99+
err := a.sendEvent(commit)
100100
assert.NoError(t, err)
101101

102102
gotEvents := ceClient.Sent()

0 commit comments

Comments
 (0)