Skip to content

Commit 88a0ec6

Browse files
committed
Merge #148: Ignore builds on other branches
Approved-by: rudymatela Auto-deploy: false
2 parents 78cfbfb + 583d864 commit 88a0ec6

File tree

8 files changed

+154
-74
lines changed

8 files changed

+154
-74
lines changed

Diff for: readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ to simulate those:
9595

9696
$ ./tools/comment deckard 31337 @hoffbot merge
9797

98-
$ ./tools/build-status c033170123456789abcdef0123456789abcdef01
98+
$ ./tools/build-status 31337 c033170123456789abcdef0123456789abcdef01
9999

100100
[Comment]: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#issue_comment
101101
[build status]: https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#status

Diff for: src/EventLoop.hs

+5-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,11 @@ mapCommitStatus status murl = case status of
8383

8484
eventFromCommitStatusPayload :: CommitStatusPayload -> Logic.Event
8585
eventFromCommitStatusPayload payload =
86-
let sha = Github.sha (payload :: CommitStatusPayload)
87-
status = Github.status (payload :: CommitStatusPayload)
88-
url = Github.url (payload :: CommitStatusPayload)
89-
in Logic.BuildStatusChanged sha (mapCommitStatus status url)
86+
let sha = Github.sha (payload :: CommitStatusPayload)
87+
status = Github.status (payload :: CommitStatusPayload)
88+
url = Github.url (payload :: CommitStatusPayload)
89+
branches = Github.branches (payload :: CommitStatusPayload)
90+
in Logic.BuildStatusChanged branches sha (mapCommitStatus status url)
9091

9192
convertGithubEvent :: Github.WebhookEvent -> Maybe Logic.Event
9293
convertGithubEvent event = case event of

Diff for: src/Github.hs

+3-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ data CommitStatusPayload = CommitStatusPayload {
9191
repository :: Text, -- Corresponds to "repository.name".
9292
status :: CommitStatus, -- Corresponds to "action".
9393
url :: Maybe Text, -- Corresponds to "target_url".
94-
sha :: Sha -- Corresponds to "sha".
94+
sha :: Sha, -- Corresponds to "sha".
95+
branches :: [Branch] -- Corresponds to "branches[*].name".
9596
} deriving (Eq, Show)
9697

9798
instance FromJSON PullRequestAction where
@@ -168,6 +169,7 @@ instance FromJSON CommitStatusPayload where
168169
<*> (v .: "state")
169170
<*> (v .: "target_url")
170171
<*> (v .: "sha")
172+
<*> ((v .: "branches") >>= traverse (.: "name"))
171173
parseJSON nonObject = typeMismatch "status payload" nonObject
172174

173175
-- Note that GitHub calls pull requests "issues" for the sake of comments: the

Diff for: src/Logic.hs

+13-6
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ data Event
259259
| PullRequestEdited PullRequestId Text BaseBranch -- ^ PR, new title, new base branch.
260260
| CommentAdded PullRequestId Username Text -- ^ PR, author and body.
261261
-- CI events
262-
| BuildStatusChanged Sha BuildStatus
262+
| BuildStatusChanged [Git.Branch] Sha BuildStatus
263263
-- Internal events
264264
| Synchronize
265265
deriving (Eq, Show)
@@ -327,7 +327,8 @@ handleEventInternal triggerConfig projectConfig mergeWindowExemption event = cas
327327
PullRequestEdited pr title baseBranch -> handlePullRequestEdited pr title baseBranch
328328
CommentAdded pr author body
329329
-> handleCommentAdded triggerConfig projectConfig mergeWindowExemption pr author body
330-
BuildStatusChanged sha status -> handleBuildStatusChanged sha status
330+
BuildStatusChanged branches sha status
331+
-> handleBuildStatusChanged projectConfig branches sha status
331332
Synchronize -> synchronizeState
332333

333334
handlePullRequestOpened
@@ -542,13 +543,19 @@ handleMergeRequested projectConfig prId author state pr approvalType = do
542543
then pure $ Pr.setIntegrationStatus prId IncorrectBaseBranch state''
543544
else pure state''
544545

545-
handleBuildStatusChanged :: Sha -> BuildStatus -> ProjectState -> Action ProjectState
546-
handleBuildStatusChanged buildSha newStatus = pure . Pr.updatePullRequests setBuildStatus
546+
handleBuildStatusChanged :: ProjectConfiguration
547+
-> [Git.Branch]
548+
-> Sha -> BuildStatus
549+
-> ProjectState -> Action ProjectState
550+
handleBuildStatusChanged config branches buildSha newStatus =
551+
pure . Pr.updatePullRequestsWithId setBuildStatus
547552
where
548-
setBuildStatus pr = case Pr.integrationStatus pr of
553+
setBuildStatus pid pr = case Pr.integrationStatus pr of
549554
-- If there is an integration candidate, and its integration sha matches that of the build,
550555
-- then update the build status for that pull request. Otherwise do nothing.
551-
Integrated candidateSha oldStatus | candidateSha == buildSha && newStatus /= oldStatus ->
556+
Integrated candidateSha oldStatus | testBranch config pid `elem` branches
557+
&& candidateSha == buildSha
558+
&& newStatus /= oldStatus ->
552559
pr { Pr.integrationStatus = Integrated buildSha newStatus
553560
, Pr.needsFeedback = case newStatus of
554561
BuildStarted _ -> True

Diff for: src/Project.hs

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ module Project
4444
updatePullRequest,
4545
updatePullRequestM,
4646
updatePullRequests,
47+
updatePullRequestsWithId,
4748
getOwners,
4849
wasIntegrationAttemptFor,
4950
MergeWindow(..))
@@ -259,6 +260,10 @@ updatePullRequests f state = state {
259260
pullRequests = IntMap.map f $ pullRequests state
260261
}
261262

263+
updatePullRequestsWithId :: (PullRequestId -> PullRequest -> PullRequest) -> ProjectState -> ProjectState
264+
updatePullRequestsWithId f state = state {
265+
pullRequests = IntMap.mapWithKey (f . PullRequestId) $ pullRequests state
266+
}
262267
-- Marks the pull request as approved by somebody or nobody.
263268
setApproval :: PullRequestId -> Maybe Approval -> ProjectState -> ProjectState
264269
setApproval pr newApproval = updatePullRequest pr changeApproval

Diff for: tests/EventLoopSpec.hs

+22-22
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ eventLoopSpec = parallel $ do
395395
[
396396
Logic.PullRequestOpened pr4 branch baseBranch c4 "Add Leon test results" "deckard",
397397
Logic.CommentAdded pr4 "rachael" "@bot merge",
398-
Logic.BuildStatusChanged c4 BuildSucceeded
398+
Logic.BuildStatusChanged [Branch "integration/4"] c4 BuildSucceeded
399399
]
400400
history `shouldBe`
401401
[ "* c4"
@@ -424,7 +424,7 @@ eventLoopSpec = parallel $ do
424424
[
425425
Logic.PullRequestOpened pr4 branch baseBranch c4 "Add Leon test results" "deckard",
426426
Logic.CommentAdded pr4 "rachael" "@bot merge",
427-
Logic.BuildStatusChanged c4 (BuildFailed Nothing)
427+
Logic.BuildStatusChanged [Branch "integration/4"] c4 (BuildFailed Nothing)
428428
]
429429
-- the build failed, so master's history is unchanged
430430
-- ... and the integration/4 branch is kept for inpection of the CI build
@@ -451,7 +451,7 @@ eventLoopSpec = parallel $ do
451451
[
452452
Logic.PullRequestOpened pr4 branch baseBranch c4 "Deploy tests!" "deckard",
453453
Logic.CommentAdded pr4 "rachael" "@bot merge and tag",
454-
Logic.BuildStatusChanged c4 BuildSucceeded
454+
Logic.BuildStatusChanged [Branch "integration/4"] c4 BuildSucceeded
455455
]
456456
history `shouldBe`
457457
[ "* c4"
@@ -505,7 +505,7 @@ eventLoopSpec = parallel $ do
505505

506506
-- The rebased commit should have been pushed to the remote repository
507507
-- 'integration' branch. Tell that building it succeeded.
508-
void $ runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
508+
void $ runLoop state [Logic.BuildStatusChanged [Branch "integration/4"] rebasedSha BuildSucceeded]
509509

510510
history `shouldBe`
511511
[ "* Merge #4"
@@ -563,7 +563,7 @@ eventLoopSpec = parallel $ do
563563

564564
-- The rebased commit should have been pushed to the remote repository
565565
-- 'integration' branch. Tell that building it succeeded.
566-
void $ runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
566+
void $ runLoop state [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha BuildSucceeded]
567567

568568
history `shouldBe`
569569
[ "* Merge #6"
@@ -600,7 +600,7 @@ eventLoopSpec = parallel $ do
600600

601601
let [rebasedSha] = integrationShas state
602602

603-
void $ runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
603+
void $ runLoop state [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha BuildSucceeded]
604604

605605
history `shouldBe`
606606
[ "* Merge #6"
@@ -658,7 +658,7 @@ eventLoopSpec = parallel $ do
658658

659659
let [rebasedSha] = integrationShas state
660660

661-
void $ runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
661+
void $ runLoop state [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha BuildSucceeded]
662662

663663
history `shouldBe`
664664
[ "* Merge #6"
@@ -722,12 +722,12 @@ eventLoopSpec = parallel $ do
722722

723723
-- The rebased commit should have been pushed to the remote repository
724724
-- 'integration' branch. Tell that building it succeeded.
725-
state' <- runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
725+
state' <- runLoop state [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha BuildSucceeded]
726726

727727
-- Repeat for the other pull request, which should be the candidate by
728728
-- now.
729729
let [rebasedSha'] = integrationShas state'
730-
void $ runLoop state' [Logic.BuildStatusChanged rebasedSha' BuildSucceeded]
730+
void $ runLoop state' [Logic.BuildStatusChanged [Branch "integration/4"] rebasedSha' BuildSucceeded]
731731

732732
history `shouldBe`
733733
[ "* c4"
@@ -761,10 +761,10 @@ eventLoopSpec = parallel $ do
761761

762762
let [rebasedSha] = integrationShas state
763763

764-
state' <- runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
764+
state' <- runLoop state [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha BuildSucceeded]
765765

766766
let [rebasedSha'] = integrationShas state'
767-
void $ runLoop state' [Logic.BuildStatusChanged rebasedSha' BuildSucceeded]
767+
void $ runLoop state' [Logic.BuildStatusChanged [Branch "integration/4"] rebasedSha' BuildSucceeded]
768768

769769
history `shouldBe`
770770
[ "* c4"
@@ -886,15 +886,15 @@ eventLoopSpec = parallel $ do
886886
-- Extract the sha of the rebased commit from the project state, and
887887
-- tell the loop that building the commit succeeded.
888888
let [rebasedSha] = integrationShas state
889-
state' <- runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
889+
state' <- runLoop state [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha BuildSucceeded]
890890

891891
-- The push should have failed, hence there should still be an
892892
-- integration candidate.
893893
Project.integratedPullRequests state' `shouldSatisfy` (not . null)
894894

895895
-- Again notify build success, now for the new commit.
896896
let [rebasedSha'] = integrationShas state'
897-
state'' <- runLoop state' [Logic.BuildStatusChanged rebasedSha' BuildSucceeded]
897+
state'' <- runLoop state' [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha' BuildSucceeded]
898898

899899
-- After the second build success, the pull request should have been
900900
-- integrated properly, so there should not be a new candidate.
@@ -937,11 +937,11 @@ eventLoopSpec = parallel $ do
937937
git ["push", "origin", refSpec (c4, masterBranch)]
938938

939939
let [rebasedSha] = integrationShas state
940-
state' <- runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
940+
state' <- runLoop state [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha BuildSucceeded]
941941

942942
-- Again notify build success, now for the new commit.
943943
let [rebasedSha'] = integrationShas state'
944-
void $ runLoop state' [Logic.BuildStatusChanged rebasedSha' BuildSucceeded]
944+
void $ runLoop state' [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha' BuildSucceeded]
945945

946946
-- After the second build success, the pull request should have been
947947
-- integrated properly, version should be incremented only once
@@ -1009,11 +1009,11 @@ eventLoopSpec = parallel $ do
10091009
git ["push", "origin", refSpec (Git.TagName "v2")]
10101010

10111011
let [rebasedSha] = integrationShas state
1012-
state' <- runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
1012+
state' <- runLoop state [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha BuildSucceeded]
10131013

10141014
-- Again notify build success, now for the new commit.
10151015
let [rebasedSha'] = integrationShas state'
1016-
void $ runLoop state' [Logic.BuildStatusChanged rebasedSha' BuildSucceeded]
1016+
void $ runLoop state' [Logic.BuildStatusChanged [Branch "integration/6"] rebasedSha' BuildSucceeded]
10171017

10181018
-- After the second build success, the pull request should have been integrated properly,
10191019
-- version should be incremented only once, and follow version that appeared in the meantime
@@ -1078,7 +1078,7 @@ eventLoopSpec = parallel $ do
10781078
-- Extract the sha of the rebased commit from the project state, and
10791079
-- tell the loop that building the commit succeeded.
10801080
let [rebasedSha] = integrationShas state
1081-
void $ runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
1081+
void $ runLoop state [Logic.BuildStatusChanged [Branch "integration/8"] rebasedSha BuildSucceeded]
10821082

10831083
-- We expect the fixup commit (which was last) to be squashed into c7, so
10841084
-- now c8 is the last commit, and there are no others. Note that if the
@@ -1118,11 +1118,11 @@ eventLoopSpec = parallel $ do
11181118
-- Extract the sha of the rebased commit from the project state, and
11191119
-- tell the loop that building the commit succeeded.
11201120
let [rebasedSha] = integrationShas state
1121-
state' <- runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
1121+
state' <- runLoop state [Logic.BuildStatusChanged [Branch "integration/8"] rebasedSha BuildSucceeded]
11221122

11231123
-- Again notify build success, now for the new commit.
11241124
let [rebasedSha'] = integrationShas state'
1125-
void $ runLoop state' [Logic.BuildStatusChanged rebasedSha' BuildSucceeded]
1125+
void $ runLoop state' [Logic.BuildStatusChanged [Branch "integration/8"] rebasedSha' BuildSucceeded]
11261126

11271127
-- We expect the fixup commit (which was last) to be squashed into c7, so
11281128
-- now c8 is the last commit, and there are no others. This time c4 and c5
@@ -1168,7 +1168,7 @@ eventLoopSpec = parallel $ do
11681168
-- tell the loop that building the commit succeeded.
11691169

11701170
let [rebasedSha] = integrationShas state
1171-
state' <- runLoop state [Logic.BuildStatusChanged rebasedSha BuildSucceeded]
1171+
state' <- runLoop state [Logic.BuildStatusChanged [Branch "integration/8"] rebasedSha BuildSucceeded]
11721172

11731173
--The pull request should not be integrated. Moreover, the presence of
11741174
--orphan fixups should make the PR ineligible for being a candidate for integration.
@@ -1210,7 +1210,7 @@ eventLoopSpec = parallel $ do
12101210

12111211
state' <- runLoop state
12121212
[
1213-
Logic.BuildStatusChanged rebasedSha BuildSucceeded,
1213+
Logic.BuildStatusChanged [Branch "integration/8"] rebasedSha BuildSucceeded,
12141214
Logic.CommentAdded pr6 "rachael" "@bot merge"
12151215
]
12161216

0 commit comments

Comments
 (0)