|
62 | 62 | cd "$source"
|
63 | 63 | cd "$path"
|
64 | 64 |
|
65 |
| -ref=$(git rev-parse HEAD) |
| 65 | +merge_commit=$(git rev-parse HEAD) |
66 | 66 |
|
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) |
69 | 69 |
|
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) |
73 | 73 |
|
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 |
75 | 83 |
|
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 |
81 | 89 |
|
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}') |
83 | 91 |
|
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" |
86 | 109 | 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 |
153 | 160 | fi
|
| 161 | + fi |
154 | 162 |
|
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 |
161 | 168 | 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" |
180 | 171 | 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