Skip to content
This repository was archived by the owner on Nov 8, 2018. It is now read-only.

Add comment_text parameter to add a PR comment using static text #179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ Set the status message for `concourse-ci` context on specified pull request.

* `comment`: *Optional.* The file path of the comment message. Comment owner is same with the owner of `access_token`.

* `comment_text`: *Optional.* Static text of the comment message. Comment owner is same with the owner of `access_token`.

This supports the [build environment](http://concourse.ci/implementing-resources.html#resource-metadata)
variables provided by concourse.

In addition, if you specify both `comment` and `comment_text`, the `$COMMENT_FILE_CONTENT` variable can be used in
the `comment_text` string to include the contents of the `comment` file with other static text and interpolated
variables.

* `merge.method`: *Optional.* Use this to merge the PR into the target branch of the PR. There are three available merge methods -- `merge`, `squash`, or `rebase`. Please this [doc](https://developer.github.com/changes/2016-09-26-pull-request-merge-api-update/) for more information.

* `merge.commit_msg`: *Optional.* Used with `merge` to set the commit message for the merge. Specify a file path to the merge commit message.
Expand Down
16 changes: 13 additions & 3 deletions assets/lib/commands/out.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,19 @@ def output
).create!
end

if params.comment
comment_path = File.join(destination, params.comment)
comment = File.read(comment_path, encoding: Encoding::UTF_8)
if params.comment || params.comment_text
if params.comment
comment_path = File.join(destination, params.comment)
comment_file_contents = File.read(comment_path, encoding: Encoding::UTF_8)
end

if params.comment_text
comment = whitelist(context: params.comment_text)
comment.gsub!("$COMMENT_FILE_CONTENT", comment_file_contents || '')
else
comment = comment_file_contents
end

Octokit.add_comment(input.source.repo, id, comment)
metadata << { 'name' => 'comment', 'value' => comment }
end
Expand Down
45 changes: 43 additions & 2 deletions spec/commands/out_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def stub_json(method, uri, body)
end

context 'when the merge is set' do
it 'retuns an error for unsupported merge types' do
it 'returns an error for unsupported merge types' do
stub_status_post

expect do
Expand Down Expand Up @@ -139,7 +139,7 @@ def stub_json(method, uri, body)
File.write(File.join(dest_dir, 'comment'), 'comment message')
end

it 'posts a comment to the PR\'s SHA' do
it 'posts a comment file to the PR\'s SHA' do
stub_status_post
stub_json(:post, 'https://api.github.com:443/repos/jtarchie/test/issues/1/comments', id: 1)

Expand All @@ -152,6 +152,47 @@ def stub_json(method, uri, body)
])
end

it 'posts a static text comment to the PR\'s SHA' do
stub_status_post
stub_json(:post, 'https://api.github.com:443/repos/jtarchie/test/issues/1/comments', id: 1)

output, = put('params' => { 'status' => 'success', 'path' => 'resource', 'comment_text' => 'static comment message' }, 'source' => { 'repo' => 'jtarchie/test' })
expect(output).to eq('version' => { 'ref' => @sha, 'pr' => '1' },
'metadata' => [
{ 'name' => 'status', 'value' => 'success' },
{ 'name' => 'url', 'value' => 'http://example.com' },
{ 'name' => 'comment', 'value' => 'static comment message' }
])
end

it 'posts a static text comment with environment variables to the PR\'s SHA' do
stub_status_post
stub_json(:post, 'https://api.github.com:443/repos/jtarchie/test/issues/1/comments', id: 1)

ENV['BUILD_TEAM_NAME'] = 'build-env-var'
output, = put('params' => { 'status' => 'success', 'path' => 'resource', 'comment_text' => 'static comment message $BUILD_TEAM_NAME' }, 'source' => { 'repo' => 'jtarchie/test' })
expect(output).to eq('version' => { 'ref' => @sha, 'pr' => '1' },
'metadata' => [
{ 'name' => 'status', 'value' => 'success' },
{ 'name' => 'url', 'value' => 'http://example.com' },
{ 'name' => 'comment', 'value' => 'static comment message build-env-var' }
])
ENV['BUILD_TEAM_NAME'] = ''
end

it 'posts a static text comment with comment file contents to the PR\'s SHA' do
stub_status_post
stub_json(:post, 'https://api.github.com:443/repos/jtarchie/test/issues/1/comments', id: 1)

output, = put('params' => { 'status' => 'success', 'path' => 'resource', 'comment' => 'resource/comment', 'comment_text' => 'static comment message: $COMMENT_FILE_CONTENT' }, 'source' => { 'repo' => 'jtarchie/test' })
expect(output).to eq('version' => { 'ref' => @sha, 'pr' => '1' },
'metadata' => [
{ 'name' => 'status', 'value' => 'success' },
{ 'name' => 'url', 'value' => 'http://example.com' },
{ 'name' => 'comment', 'value' => 'static comment message: comment message' }
])
end

context 'when the message file does not exist' do
it 'returns a helpful error message' do
expect do
Expand Down