-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable Webhooks for Pull Requests #746
Changes from 2 commits
c8c1957
7809e41
b8c8257
9b239fa
09b7530
2d6a0cb
b8229ed
ad71fee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class Changeset::CodePush | ||
attr_reader :repo, :data | ||
|
||
def initialize(repo, data) | ||
@repo = repo | ||
@data = data | ||
end | ||
|
||
def self.changeset_from_webhook(project, params = {}) | ||
new(project.github_repo, params) | ||
end | ||
|
||
def self.valid_webhook?(_) | ||
true | ||
end | ||
|
||
def sha | ||
data[:after] | ||
end | ||
|
||
def branch | ||
data[:ref][/refs\/heads\/(.+)/, 1] | ||
end | ||
|
||
def service_type | ||
'code' # Samson webhook category | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
class Changeset::IssueComment | ||
attr_reader :repo, :data | ||
|
||
def initialize(repo, data) | ||
@repo = repo | ||
@body = data['body'] | ||
@data = data['issue'] | ||
end | ||
|
||
def self.changeset_from_webhook(project, params = {}) | ||
new(project.github_repo, params) | ||
end | ||
|
||
def self.valid_webhook?(params) | ||
comment = params[:comment] || {} | ||
comment[:body] =~ Changeset::PullRequest::WEBHOOK_FILTER | ||
end | ||
|
||
def sha | ||
pull_request.sha | ||
end | ||
|
||
def branch | ||
pull_request.sha | ||
end | ||
|
||
def service_type | ||
'pull_request' # Samson webhook category | ||
end | ||
|
||
private | ||
|
||
def pull_request | ||
@pull_request ||= Changeset::PullRequest.find(repo, data['number']) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ class Changeset::PullRequest | |
CODE_ONLY = "[A-Z][A-Z\\d]+-\\d+" # e.g., S4MS0N-123, SAM-456 | ||
PUNCT = "\\s|\\p{Punct}|~|=" | ||
|
||
WEBHOOK_FILTER = /(\[)\s*(samson)\s*(\])/i # [samson] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about |
||
|
||
# Matches a markdown section heading named "Risks". | ||
RISKS_SECTION = /^\s*#*\s*Risks?\s*#*\s*\n(?:\s*[-=]*\s*\n)?/i | ||
|
||
|
@@ -31,6 +33,17 @@ def self.find(repo, number) | |
nil | ||
end | ||
|
||
def self.changeset_from_webhook(project, params = {}) | ||
find(project.github_repo, params[:number]) | ||
end | ||
|
||
def self.valid_webhook?(params) | ||
data = params[:pull_request] || {} | ||
return false unless data[:state] == 'open' | ||
|
||
data[:body] =~ WEBHOOK_FILTER || data.fetch(:base, {})[:ref] == 'production' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. production PRs are always valid ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the description said it would ignore production ... but this does the opposite ?
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant in the description that it would ignore the "requirement" of having [samson] present. However, the more I think about this the more I'm thinking we should always require an explicit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah i'm for requiring an explicit user action to kick this off |
||
end | ||
|
||
attr_reader :repo | ||
|
||
def initialize(repo, data) | ||
|
@@ -51,6 +64,18 @@ def reference | |
"##{number}" | ||
end | ||
|
||
def sha | ||
@data.head.sha | ||
end | ||
|
||
def branch | ||
@data.head.ref | ||
end | ||
|
||
def state | ||
@data.state | ||
end | ||
|
||
def users | ||
users = [@data.user, @data.merged_by] | ||
users.compact.map {|user| Changeset::GithubUser.new(user) }.uniq | ||
|
@@ -71,6 +96,10 @@ def jira_issues | |
@jira_issues ||= parse_jira_issues | ||
end | ||
|
||
def service_type | ||
'pull_request' # Samson webhook category | ||
end | ||
|
||
private | ||
|
||
def parse_risks(body) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,11 @@ | |
|
||
before do | ||
Deploy.delete_all | ||
|
||
Integrations::GithubController.github_hook_secret = 'test' | ||
end | ||
|
||
project.webhooks.create!(stage: stages(:test_staging), branch: "dev", source: 'github') | ||
does_not_deploy 'when the event is invalid' do | ||
request.headers['X-Github-Event'] = 'event' | ||
end | ||
|
||
it 'does not deploy if signature is invalid' do | ||
|
@@ -31,28 +32,57 @@ | |
response.status.must_equal 200 | ||
end | ||
|
||
it 'does not deploy if event is invalid' do | ||
request.headers['X-Github-Event'] = 'event' | ||
|
||
hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), 'test', payload.to_param) | ||
request.headers['X-Hub-Signature'] = "sha1=#{hmac}" | ||
describe 'with a code push event' do | ||
before do | ||
request.headers['X-Github-Event'] = 'code_push' | ||
project.webhooks.create!(stage: stages(:test_staging), branch: "dev", source: 'github') | ||
end | ||
|
||
post :create, payload.merge(token: project.token) | ||
let(:user_name) { 'Github' } | ||
|
||
project.deploys.must_equal [] | ||
response.status.must_equal 200 | ||
test_regular_commit "Github", no_mapping: { ref: 'refs/heads/foobar' }, failed: false do | ||
request.headers['X-Github-Event'] = 'push' | ||
hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), 'test', payload.to_param) | ||
request.headers['X-Hub-Signature'] = "sha1=#{hmac}" | ||
end | ||
end | ||
|
||
describe 'with a valid signature' do | ||
describe 'with a pull request event' do | ||
before do | ||
request.headers['X-Github-Event'] = 'pull_request' | ||
project.webhooks.create!(stage: stages(:test_staging), branch: "dev", source: 'any_pull_request') | ||
end | ||
|
||
let(:user_name) { 'Github' } | ||
let(:payload) do | ||
{ | ||
ref: 'refs/heads/dev', | ||
after: commit, | ||
number: '42', | ||
pull_request: {state: 'open', body: 'imafixwolves [samson]'} | ||
}.with_indifferent_access | ||
end | ||
let(:api_response) do | ||
stub({ | ||
user: stub(login: 'foo'), | ||
merged_by: stub(login: 'bar'), | ||
body: '', | ||
head: stub(sha: commit, ref: 'refs/heads/dev') | ||
}) | ||
end | ||
|
||
before do | ||
request.headers['X-Github-Event'] = 'push' | ||
does_not_deploy 'with a non-open pull request state' do | ||
payload.deep_merge!(pull_request: {state: 'closed'}) | ||
end | ||
|
||
hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), 'test', payload.to_param) | ||
request.headers['X-Hub-Signature'] = "sha1=#{hmac}" | ||
does_not_deploy 'without "[samson]" in the body' do | ||
payload.deep_merge!(pull_request: {body: 'imafixwolves'}) | ||
end | ||
|
||
test_regular_commit "Github", no_mapping: { ref: 'refs/heads/foobar' }, failed: false | ||
# test_regular_commit "Github", no_mapping: { ref: 'refs/heads/foobar' }, failed: false do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. delete or uncomment ? |
||
# GITHUB.stubs(:pull_request).with('bar/foo', '42').returns(api_response) | ||
# hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), 'test', payload.to_param) | ||
# request.headers['X-Hub-Signature'] = "sha1=#{hmac}" | ||
# end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
DataStruct = Struct.new(:user, :merged_by, :body, :title) | ||
UserStruct = Struct.new(:login) | ||
|
||
let(:project) { projects(:test) } | ||
let(:data) { DataStruct.new(user, merged_by, body) } | ||
let(:pr) { Changeset::PullRequest.new("xxx", data) } | ||
let(:user) { UserStruct.new("foo") } | ||
|
@@ -29,6 +30,22 @@ | |
end | ||
end | ||
|
||
describe ".changeset_from_webhook" do | ||
before do | ||
Rails.cache.clear | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does this come from? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leftover from when the |
||
end | ||
|
||
it 'finds the pull request' do | ||
GITHUB.stubs(:pull_request).with("bar/foo", 42).returns(data) | ||
data.title = "Make it bigger!" | ||
|
||
webhook_data = {number: 42, pull_request: {state: 'open'}} | ||
pr = Changeset::PullRequest.changeset_from_webhook(project, webhook_data) | ||
|
||
pr.title.must_equal "Make it bigger!" | ||
end | ||
end | ||
|
||
describe "#users" do | ||
it "returns the users associated with the pull request" do | ||
pr.users.map(&:login).must_equal ["foo", "bar"] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
pull_request.branch
?