Skip to content

Commit 80043ee

Browse files
author
Thomas Detoux
committed
Use an API call to check for new versions instead of git ls-remote
1 parent 7aae0e3 commit 80043ee

File tree

3 files changed

+45
-43
lines changed

3 files changed

+45
-43
lines changed

assets/check

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# vim: set ft=sh
33

44
set -e
5+
set -o pipefail
56

67
exec 3>&1 # make stdout available as fd 3 for the result
78
exec 1>&2 # redirect all output to stderr for logging
@@ -48,45 +49,27 @@ if [ -z "$uri" ]; then
4849
exit 1
4950
fi
5051

51-
# if option 'rebuild_when_target_changed' is enabled take merge branch since commit will always change for changes on target branch
52-
prq_branch="from"
53-
if [ "$rebuild_when_target_changed" == "true" ]; then
54-
prq_branch="merge"
55-
fi
56-
57-
# collect all pull requests from uri
58-
pull_requests=$(git ls-remote "$uri" | grep -E "/pull\-requests/[0-9]+/${prq_branch}" | cat)
59-
6052
versions="[]"
61-
if [ -n "$pull_requests" ]; then
62-
log "Calculating repository specifics"
63-
# determine repository name for calling REST api
64-
repo_name=$(basename "$uri" | sed "s/.git$//")
65-
repo_project=$(basename $(dirname "$uri"))
66-
67-
# parse uri and retrieve host
68-
uri_parser "$uri"
69-
repo_host="${uri_schema}://${uri_address}"
7053

71-
repo_host=${repo_host}$(getBasePathOfBitbucket)
54+
log "Calculating repository specifics"
55+
# determine repository name for calling REST api
56+
repo_name=$(basename "$uri" | sed "s/.git$//")
57+
repo_project=$(basename $(dirname "$uri"))
7258

73-
versions="[]"
74-
while read pull_request ; do
75-
log "Verifying pull request"
76-
# determine hash and prq number from grep
77-
prq_number=$(echo "$pull_request" | sed -E "s/^.*\/pull-requests\/([0-9]+)\/.*$/\\1/")
78-
prq_hash=$(echo "$pull_request" | awk '{print $1}')
59+
# parse uri and retrieve host
60+
uri_parser "$uri"
61+
repo_host="${uri_schema}://${uri_address}"
7962

80-
# verify target branch of prq
81-
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification")
63+
repo_host=${repo_host}$(getBasePathOfBitbucket)
8264

83-
if [ "$prq" = "ERROR" ]; then
84-
continue
85-
fi
65+
# collect all pull requests from uri and loop on them
66+
pull_requests=$(bitbucket_pullrequests "$repo_host" "$repo_project" "$repo_name" "" "$skip_ssl_verification" |
67+
jq -er '.[] | [.id, .fromRef.latestCommit, .toRef.latestCommit, .toRef.displayId, .createdDate]|@tsv')
8668

87-
log "Pull request #${prq_number}"
69+
if [ -n "$pull_requests" ]; then
70+
while IFS=$'\t' read -r prq_number from_hash to_hash prq_to_branch prq_verify_date; do
71+
log "Verifying pull request #${prq_number}"
8872

89-
prq_to_branch=$(echo "$prq" | jq -r '.toRef.displayId')
9073
if [[ "$prq_to_branch" =~ $only_for_branch ]]; then
9174

9275
if [ "$only_when_mergeable" == "true" -o "$only_without_conflicts" == "true" ]; then
@@ -102,15 +85,18 @@ if [ -n "$pull_requests" ]; then
10285
fi
10386

10487
# edit timestamp to version to force new build when rebuild_phrase is included in comments
105-
prq_verify_date=$(echo "$prq" | jq -r '.createdDate')
10688
skip_build=false
10789
comments=$(bitbucket_pullrequest_overview_comments "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification" | jq -c '.[]')
10890
if [ -n "$comments" ]; then
10991
while read -r comment; do
11092
text=$(echo "$comment" | jq -r '.text')
11193

11294
# check for progress or finished messages => do not include in versions when available
113-
if bitbucket_pullrequest_comment_commit_match "$text" "$prq_hash"; then
95+
hash="$from_hash"
96+
if [ "$rebuild_when_target_changed" == "true" ]; then
97+
hash="$to_hash"
98+
fi
99+
if bitbucket_pullrequest_comment_commit_match "$text" "$hash"; then
114100
log "Skipping PRQ #$prq_number since already handled"
115101
skip_build=true
116102
break
@@ -127,7 +113,12 @@ if [ -n "$pull_requests" ]; then
127113
# add prq to versions
128114
if [ "$skip_build" == "false" ]; then
129115
pretty_date=$(date_from_epoch_seconds "$(( ($prq_verify_date + 500) / 1000))")
130-
versions+=" + [{ id: \"$prq_number\", hash: \"$prq_hash\", date: \"$pretty_date\", change: $prq_verify_date }]"
116+
117+
if [ "$rebuild_when_target_changed" == "false" ]; then
118+
to_hash=""
119+
fi
120+
121+
versions+=" + [{ id: \"$prq_number\", from: \"$from_hash\", to: \"$to_hash\", date: \"$pretty_date\", change: $prq_verify_date }]"
131122
fi
132123

133124
fi

assets/helpers/bitbucket.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,16 @@ bitbucket_pullrequest() {
9595
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4" "" "" "" "$6" "$5"
9696
}
9797

98+
bitbucket_pullrequests() {
99+
# $1: host
100+
# $2: project
101+
# $3: repository id
102+
# $4: netrc file (default: $HOME/.netrc)
103+
# $5: skip ssl verification
104+
log "Retrieving all open pull requests for $2/$3"
105+
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests" "" "" "" "$5" "$4"
106+
}
107+
98108
bitbucket_pullrequest_merge() {
99109
# $1: host
100110
# $2: project

assets/out

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,7 @@ if [ -z "$prq_number" ]; then
9191
exit 1
9292
fi
9393

94-
# if option 'rebuild_when_target_changed' is enabled take merge branch since commit will always change for changes on target branch
95-
prq_branch="from"
96-
if [ "$rebuild_when_target_changed" == "true" ]; then
97-
prq_branch="merge"
98-
fi
99-
100-
prq_hash=$(echo "$ls_remote" | grep -E "/pull\-requests/${prq_number}/${prq_branch}" | awk '{print $1}')
94+
prq_hash=$(echo "$ls_remote" | grep -E "/pull\-requests/${prq_number}/from" | awk '{print $1}')
10195

10296
if [ -z "$prq_hash" ]; then
10397
log "Failed to determine pull request hash from id $prq_number in \n$ls_remote"
@@ -193,10 +187,17 @@ if [ -z "$commented" ]; then
193187
bitbucket_pullrequest_add_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "" "$skip_ssl_verification" >/dev/null
194188
fi
195189

190+
191+
to_hash=""
192+
if [ "$rebuild_when_target_changed" == "true" ]; then
193+
to_hash="$target_commit"
194+
fi
195+
196196
jq -n "{
197197
version: {
198198
id: \"$prq_number\",
199-
hash: \"$prq_hash\",
199+
from: \"$source_commit\",
200+
to: \"$to_hash\",
200201
date: \"$prq_verify_date\"
201202
},
202203
metadata: $(pullrequest_metadata "$prq_number" "$uri" "$skip_ssl_verification")

0 commit comments

Comments
 (0)