Skip to content

Commit 95b2746

Browse files
committed
Handle event for PRs being closed
This didn't exist previously. Will require an updated pipeline stack to receive event.
1 parent 2412be9 commit 95b2746

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

builder/build/prebuild.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ var buildkitdConfig = map[string]map[string]map[string][]string{
2929
},
3030
}
3131

32+
const (
33+
ClosedPRStatus = "closed"
34+
MergedPRStatus = "merged"
35+
OpenPRStatus = "open"
36+
CreatedPRStatus = "created"
37+
)
38+
3239
// define a struct named Build
3340
type Build struct {
3441
Appname string
@@ -255,18 +262,21 @@ func (b *Build) ImageName() (string, error) {
255262

256263
func (b *Build) NewPRStatus() string {
257264
if b.CreateReviewApp {
258-
return "created"
265+
return CreatedPRStatus
259266
}
260267
if b.CodebuildWebhookEvent == "PULL_REQUEST_CREATED" || b.CodebuildWebhookEvent == "PULL_REQUEST_REOPENED" {
261-
return "open"
268+
return OpenPRStatus
262269
}
263270
if b.CodebuildWebhookEvent == "PULL_REQUEST_MERGED" {
264-
return "merged"
271+
return MergedPRStatus
272+
}
273+
if b.CodebuildWebhookEvent == "PULL_REQUEST_CLOSED" {
274+
return ClosedPRStatus
265275
}
266276
_, err := b.GetPRStatus()
267277
if err != nil {
268278
b.Log().Debug().Err(err).Msg("failed to get PR status")
269-
return "open"
279+
return OpenPRStatus
270280
}
271281
return ""
272282
}
@@ -280,7 +290,7 @@ func (b *Build) HandlePR() (bool, error) {
280290
}
281291
newStatus := b.NewPRStatus()
282292
b.Log().Debug().Str("status", newStatus).Msg("PR status")
283-
if newStatus == "merged" {
293+
if newStatus == MergedPRStatus || newStatus == ClosedPRStatus {
284294
hasReviewApp, err := b.ReviewAppStackExists()
285295
// TODO err is ignored because it usually means the stack doesn't exist
286296
if err != nil {
@@ -310,7 +320,7 @@ func (b *Build) HandlePR() (bool, error) {
310320
return false, err
311321
}
312322
}
313-
if status.Status != "created" {
323+
if status.Status != CreatedPRStatus {
314324
err = b.SkipBuild()
315325
return true, err
316326
}

builder/build/prebuild_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,40 @@ func TestHandlePRMergedAndDestroy(t *testing.T) {
527527
mockedState.AssertExpectations(t)
528528
}
529529

530+
func TestHandlePRClosedAndDestroy(t *testing.T) {
531+
pr := "pr/123"
532+
appName := "test-app"
533+
mockedAWS := new(MockAWS)
534+
mockedAWS.On(
535+
"DescribeStack",
536+
fmt.Sprintf("apppack-reviewapp-%s%s", appName, strings.Split(pr, "/")[1]),
537+
).Return(&types.Stack{}, nil)
538+
mockedAWS.On(
539+
"DestroyStack",
540+
fmt.Sprintf("apppack-reviewapp-%s%s", appName, strings.Split(pr, "/")[1]),
541+
).Return(nil)
542+
mockedState := emptyState()
543+
b := Build{
544+
Appname: appName,
545+
Pipeline: true,
546+
CodebuildSourceVersion: pr,
547+
CodebuildWebhookEvent: "PULL_REQUEST_CLOSED",
548+
CodebuildBuildId: CodebuildBuildId,
549+
aws: mockedAWS,
550+
state: mockedState,
551+
Ctx: testContext,
552+
}
553+
skip, err := b.HandlePR()
554+
if err != nil {
555+
t.Error("HandlePR should return nil")
556+
}
557+
if !skip {
558+
t.Error("HandlePR should skip the build when the PR is closed")
559+
}
560+
mockedAWS.AssertExpectations(t)
561+
mockedState.AssertExpectations(t)
562+
}
563+
530564
func TestRemoveDuplicateStr(t *testing.T) {
531565
slice := []string{"a", "b", "c", "a", "b"}
532566
expected := []string{"a", "b", "c"}

0 commit comments

Comments
 (0)