22# vim: set ft=sh
33
44set -e
5+ set -o pipefail
56
67exec 3>&1 # make stdout available as fd 3 for the result
78exec 1>&2 # redirect all output to stderr for logging
@@ -48,45 +49,27 @@ if [ -z "$uri" ]; then
4849 exit 1
4950fi
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-
6052versions=" []"
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
0 commit comments