diff --git a/README.md b/README.md index 61c65ec..605bc8c 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/assets/lib/commands/out.rb b/assets/lib/commands/out.rb index 547a581..0d2e4a0 100755 --- a/assets/lib/commands/out.rb +++ b/assets/lib/commands/out.rb @@ -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 diff --git a/spec/commands/out_spec.rb b/spec/commands/out_spec.rb index a6524f0..d73297c 100644 --- a/spec/commands/out_spec.rb +++ b/spec/commands/out_spec.rb @@ -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 @@ -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) @@ -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