From f83ef3b53969824cee60b6ba23446f6a31f325e2 Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Fri, 29 Jun 2018 18:27:25 -0700 Subject: [PATCH 1/2] Working sha override for merge commit. --- assets/lib/commands/out.rb | 9 ++++++++- assets/lib/pull_request.rb | 4 ++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/assets/lib/commands/out.rb b/assets/lib/commands/out.rb index 547a581..2d47379 100755 --- a/assets/lib/commands/out.rb +++ b/assets/lib/commands/out.rb @@ -41,6 +41,13 @@ def output version = { 'ref' => sha } else pr = PullRequest.from_github(repo: repo, id: id) + if sha == pr.merge_commit_sha + # If the SHA that we have here is the merge commit's SHA, + # we know that the correct SHA is the one at the tip of + # the PR (which might be the same as the merge SHA if + # there's no need for a merge). + sha = pr.sha + end metadata << { 'name' => 'url', 'value' => pr.url } version = { 'pr' => id, 'ref' => sha } end @@ -77,7 +84,7 @@ def output File.read(commit_path, encoding: Encoding::UTF_8) else '' - end + end Octokit.merge_pull_request(input.source.repo, id, commit_msg, merge_method: params.merge.method, accept: 'application/vnd.github.polaris-preview+json') metadata << { 'name' => 'merge', 'value' => params.merge.method } metadata << { 'name' => 'merge_commit_msg', 'value' => commit_msg } diff --git a/assets/lib/pull_request.rb b/assets/lib/pull_request.rb index 6c329ea..dea07bc 100644 --- a/assets/lib/pull_request.rb +++ b/assets/lib/pull_request.rb @@ -47,6 +47,10 @@ def sha @pr['head']['sha'] end + def merge_commit_sha + @pr['merge_commit_sha'] + end + def url @pr['html_url'] end From baefc467ed3914d99a68d979b7d34b4545b85ea3 Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Mon, 2 Jul 2018 12:30:06 -0700 Subject: [PATCH 2/2] Add test for merge commit detection. --- spec/commands/out_spec.rb | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/spec/commands/out_spec.rb b/spec/commands/out_spec.rb index a6524f0..e0606a9 100644 --- a/spec/commands/out_spec.rb +++ b/spec/commands/out_spec.rb @@ -303,6 +303,23 @@ def stub_json(method, uri, body) { 'name' => 'url', 'value' => 'http://example.com' } ]) end + context 'when fetching a merged commit' do + it 'posts the status to the PR\'s SHA instead of the merge SHA.' do + stub_json(:get, 'https://api.github.com:443/repos/jtarchie/test/pulls/1', + html_url: 'http://example.com', + number: 1, + head: { sha: 'abcdef' }, + merge_commit_sha: @sha) + stub_request(:post, 'https://api.github.com:443/repos/jtarchie/test/statuses/abcdef') + output, = put('params' => { 'status' => 'failure', 'path' => 'resource' }, 'source' => { 'repo' => 'jtarchie/test' }) + expect(output).to eq('version' => { 'ref' => 'abcdef', 'pr' => '1' }, + 'metadata' => [ + { 'name' => 'status', 'value' => 'failure' }, + { 'name' => 'url', 'value' => 'http://example.com' } + ]) + + end + end end end end