Skip to content

Commit 7aae0e3

Browse files
Merge pull request #16 from emerald-squad/custom-comment
Add a custom comment after the comment made by the resource
2 parents bbbe35d + 890dbf7 commit 7aae0e3

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ Set the status message on specified pull request.
114114

115115
* [`on_success`](https://concourse.ci/on-success-step.html) and [`on_failure`](https://concourse.ci/on-failure-step.html) triggers may be useful for you when you wanted to reflect build result to the pull request (see the example below).
116116

117+
* `comment`: *Optional.* A custom comment that you want added to the status message.
118+
Any occurence of `[[BRANCH]]` will be replace by the actual branch name form the
119+
pull request.
120+
121+
* `commentFile`: *Optional.* The path to a file that contains a custom comment to
122+
add to the message. This allow the comment to be built by a previous task in the job.
123+
117124
## Example pipeline
118125

119126
```yaml

assets/helpers/bitbucket.sh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,33 @@ bitbucket_pullrequest_progress_comment() {
158158
# $2: hash of merge commit
159159
# $3: hash of source commit
160160
# $4: hash of target commit
161+
# $5: custom comment
161162
local hash="$2"
162163

163164
local progress_msg_end=""
165+
local custom_comment=""
166+
164167
if [ "$hash" == "$3" ]; then
165168
progress_msg_end+=" into $4]"
166169
else
167170
progress_msg_end="] $3 into $4"
168171
fi
169172

173+
if [ -n "$5" ]; then
174+
custom_comment="\n\n$5"
175+
fi
176+
170177
local build_url="$ATC_EXTERNAL_URL/teams/$(rawurlencode "$BUILD_TEAM_NAME")/pipelines/$(rawurlencode "$BUILD_PIPELINE_NAME")/jobs/$(rawurlencode "$BUILD_JOB_NAME")/builds/$(rawurlencode "$BUILD_NAME")"
171178
local build_result_pre=" \n\n **["
172179
local build_result_post="]($build_url)** - Build #$BUILD_NAME"
173180

174181
case "$1" in
175182
success)
176-
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✓ BUILD SUCCESS${build_result_post}" ;;
183+
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✓ BUILD SUCCESS${build_result_post}${custom_comment}" ;;
177184
failure)
178-
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✕ BUILD FAILED${build_result_post}" ;;
185+
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✕ BUILD FAILED${build_result_post}${custom_comment}" ;;
179186
pending)
180-
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Started")${progress_msg_end}${build_result_pre}⌛ BUILD IN PROGRESS${build_result_post}" ;;
187+
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Started")${progress_msg_end}${build_result_pre}⌛ BUILD IN PROGRESS${build_result_post}${custom_comment}" ;;
181188
esac
182189
}
183190

assets/out

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payloa
4141

4242
path=$(jq -r '.params.path // ""' < "$payload")
4343
status=$(jq -r '.params.status // ""' < "$payload")
44+
additionnal_comment=$(jq -r '.params.comment // ""' < "$payload")
45+
additionnal_comment_file=$(jq -r '.params.commentFile // ""' < "$payload")
4446

4547
configure_git_ssl_verification "$skip_ssl_verification"
4648
configure_git_global "${git_config_payload}"
@@ -61,14 +63,20 @@ if [ -z "$status" ]; then
6163
fi
6264

6365
cd "$source"
66+
67+
if [ -n "$additionnal_comment_file" ]; then
68+
additionnal_comment="$(<${additionnal_comment_file})"
69+
fi
70+
6471
cd "$path"
6572

6673
merge_commit=$(git rev-parse HEAD)
6774
ls_remote=$(git ls-remote "$uri")
6875

69-
# collect prq id from git config stored in git config (during get step)
76+
# collect prq id and branch name from git config stored in git config (during get step)
7077
# included cat to catch error
7178
prq_number=$(git config --get pullrequest.id | cat)
79+
branch=$(git config --get pullrequest.branch | cat)
7280

7381
if [ -z "$prq_number" ]; then
7482
prqs=$(echo "$ls_remote" | grep -E "/pull\-requests/[0-9]+" | grep "$merge_commit" | cat)
@@ -143,8 +151,13 @@ bitbucket_pullrequest_commit_status "$repo_host" "$source_commit" "$data" "" ""
143151
# use the pullrequest date stored in git config in get
144152
prq_verify_date=$(git config --get pullrequest.date | cat)
145153

154+
# Add branch name to additional comment
155+
if [ -n "$additionnal_comment" ]; then
156+
additionnal_comment="${additionnal_comment//\[\[BRANCH\]\]/$branch}"
157+
fi
158+
146159
# add comment to pull request to track if build was started/finished
147-
comment_message=$(bitbucket_pullrequest_progress_comment "$status" "$prq_hash" "$source_commit" "$target_commit")
160+
comment_message=$(bitbucket_pullrequest_progress_comment "$status" "$prq_hash" "$source_commit" "$target_commit", "$additionnal_comment")
148161
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification" | jq -c '.[]')
149162
commented=""
150163
skip_verify=false

0 commit comments

Comments
 (0)