Skip to content

Commit 2243b68

Browse files
Fix pull request comment match so finished comments overwrite progress comments
1 parent 762015a commit 2243b68

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

assets/helpers/bitbucket.sh

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,41 +108,53 @@ bitbucket_pullrequest_overview_comments() {
108108
map({ id: .comment.id, version: .comment.version, text: .comment.text, createdDate: .comment.createdDate })'
109109
}
110110

111+
bitbucket_pullrequest_progress_msg_start() {
112+
# $1: pull request hash
113+
# $2: type
114+
local hash="$1"
115+
local type="$2"
116+
117+
local build_url_job="$ATC_EXTERNAL_URL/teams/$(rawurlencode "$BUILD_TEAM_NAME")/pipelines/$(rawurlencode "$BUILD_PIPELINE_NAME")/jobs/$(rawurlencode "$BUILD_JOB_NAME")"
118+
echo "[*Build$type* at **[${BUILD_PIPELINE_NAME} > ${BUILD_JOB_NAME}]($build_url_job)** for $hash"
119+
}
120+
111121
bitbucket_pullrequest_progress_commit_match() {
112122
# $1: pull request comment
113123
# $2: pull request hash
114-
# $3: type of build to match (default: Started|Finished)
124+
# $3: type of build to match
115125
local comment="$1"
116126
local hash="$2"
117-
local type=${3:-(Started|Finished)}
118-
local build_url_job="$ATC_EXTERNAL_URL/teams/$(rawurlencode "$BUILD_TEAM_NAME")/pipelines/$(rawurlencode "$BUILD_PIPELINE_NAME")/jobs/$(rawurlencode "$BUILD_JOB_NAME")"
119-
echo "$comment" | grep -Ec "^\[\*Build$type\* at \*\*\[${BUILD_PIPELINE_NAME} \> ${BUILD_JOB_NAME}\]\($build_url_job\)\*\* for $hash" > /dev/null
127+
local type="$3"
128+
129+
local msg=$(bitbucket_pullrequest_progress_msg_start "$hash" "$type")
130+
echo "$comment" | grep -Ec "^$(regex_escape "$msg")" > /dev/null
120131
}
121132

122133
bitbucket_pullrequest_progress_comment() {
123134
# $1: status (success, failure or pending)
124135
# $2: hash of merge commit
125136
# $3: hash of source commit
126137
# $4: hash of target commit
127-
local build_url_job="$ATC_EXTERNAL_URL/teams/$(rawurlencode "$BUILD_TEAM_NAME")/pipelines/$(rawurlencode "$BUILD_PIPELINE_NAME")/jobs/$(rawurlencode "$BUILD_JOB_NAME")"
128-
local build_url="${build_url_job}/builds/$(rawurlencode "$BUILD_NAME")"
129-
local build_status_pre="[*Build"
130-
local build_status_post="* at **[${BUILD_PIPELINE_NAME} > ${BUILD_JOB_NAME}]($build_url_job)** for $2"
131-
if [ "$2" == "$3" ]; then
132-
build_status_post+=" into $4]"
138+
local hash="$2"
139+
140+
local progress_msg_end=""
141+
if [ "$hash" == "$3" ]; then
142+
progress_msg_end+=" into $4]"
133143
else
134-
build_status_post="] $3 into $4"
144+
progress_msg_end="] $3 into $4"
135145
fi
146+
147+
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")"
136148
local build_result_pre=" \n\n **["
137149
local build_result_post="]($build_url)** - Build #$BUILD_NAME"
138150

139151
case "$1" in
140152
success)
141-
echo "${build_status_pre}Finished${build_status_post}${build_result_pre}✓ BUILD SUCCESS${build_result_post}" ;;
153+
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✓ BUILD SUCCESS${build_result_post}" ;;
142154
failure)
143-
echo "${build_status_pre}Finished${build_status_post}${build_result_pre}✕ BUILD FAILED${build_result_post}" ;;
155+
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Finished")${progress_msg_end}${build_result_pre}✕ BUILD FAILED${build_result_post}" ;;
144156
pending)
145-
echo "${build_status_pre}Started${build_status_post}${build_result_pre}⌛ BUILD IN PROGRESS${build_result_post}" ;;
157+
echo "$(bitbucket_pullrequest_progress_msg_start "$hash" "Started")${progress_msg_end}${build_result_pre}⌛ BUILD IN PROGRESS${build_result_post}" ;;
146158
esac
147159
}
148160

assets/helpers/utils.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,7 @@ rawurlencode() {
142142

143143
echo "${encoded}"
144144
}
145+
146+
regex_escape() {
147+
echo "$1" | sed 's/[^^]/[&]/g; s/\^/\\^/g'
148+
}

0 commit comments

Comments
 (0)