@@ -164,7 +164,7 @@ func (a *adapter) Start(stopCh <-chan struct{}) error {
164
164
165
165
for _ , pr := range pullRequests {
166
166
resetBackoff = true
167
- err = a .sendPREvent (pr )
167
+ err = a .sendEvent (pr )
168
168
if err != nil {
169
169
a .logger .Errorw ("Failed to send PR event" , "error" , err )
170
170
return resetBackoff , nil
@@ -201,7 +201,7 @@ func (a *adapter) processCommits() error {
201
201
202
202
lastCommit = * commitOutput .Commit .CommitId
203
203
204
- err = a .sendPushEvent (commitOutput .Commit )
204
+ err = a .sendEvent (commitOutput .Commit )
205
205
if err != nil {
206
206
return fmt .Errorf ("failed to send push event: %w" , err )
207
207
}
@@ -216,68 +216,49 @@ func (a *adapter) preparePullRequests() ([]*codecommit.PullRequest, error) {
216
216
RepositoryName : & a .arn .Resource ,
217
217
}
218
218
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
+ }
237
224
238
- pullRequests = append ( pullRequests , prInfo . PullRequest )
239
- }
225
+ for _ , id := range pullRequestsOutput . PullRequestIds {
226
+ pri := codecommit. GetPullRequestInput { PullRequestId : id }
240
227
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 )
243
231
}
244
232
245
- input . NextToken = pullRequestsOutput . NextToken
233
+ pullRequests = append ( pullRequests , prInfo . PullRequest )
246
234
}
247
235
248
236
return pullRequests , nil
249
237
}
250
238
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" )
273
242
274
243
event := cloudevents .NewEvent (cloudevents .VersionV1 )
275
- event .SetType (v1alpha1 .AWSEventType (a .arn .Service , prEventType ))
276
244
event .SetSubject (a .branch )
277
245
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" )
281
262
}
282
263
283
264
if result := a .ceClient .Send (context .Background (), event ); ! cloudevents .IsACK (result ) {
0 commit comments