Skip to content

Commit 99a6be4

Browse files
Solve issue where PRQ id could not be calculated when target branch changed and PRQ is retrieved from cache
1 parent 995a006 commit 99a6be4

File tree

2 files changed

+129
-111
lines changed

2 files changed

+129
-111
lines changed

assets/in

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,22 @@ if [ -z "$uri" ]; then
5353
exit 1
5454
fi
5555

56-
depthflag=""
56+
if [ -z "$prq_id" ]; then
57+
log "invalid payload (missing pull request id):" "$(cat "$payload")"
58+
exit 1
59+
fi
60+
61+
depthflag="--depth 1"
5762
if test "$depth" -gt 0 2> /dev/null; then
5863
depthflag="--depth $depth"
5964
fi
6065

61-
mkdir -p "$destination"
62-
cd "$destination"
63-
6466
branch="pull-requests/${prq_id}/merge"
6567

66-
git init
67-
git remote add origin "$uri"
68+
log "Cloning $uri in $destination"
69+
git clone $depthflag "$uri" "$destination"
70+
cd "$destination"
71+
6872
git fetch $depthflag origin "+refs/${branch}:refs/remotes/origin/${branch}"
6973
git checkout "${branch}"
7074

@@ -112,6 +116,16 @@ if [ "$disable_git_lfs" != "true" ]; then
112116
git submodule foreach "git lfs fetch && git lfs checkout"
113117
fi
114118

119+
# calculate source and target commit
120+
source_commit=$(git rev-list --parents -1 $ref | awk '{print $3}')
121+
target_commit=$(git rev-list --parents -1 $ref | awk '{print $2}')
122+
123+
# expose configuration of pull request that can be used in container
124+
git config --add pullrequest.id $prq_id
125+
git config --add pullrequest.source_commit $source_commit
126+
git config --add pullrequest.target_commit $target_commit
127+
git config --add pullrequest.merge_commit $ref
128+
115129
jq -n "{
116130
version: $(jq '.version' < "$payload"),
117131
metadata: $(pullrequest_metadata "$prq_id" "$uri" )

assets/out

Lines changed: 109 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -62,119 +62,123 @@ fi
6262
cd "$source"
6363
cd "$path"
6464

65-
ref=$(git rev-parse HEAD)
65+
merge_commit=$(git rev-parse HEAD)
6666

67-
ls_remote=$(git ls-remote "$uri")
68-
prqs=$(echo "$ls_remote" | grep -E "/pull\-requests/[0-9]+" | grep "$ref" | cat)
67+
# collect prq id from git config stored in git config (during get step)
68+
prq_number=$(git config --get pullrequest.id)
6969

70-
# search for prq merge commits in git ls-remote that match current commit
71-
# if none match we can't return a version since we can't determine the correct prq
72-
prq_number=$(echo "$prqs" | sed -E "s/^.*\/pull-requests\/([0-9]+)\/.*$/\\1/")
70+
if [ -z "$prq_number" ]; then
71+
ls_remote=$(git ls-remote "$uri")
72+
prqs=$(echo "$ls_remote" | grep -E "/pull\-requests/[0-9]+" | grep "$merge_commit" | cat)
7373

74-
if [ -n "$prq_number" ]; then
74+
# search for prq from commits in git ls-remote that match current commit
75+
# if none match we can't return a version since we can't determine the correct prq
76+
prq_number=$(echo "$prqs" | sed -E "s/^.*\/pull-requests\/([0-9]+)\/.*$/\\1/")
77+
fi
78+
79+
if [ -z "$prq_number" ]; then
80+
log "Can't determine pull request id for commit $merge_commit"
81+
exit 1
82+
fi
7583

76-
# if option 'rebuild_when_target_changed' is enabled take merge branch since commit will always change for changes on target branch
77-
prq_branch="from"
78-
if [ "$rebuild_when_target_changed" == "true" ]; then
79-
prq_branch="merge"
80-
fi
84+
# if option 'rebuild_when_target_changed' is enabled take merge branch since commit will always change for changes on target branch
85+
prq_branch="from"
86+
if [ "$rebuild_when_target_changed" == "true" ]; then
87+
prq_branch="merge"
88+
fi
8189

82-
prq_hash=$(echo "$ls_remote" | grep -E "/pull\-requests/${prq_number}/${prq_branch}" | awk '{print $1}')
90+
prq_hash=$(echo "$ls_remote" | grep -E "/pull\-requests/${prq_number}/${prq_branch}" | awk '{print $1}')
8391

84-
if [ -z "$prq_hash" ]; then
85-
log "Failed to determine pull request hash from id $prq_number in \n$ls_remote"
92+
if [ -z "$prq_hash" ]; then
93+
log "Failed to determine pull request hash from id $prq_number in \n$ls_remote"
94+
exit 1
95+
fi
96+
97+
case "$status" in
98+
success)
99+
build_state="SUCCESSFUL"
100+
;;
101+
failure)
102+
build_state="FAILED"
103+
;;
104+
pending)
105+
build_state="INPROGRESS"
106+
;;
107+
*)
108+
log "status '$status' is not supported -- only success, failure or pending"
86109
exit 1
87-
fi
88-
89-
case "$status" in
90-
success)
91-
build_state="SUCCESSFUL"
92-
;;
93-
failure)
94-
build_state="FAILED"
95-
;;
96-
pending)
97-
build_state="INPROGRESS"
98-
;;
99-
*)
100-
log "status '$status' is not supported -- only success, failure or pending"
101-
exit 1
102-
esac
103-
104-
# set build status on commit in bitbucket, this has to be the latest source commit or the pull request won't pick it up
105-
source_commit=$(git rev-list --parents -1 $(git rev-parse HEAD) | awk '{print $3}')
106-
target_commit=$(git rev-list --parents -1 $(git rev-parse HEAD) | awk '{print $2}')
107-
108-
# determine repository name for calling REST api
109-
repo_name=$(basename "$uri" | sed "s/.git$//")
110-
repo_project=$(basename $(dirname "$uri"))
111-
112-
# parse uri and retrieve host
113-
uri_parser "$uri"
114-
repo_host="${uri_schema}://${uri_address}"
115-
116-
# include ATC_EXTERNAL_URL in build status key, different sources should have different builds
117-
build_key="$BUILD_TEAM_NAME-$BUILD_PIPELINE_NAME-$BUILD_JOB_NAME-$ATC_EXTERNAL_URL"
118-
build_name="Concourse $BUILD_TEAM_NAME: $BUILD_PIPELINE_NAME - $BUILD_JOB_NAME - #$BUILD_NAME"
119-
build_url="$ATC_EXTERNAL_URL/teams/$BUILD_TEAM_NAME/pipelines/$BUILD_PIPELINE_NAME/jobs/$BUILD_JOB_NAME/builds/$BUILD_NAME"
120-
build_description="Concourse build for PRQ #$prq_number @ $ATC_EXTERNAL_URL"
121-
122-
data=$(jq -cn "{
123-
state: \"$build_state\",
124-
key: \"$build_key\",
125-
name: \"$build_name\",
126-
url: \"$build_url\",
127-
description: \"$build_description\"
128-
}")
129-
130-
# set commit build status for source commit
131-
bitbucket_pullrequest_commit_status "$repo_host" "$source_commit" "$data"
132-
133-
# use the current commit timestamp as date
134-
prq_verify_date=$(git log -1 --format=format:%at)
135-
136-
# add comment to pull request to track if build was started/finished
137-
comment_message=$(bitbucket_pullrequest_progress_comment "$status" "$prq_hash" "$source_commit" "$target_commit")
138-
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" | jq -c '.[]')
139-
commented=""
140-
skip_verify=false
141-
if [ -n "$comments" ]; then
142-
while read comment; do
143-
id=$(echo "$comment" | jq -r '.id')
144-
text=$(echo "$comment" | jq -r '.text')
145-
version=$(echo "$comment" | jq -r '.version')
146-
147-
# check for progress messages => if pull request number matches then edit comment (instead of creating a new one)
148-
if [ -z "$commented" ]; then
149-
if bitbucket_pullrequest_progress_commit_match "$text" "$prq_hash" "Started"; then
150-
bitbucket_pullrequest_update_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "$id" "$version" >/dev/null
151-
commented=true
152-
fi
110+
esac
111+
112+
# set build status on commit in bitbucket, this has to be the latest source commit or the pull request won't pick it up
113+
source_commit=$(git rev-list --parents -1 $merge_commit | awk '{print $3}')
114+
target_commit=$(git rev-list --parents -1 $merge_commit | awk '{print $2}')
115+
116+
# determine repository name for calling REST api
117+
repo_name=$(basename "$uri" | sed "s/.git$//")
118+
repo_project=$(basename $(dirname "$uri"))
119+
120+
# parse uri and retrieve host
121+
uri_parser "$uri"
122+
repo_host="${uri_schema}://${uri_address}"
123+
124+
# include ATC_EXTERNAL_URL in build status key, different sources should have different builds
125+
build_key="$BUILD_TEAM_NAME-$BUILD_PIPELINE_NAME-$BUILD_JOB_NAME-$ATC_EXTERNAL_URL"
126+
build_name="Concourse $BUILD_TEAM_NAME: $BUILD_PIPELINE_NAME - $BUILD_JOB_NAME - #$BUILD_NAME"
127+
build_url="$ATC_EXTERNAL_URL/teams/$BUILD_TEAM_NAME/pipelines/$BUILD_PIPELINE_NAME/jobs/$BUILD_JOB_NAME/builds/$BUILD_NAME"
128+
build_description="Concourse build for PRQ #$prq_number @ $ATC_EXTERNAL_URL"
129+
130+
data=$(jq -cn "{
131+
state: \"$build_state\",
132+
key: \"$build_key\",
133+
name: \"$build_name\",
134+
url: \"$build_url\",
135+
description: \"$build_description\"
136+
}")
137+
138+
# set commit build status for source commit
139+
bitbucket_pullrequest_commit_status "$repo_host" "$source_commit" "$data"
140+
141+
# use the current commit timestamp as date
142+
prq_verify_date=$(git log -1 --format=format:%at)
143+
144+
# add comment to pull request to track if build was started/finished
145+
comment_message=$(bitbucket_pullrequest_progress_comment "$status" "$prq_hash" "$source_commit" "$target_commit")
146+
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" | jq -c '.[]')
147+
commented=""
148+
skip_verify=false
149+
if [ -n "$comments" ]; then
150+
while read comment; do
151+
id=$(echo "$comment" | jq -r '.id')
152+
text=$(echo "$comment" | jq -r '.text')
153+
version=$(echo "$comment" | jq -r '.version')
154+
155+
# check for progress messages => if pull request number matches then edit comment (instead of creating a new one)
156+
if [ -z "$commented" ]; then
157+
if bitbucket_pullrequest_progress_commit_match "$text" "$prq_hash" "Started"; then
158+
bitbucket_pullrequest_update_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "$id" "$version" >/dev/null
159+
commented=true
153160
fi
161+
fi
154162

155-
# edit timestamp to force new build when rebuild_phrase is included in comments
156-
if [ "$skip_verify" == "true" ]; then
157-
if echo "$text" | grep -Ec "$rebuild_phrase" > /dev/null; then
158-
prq_verify_date=$(( ($(echo "$comment" | jq -r '.createdDate') + 500) / 1000))
159-
skip_verify=true
160-
fi
163+
# edit timestamp to force new build when rebuild_phrase is included in comments
164+
if [ "$skip_verify" == "true" ]; then
165+
if echo "$text" | grep -Ec "$rebuild_phrase" > /dev/null; then
166+
prq_verify_date=$(( ($(echo "$comment" | jq -r '.createdDate') + 500) / 1000))
167+
skip_verify=true
161168
fi
162-
done <<< "$comments"
163-
fi
164-
165-
if [ -z "$commented" ]; then
166-
bitbucket_pullrequest_add_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" >/dev/null
167-
fi
168-
169-
jq -n "{
170-
version: {
171-
id: \"$prq_number\",
172-
hash: \"$prq_hash\",
173-
date: \"$(date_from_millis "$prq_verify_date")\"
174-
},
175-
metadata: $(pullrequest_metadata "$prq_number" "$uri" )
176-
}" >&3
177-
else
178-
log "Can't determine pull request id for commit $ref"
179-
jq -n "{}" >&3
169+
fi
170+
done <<< "$comments"
180171
fi
172+
173+
if [ -z "$commented" ]; then
174+
bitbucket_pullrequest_add_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" >/dev/null
175+
fi
176+
177+
jq -n "{
178+
version: {
179+
id: \"$prq_number\",
180+
hash: \"$prq_hash\",
181+
date: \"$(date_from_millis "$prq_verify_date")\"
182+
},
183+
metadata: $(pullrequest_metadata "$prq_number" "$uri" )
184+
}" >&3

0 commit comments

Comments
 (0)