Skip to content

Commit 597d235

Browse files
authored
fix(github-hooks): support ready_for_review hook from github (#518)
## 📝 Description Draft PRs that are converted to regular PRs don't trigger builds because the GitHub webhook action `ready_for_review` is not in the list of supported actions in the webhook filter. ## ✅ Checklist - [x] I have tested this change - [ ] This change requires documentation update
1 parent 66f2436 commit 597d235

File tree

8 files changed

+64
-1
lines changed

8 files changed

+64
-1
lines changed

github_hooks/lib/repo_host/api/payload.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ def pull_request?
1515
end
1616
alias_method :is_pull_request?, :pull_request?
1717

18+
def draft_pull_request?
19+
false
20+
end
21+
22+
def pull_request_ready_for_review?
23+
false
24+
end
25+
1826
def pr_head_repo_name
1927
nil
2028
end

github_hooks/lib/repo_host/bitbucket/payload.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ def draft_pull_request?
149149
false
150150
end
151151

152+
def pull_request_ready_for_review?
153+
false
154+
end
155+
152156
# ❌
153157
def pull_request_within_repo?
154158
return false unless pull_request?

github_hooks/lib/repo_host/git/payload.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def draft_pull_request?
1919
false
2020
end
2121

22+
def pull_request_ready_for_review?
23+
false
24+
end
25+
2226
def pr_head_repo_name
2327
nil
2428
end

github_hooks/lib/repo_host/github/payload.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Payload
99
PULL_REQUEST_REOPENED = "reopened"
1010
PULL_REQUEST_CLOSED = "closed"
1111
PULL_REQUEST_COMMIT = "synchronize"
12+
PULL_REQUEST_READY_FOR_REVIEW = "ready_for_review"
1213

1314
def initialize(payload)
1415
@data = JSON.parse(payload)
@@ -184,6 +185,10 @@ def pull_request_commit?
184185
@action == PULL_REQUEST_COMMIT
185186
end
186187

188+
def pull_request_ready_for_review?
189+
@action == PULL_REQUEST_READY_FOR_REVIEW
190+
end
191+
187192
def pull_request_number
188193
@data["number"] if pull_request?
189194
end

github_hooks/lib/repo_host/gitlab/payload.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ def draft_pull_request?
3535
pull_request? && data.dig("object_attributes", "draft")
3636
end
3737

38+
def pull_request_ready_for_review?
39+
return false unless pull_request?
40+
return false unless data.dig("object_attributes", "action") == "update"
41+
42+
# Check if it was previously draft and is now not draft
43+
data.dig("changes", "draft", "previous") == true &&
44+
data.dig("changes", "draft", "current") == false
45+
end
46+
3847
def pr_head_repo_name
3948
data.dig("object_attributes", "repository", "name")
4049
end

github_hooks/lib/semaphore/repo_host/github/webhook_filter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class WebhookFilter
55
MEMBER_GITHUB_WEBHOOK_EVENT = ["member", "membership", "team"]
66
GITHUB_APP_WEBHOOK_EVENTS = ["installation", "installation_repositories"]
77
SUPPORTED_GITHUB_WEBHOOK_EVENTS = ["push", "pull_request", "member", "issue_comment", "installation", "installation_repositories", "team", "membership", "repository"]
8-
SUPPORTED_GITHUB_PULL_REQUEST_ACTIONS = ["opened", "synchronize", "closed", "reopened"]
8+
SUPPORTED_GITHUB_PULL_REQUEST_ACTIONS = ["opened", "synchronize", "closed", "reopened", "ready_for_review"]
99
SUPPORTED_PR_COMMANDS = ["/sem-approve"]
1010

1111
def initialize(request, payload)

github_hooks/lib/semaphore/repo_host/hooks/handler.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ def self.run(workflow, logger, hook_payload = "", signature = "", retries = 0)
194194
return
195195
end
196196

197+
# Skip ready_for_review events when build_draft_pr is enabled
198+
# (the PR was already building as a draft)
199+
if draft_pr_allowed?(workflow.project) && workflow.payload.pull_request_ready_for_review?
200+
logger.info("skip-ready-for-review-when-building-drafts")
201+
workflow.update(:state => Workflow::STATE_SKIP_DRAFT_PR)
202+
return
203+
end
204+
197205
state, meta, msg = update_pr_data(workflow.project, workflow.pull_request_number, workflow.commit_sha)
198206
case state
199207
when :not_found

github_hooks/spec/lib/semaphore/repo_host/github/webhook_filter_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,31 @@
173173

174174
end
175175

176+
context "ready_for_review" do
177+
178+
let(:payload) do
179+
<<-PAYLOAD
180+
{
181+
"action": "ready_for_review",
182+
"pull_request":{
183+
"draft": false,
184+
"head":{
185+
"label": "owner_1:branch"
186+
},
187+
"base":{
188+
"label": "owner_2:branch"
189+
}
190+
}
191+
}
192+
PAYLOAD
193+
end
194+
195+
it "returns false" do
196+
expect(filter.unsupported_webhook?).to eql(false)
197+
end
198+
199+
end
200+
176201
context "other" do
177202

178203
let(:payload) do

0 commit comments

Comments
 (0)