Skip to content

Commit feefbbd

Browse files
Merge pull request #8 from gijsboer/continue-on-failed-retrieval-of-pr
Fix numerous things
2 parents 154c93b + f3b8fb8 commit feefbbd

File tree

7 files changed

+80
-33
lines changed

7 files changed

+80
-33
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ RUN apk --update add \
55
bash \
66
jq \
77
curl \
8-
git
8+
git \
9+
openssh-client
910

1011
# can't `git pull` unless we set these
1112
RUN git config --global user.email "git@localhost" && \

assets/check

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ cat > "$payload" <&0
2525

2626
log "Configuring git credentials"
2727
load_pubkey "$payload"
28-
configure_git_ssl_verification "$payload"
28+
2929
configure_credentials "$payload"
3030

3131
log "Parsing payload"
3232
uri=$(jq -r '.source.uri // ""' < "$payload")
33+
skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // false' < ${payload})
3334
git_config_payload=$(jq -r '.source.git_config // []' < "$payload")
3435

3536
only_for_branch=$(jq -r '.source.only_for_branch // "."' < "$payload")
@@ -39,6 +40,7 @@ only_when_asked=$(jq -r '.source.only_when_asked // "false"' < "$payload")
3940
rebuild_when_target_changed=$(jq -r '.source.rebuild_when_target_changed // "false"' < "$payload")
4041
rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payload")
4142

43+
configure_git_ssl_verification "$skip_ssl_verification"
4244
configure_git_global "${git_config_payload}"
4345

4446
if [ -z "$uri" ]; then
@@ -66,6 +68,8 @@ if [ -n "$pull_requests" ]; then
6668
uri_parser "$uri"
6769
repo_host="${uri_schema}://${uri_address}"
6870

71+
repo_host=${repo_host}$(getBasePathOfBitbucket)
72+
6973
versions="[]"
7074
while read pull_request ; do
7175
log "Verifying pull request"
@@ -74,12 +78,19 @@ if [ -n "$pull_requests" ]; then
7478
prq_hash=$(echo "$pull_request" | awk '{print $1}')
7579

7680
# verify target branch of prq
77-
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_number")
81+
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification")
82+
83+
if [ "$prq" = "ERROR" ]; then
84+
continue
85+
fi
86+
87+
log "Pull request #${prq_number}"
88+
7889
prq_to_branch=$(echo "$prq" | jq -r '.toRef.displayId')
7990
if [[ "$prq_to_branch" =~ $only_for_branch ]]; then
8091

8192
if [ "$only_when_mergeable" == "true" -o "$only_without_conflicts" == "true" ]; then
82-
prq_merge=$(bitbucket_pullrequest_merge "$repo_host" "$repo_project" "$repo_name" "$prq_number")
93+
prq_merge=$(bitbucket_pullrequest_merge "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification")
8394

8495
# verify if prq has merge conflicts
8596
conflicted=$(echo "$prq_merge" | jq -r '.conflicted')
@@ -93,7 +104,7 @@ if [ -n "$pull_requests" ]; then
93104
# edit timestamp to version to force new build when rebuild_phrase is included in comments
94105
prq_verify_date=$(echo "$prq" | jq -r '.createdDate')
95106
skip_build=false
96-
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" | jq -c '.[]')
107+
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification" | jq -c '.[]')
97108
if [ -n "$comments" ]; then
98109
while read -r comment; do
99110
text=$(echo "$comment" | jq -r '.text')

assets/helpers/bitbucket.sh

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ bitbucket_request() {
1313
# $3: query
1414
# $4: data
1515
# $5: url base path
16-
# $6: netrc file (default: $HOME/.netrc)
17-
# $7: HTTP method (default: POST for data, GET without data)
18-
# $8: recursive data for bitbucket paging
16+
# $6: Skip SSL verification
17+
# $7: netrc file (default: $HOME/.netrc)
18+
# $8: HTTP method (default: POST for data, GET without data)
19+
# $9: recursive data for bitbucket paging
1920

2021
local data="$4"
2122
local path=${5:-rest/api/1.0}
22-
local netrc_file=${6:-$HOME/.netrc}
23-
local method="$7"
24-
local recursive=${8:-limit=${VALUES_LIMIT}}
23+
local skip_ssl_verification=${6:-"false"}
24+
local netrc_file=${7:-$HOME/.netrc}
25+
local method="$8"
26+
local recursive=${9:-limit=${VALUES_LIMIT}}
2527

2628
local request_url="${1}/${path}/${2}?${recursive}&${3}"
2729
local request_result=$(tmp_file_unique bitbucket-request)
@@ -47,6 +49,10 @@ bitbucket_request() {
4749
extra_options+=" -X $method"
4850
fi
4951

52+
if [ "$skip_ssl_verification" = "true" ]; then
53+
extra_options+=" -k"
54+
fi
55+
5056
curl_cmd="curl -s --netrc-file \"$netrc_file\" $extra_options \"$request_url\" > \"$request_result\""
5157
if ! eval $curl_cmd; then
5258
log "Bitbucket request $request_url failed"
@@ -60,12 +66,15 @@ bitbucket_request() {
6066

6167
if [ "$(jq -r '.isLastPage' < "$request_result")" == "false" ]; then
6268
local nextPage=$(jq -r '.nextPageStart' < "$request_result")
63-
local nextResult=$(bitbucket_request "$1" "$2" "$3" "$4" "$5" "$6" "$7" "start=${nextPage}&limit=${VALUES_LIMIT}")
69+
local nextResult=$(bitbucket_request "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "start=${nextPage}&limit=${VALUES_LIMIT}")
6470
jq -c '.values' < "$request_result" | jq -c ". + $nextResult"
6571
elif [ "$(jq -c '.values' < "$request_result")" != "null" ]; then
6672
jq -c '.values' < "$request_result"
6773
elif [ "$(jq -c '.errors' < "$request_result")" == "null" ]; then
6874
jq '.' < "$request_result"
75+
elif [ "${request_result/NoSuchPullRequestException}" = "${request_result}" ]; then
76+
printf "ERROR"
77+
return
6978
else
7079
log "Bitbucket request ($request_url) failed: $(cat $request_result)"
7180
exit 1
@@ -81,8 +90,9 @@ bitbucket_pullrequest() {
8190
# $3: repository id
8291
# $4: pullrequest id
8392
# $5: netrc file (default: $HOME/.netrc)
93+
# $6: skip ssl verification
8494
log "Retrieving pull request #$4 for $2/$3"
85-
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4" "" "" "" "$5"
95+
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4" "" "" "" "$6" "$5"
8696
}
8797

8898
bitbucket_pullrequest_merge() {
@@ -91,8 +101,9 @@ bitbucket_pullrequest_merge() {
91101
# $3: repository id
92102
# $4: pullrequest id
93103
# $5: netrc file (default: $HOME/.netrc)
104+
# $6: skip ssl verification
94105
log "Retrieving pull request merge status #$4 for $2/$3"
95-
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/merge" "" "" "" "$5"
106+
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/merge" "" "" "" "$6" "$5"
96107
}
97108

98109
bitbucket_pullrequest_overview_comments() {
@@ -101,8 +112,10 @@ bitbucket_pullrequest_overview_comments() {
101112
# $3: repository id
102113
# $4: pullrequest id
103114
# $5: netrc file (default: $HOME/.netrc)
115+
# $6: skip ssl verification
116+
104117
log "Retrieving pull request comments #$4 for $2/$3"
105-
set -o pipefail; bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/activities" "" "" "" "$5" | \
118+
set -o pipefail; bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/activities" "" "" "" "$6" "$5" | \
106119
jq 'map(select(.action == "COMMENTED" and .commentAction == "ADDED" and .commentAnchor == null)) |
107120
sort_by(.createdDate) | reverse |
108121
map({ id: .comment.id, version: .comment.version, text: .comment.text, createdDate: .comment.createdDate })'
@@ -162,9 +175,10 @@ bitbucket_pullrequest_commit_status() {
162175
# $1: host
163176
# $2: commit
164177
# $3: data
165-
# $4: netrc file (default: $HOME/.netrc)
178+
# $5: netrc file (default: $HOME/.netrc)
179+
# $6: skip ssl verification
166180
log "Setting pull request status $2"
167-
bitbucket_request "$1" "commits/$2" "" "$3" "rest/build-status/1.0" "$5"
181+
bitbucket_request "$1" "commits/$2" "" "$3" "rest/build-status/1.0" "$6" "$5"
168182
}
169183

170184
bitbucket_pullrequest_add_comment_status() {
@@ -174,8 +188,9 @@ bitbucket_pullrequest_add_comment_status() {
174188
# $4: pullrequest id
175189
# $5: comment
176190
# $6: netrc file (default: $HOME/.netrc)
191+
# $7: skip ssl verification
177192
log "Adding pull request comment for status on #$4 for $2/$3"
178-
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/comments" "" "{\"text\": \"$5\" }" "" "$6"
193+
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/comments" "" "{\"text\": \"$5\" }" "" "$7" "$6"
179194
}
180195

181196
bitbucket_pullrequest_update_comment_status() {
@@ -187,6 +202,7 @@ bitbucket_pullrequest_update_comment_status() {
187202
# $6: comment id
188203
# $7: comment version
189204
# $8: netrc file (default: $HOME/.netrc)
205+
# $9: skip ssl verification
190206
log "Updating pull request comment (id: $6) for status on #$4 for $2/$3"
191-
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/comments/$6" "" "{\"text\": \"$5\", \"version\": \"$7\" }" "" "$8" "PUT"
207+
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4/comments/$6" "" "{\"text\": \"$5\", \"version\": \"$7\" }" "" "$9" "$8" "PUT"
192208
}

assets/helpers/git.sh

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ configure_git_global() {
2727
}
2828

2929
configure_git_ssl_verification() {
30-
skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // false' < $1)
31-
if [ "$skip_ssl_verification" = "true" ]; then
30+
if [ "$1" = "true" ]; then
3231
export GIT_SSL_NO_VERIFY=true
3332
fi
3433
}
3534

3635
add_pullrequest_metadata_basic() {
3736
# $1: pull request number
3837
# $2: pull request repository
38+
# $3: skip ssl verification
3939
local repo_name=$(basename "$2" | sed "s/.git$//")
4040
local repo_project=$(basename $(dirname "$2"))
4141

4242
# parse uri and retrieve host
4343
uri_parser "$2"
44-
local repo_host="${uri_schema}://${uri_address}"
44+
local repo_host="${uri_schema}://${uri_address}"$(getBasePathOfBitbucket)
4545

46-
local title=$(set -o pipefail; bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$1" | jq -r '.title')
46+
local title=$(set -o pipefail; bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$1" "" "$3" | jq -r '.title')
4747
local commit=$(git rev-parse HEAD)
4848
local author=$(git log -1 --format=format:%an)
4949

@@ -87,12 +87,13 @@ add_pullrequest_metadata_commit() {
8787
pullrequest_metadata() {
8888
# $1: pull request number
8989
# $2: pull request repository
90+
# $3: skip ssl verification
9091

9192
local source_commit=$(git rev-list --parents -1 $(git rev-parse HEAD) | awk '{print $3}')
9293
local target_commit=$(git rev-list --parents -1 $(git rev-parse HEAD) | awk '{print $2}')
9394

9495
jq -n "[]" | \
95-
add_pullrequest_metadata_basic "$1" "$2" | \
96+
add_pullrequest_metadata_basic "$1" "$2" "$3" | \
9697
add_pullrequest_metadata_commit "source" "$source_commit" | \
9798
add_pullrequest_metadata_commit "target" "$target_commit"
9899
}

assets/helpers/utils.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,19 @@ rawurlencode() {
146146
regex_escape() {
147147
echo "$1" | sed 's/[^^]/[&]/g; s/\^/\\^/g'
148148
}
149+
150+
getBasePathOfBitbucket() {
151+
# get base path in case bitbucket does not run on /
152+
153+
local base_path=""
154+
for i in "${!uri_parts[@]}"
155+
do
156+
if [ ${uri_parts[$i]} = "scm" ]; then
157+
break
158+
fi
159+
160+
base_path=$base_path"/"${uri_parts[$i]}
161+
done
162+
163+
echo ${base_path}
164+
}

assets/in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ payload=$(tmpfile request)
3131
cat > "$payload" <&0
3232

3333
load_pubkey "$payload"
34-
configure_git_ssl_verification "$payload"
3534
configure_credentials "$payload"
3635

36+
skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // false' < ${payload})
3737
uri=$(jq -r '.source.uri // ""' < "$payload")
3838
git_config_payload=$(jq -r '.source.git_config // []' < "$payload")
3939
commit_verification_key_ids=$(jq -r '(.source.commit_verification_key_ids // [])[]' < "$payload")
@@ -46,6 +46,7 @@ disable_git_lfs=$(jq -r '(.params.disable_git_lfs // false)' < "$payload")
4646

4747
prq_id=$(jq -r '.version.id // ""' < "$payload")
4848

49+
configure_git_ssl_verification "$skip_ssl_verification"
4950
configure_git_global "${git_config_payload}"
5051

5152
if [ -z "$uri" ]; then
@@ -138,5 +139,5 @@ git config --add pullrequest.merge $ref
138139

139140
jq -n "{
140141
version: $(jq '.version' < "$payload"),
141-
metadata: $(pullrequest_metadata "$prq_id" "$uri")
142+
metadata: $(pullrequest_metadata "$prq_id" "$uri" "$skip_ssl_verification")
142143
}" >&3

assets/out

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ payload=$(tmpfile request)
3131
cat > "$payload" <&0
3232

3333
load_pubkey "$payload"
34-
configure_git_ssl_verification "$payload"
3534
configure_credentials "$payload"
3635

36+
skip_ssl_verification=$(jq -r '.source.skip_ssl_verification // false' < ${payload})
3737
uri=$(jq -r '.source.uri // ""' < "$payload")
3838
git_config_payload=$(jq -r '.source.git_config // []' < "$payload")
3939
rebuild_when_target_changed=$(jq -r '.source.rebuild_when_target_changed // "false"' < "$payload")
@@ -42,6 +42,7 @@ rebuild_phrase=$(jq -r '.source.rebuild_phrase // "test this please"' < "$payloa
4242
path=$(jq -r '.params.path // ""' < "$payload")
4343
status=$(jq -r '.params.status // ""' < "$payload")
4444

45+
configure_git_ssl_verification "$skip_ssl_verification"
4546
configure_git_global "${git_config_payload}"
4647

4748
if [ -z "$uri" ]; then
@@ -120,7 +121,7 @@ repo_project=$(basename $(dirname "$uri"))
120121

121122
# parse uri and retrieve host
122123
uri_parser "$uri"
123-
repo_host="${uri_schema}://${uri_address}"
124+
repo_host="${uri_schema}://${uri_address}"$(getBasePathOfBitbucket)
124125

125126
# include ATC_EXTERNAL_URL in build status key, different sources should have different builds
126127
build_key="$BUILD_TEAM_NAME-$BUILD_PIPELINE_NAME-$BUILD_JOB_NAME-$ATC_EXTERNAL_URL"
@@ -137,14 +138,14 @@ data=$(jq -cn "{
137138
}")
138139

139140
# set commit build status for source commit
140-
bitbucket_pullrequest_commit_status "$repo_host" "$source_commit" "$data"
141+
bitbucket_pullrequest_commit_status "$repo_host" "$source_commit" "$data" "" "" "$skip_ssl_verification"
141142

142143
# use the current commit timestamp as date
143144
prq_verify_date=$(git log -1 --format=format:%at)
144145

145146
# add comment to pull request to track if build was started/finished
146147
comment_message=$(bitbucket_pullrequest_progress_comment "$status" "$prq_hash" "$source_commit" "$target_commit")
147-
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" | jq -c '.[]')
148+
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification" | jq -c '.[]')
148149
commented=""
149150
skip_verify=false
150151
if [ -n "$comments" ]; then
@@ -156,7 +157,7 @@ if [ -n "$comments" ]; then
156157
# check for progress messages => if pull request number matches then edit comment (instead of creating a new one)
157158
if [ -z "$commented" ]; then
158159
if bitbucket_pullrequest_progress_commit_match "$text" "$prq_hash" "Started"; then
159-
bitbucket_pullrequest_update_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "$id" "$version" >/dev/null
160+
bitbucket_pullrequest_update_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "$id" "$version" "" "$skip_ssl_verification" >/dev/null
160161
commented=true
161162
fi
162163
fi
@@ -176,7 +177,7 @@ if [ -n "$comments" ]; then
176177
fi
177178

178179
if [ -z "$commented" ]; then
179-
bitbucket_pullrequest_add_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" >/dev/null
180+
bitbucket_pullrequest_add_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "" "$skip_ssl_verification" >/dev/null
180181
fi
181182

182183
jq -n "{
@@ -185,5 +186,5 @@ jq -n "{
185186
hash: \"$prq_hash\",
186187
date: \"$(date_from_epoch_seconds "$prq_verify_date")\"
187188
},
188-
metadata: $(pullrequest_metadata "$prq_number" "$uri")
189+
metadata: $(pullrequest_metadata "$prq_number" "$uri" "$skip_ssl_verification")
189190
}" >&3

0 commit comments

Comments
 (0)