Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 28 additions & 45 deletions assets/check
Original file line number Diff line number Diff line change
Expand Up @@ -49,50 +49,28 @@ if [ -z "$uri" ]; then
exit 1
fi

# if option 'rebuild_when_target_changed' is enabled take merge branch since commit will always change for changes on target branch
prq_branch="from"
if [ "$rebuild_when_target_changed" == "true" ]; then
prq_branch="merge"
fi
versions="[]"

# collect all pull requests from uri
REMOTES=$(git ls-remote "$uri")
set +e
PULL_REQUESTS=$(echo "$REMOTES" | grep -E "/pull\\-requests/[0-9]+/${prq_branch}")
set -e
log "Calculating repository specifics"
# determine repository name for calling REST api
repo_name=$(basename "$uri" | sed "s/.git$//")
repo_project=$(basename $(dirname "$uri"))

versions="[]"
if [ -n "$PULL_REQUESTS" ]; then
log "Calculating repository specifics"
# determine repository name for calling REST api
repo_name=$(basename "$uri" | sed "s/.git$//")
repo_project=$(basename $(dirname "$uri"))

# parse uri and retrieve host
uri_parser "$uri"
repo_host="${uri_schema}://${uri_address}"

repo_host=${repo_host}$(getBasePathOfBitbucket)

versions="[]"
while read pull_request ; do
log "Verifying pull request"
# determine hash and prq number from grep
prq_number=$(echo "$pull_request" | sed -E "s/^.*\/pull-requests\/([0-9]+)\/.*$/\\1/")
prq_hash=$(echo "$pull_request" | awk '{print $1}')

# verify target branch of prq
prq=$(bitbucket_pullrequest "$repo_host" "$repo_project" "$repo_name" "$prq_number" "" "$skip_ssl_verification")

if [ "$prq" = "NO_SUCH_PULL_REQUEST" ]; then
continue
fi
# parse uri and retrieve host
uri_parser "$uri"
repo_host="${uri_schema}://${uri_address}"

repo_host=${repo_host}$(getBasePathOfBitbucket)

PULL_REQUEST_DATE=$(echo "$prq" | jq -r '.updatedDate')
# collect all pull requests from uri and loop on them
pull_requests=$(bitbucket_pullrequests "$repo_host" "$repo_project" "$repo_name" "" "$skip_ssl_verification" |
jq -r '.[] | [.id, .fromRef.latestCommit, .toRef.latestCommit, .toRef.displayId, .updatedDate]|@tsv')

if [ -n "$pull_requests" ]; then
while IFS=$'\t' read -r prq_number from_hash to_hash prq_to_branch PULL_REQUEST_DATE; do

log "Pull request #${prq_number}"

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

if [ "$only_when_mergeable" == "true" -o "$only_without_conflicts" == "true" ]; then
Expand All @@ -119,7 +97,11 @@ if [ -n "$PULL_REQUESTS" ]; then
text=$(echo "$comment" | jq -r '.text')

# check for progress or finished messages => do not include in versions when available
if bitbucket_pullrequest_comment_commit_match "$text" "$prq_hash"; then
hash="$from_hash"
if [ "$rebuild_when_target_changed" == "true" ]; then
hash="$to_hash"
fi
if bitbucket_pullrequest_comment_commit_match "$text" "$hash"; then
log "Skipping PRQ #$prq_number since already handled"
skip_build=true
break
Expand All @@ -133,16 +115,17 @@ if [ -n "$PULL_REQUESTS" ]; then
done <<< "$comments"
fi

if [ "$PULL_REQUEST_DATE" -lt "$CURRENT_VERSION_DATE" ]; then
continue
fi

# add prq to versions
if [ "$skip_build" == "false" ]; then
versions+=" + [{ id: \"$prq_number\", hash: \"$prq_hash\", date: \"$PULL_REQUEST_DATE\" }]"
if [ "$rebuild_when_target_changed" == "false" ]; then
to_hash=""
fi

versions+=" + [{ id: \"$prq_number\", from: \"$from_hash\", to: \"$to_hash\", date: \"$PULL_REQUEST_DATE\" }]"
fi

fi
done <<< "$PULL_REQUESTS"
done <<< "$pull_requests"
fi

# On the first request return only the current version.
Expand Down
10 changes: 10 additions & 0 deletions assets/helpers/bitbucket.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,16 @@ bitbucket_pullrequest() {
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests/$4" "" "" "" "$6" "$5"
}

bitbucket_pullrequests() {
# $1: host
# $2: project
# $3: repository id
# $4: netrc file (default: $HOME/.netrc)
# $5: skip ssl verification
log "Retrieving all open pull requests for $2/$3"
bitbucket_request "$1" "projects/$2/repos/$3/pull-requests" "" "" "" "$5" "$4"
}

bitbucket_pullrequest_merge() {
# $1: host
# $2: project
Expand Down
9 changes: 8 additions & 1 deletion assets/out
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,17 @@ if [ -z "$commented" ] && [ "$create_comments" == "true" ]; then
bitbucket_pullrequest_add_comment_status "$repo_host" "$repo_project" "$repo_name" "$prq_number" "$comment_message" "" "$skip_ssl_verification" >/dev/null
fi


to_hash=""
if [ "$rebuild_when_target_changed" == "true" ]; then
to_hash="$target_commit"
fi

jq -n "{
version: {
id: \"$prq_number\",
hash: \"$prq_hash\",
from: \"$source_commit\",
to: \"$to_hash\",
date: \"$PULL_REQUEST_DATE\"
},
metadata: $(pullrequest_metadata "$prq_number" "$uri" "$skip_ssl_verification")
Expand Down