Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BitBucket Cloud - Update all PRs for Concourse #54

Closed
wants to merge 9 commits into from
22 changes: 8 additions & 14 deletions assets/check
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash
# vim: set ft=sh

set -euo pipefail
set -euox pipefail

exec 3>&1
exec 1>&2
Expand Down Expand Up @@ -44,7 +44,7 @@ fi
if [[ "$bitbucket_type" == "server" ]]; then
request() {
uri="${base_url}/rest/api/1.0/projects/${project}/repos/${repository}${1}"
curl -sSL --fail -u "${username}:${password}" "$uri"
curl -sSL -u "${username}:${password}" "$uri"
}

if [[ -n "${source_branch}" ]]; then
Expand Down Expand Up @@ -82,7 +82,7 @@ if [[ "$bitbucket_type" == "server" ]]; then
for id in $(jq -r '.[].id' <<< "$prs"); do
uri="${base_url}/rest/api/1.0/projects/${project}/repos/${repository}/pull-requests/${id}/changes?limit=${changes_limit}"

changes=$(curl -sSL --fail -u "${username}:${password}" "${uri}" \
changes=$(curl -sSL -u "${username}:${password}" "${uri}" \
| jq --arg paths ^$paths '.values | map(.path.toString) | map(select(test($paths))) | any')
[[ $changes != false ]] && ids+="${id},"
done
Expand All @@ -109,14 +109,14 @@ elif [[ "$bitbucket_type" == "cloud" ]]; then
if [[ -n "${oauth_id}" ]]; then
oauth_response=$(mktemp /tmp/resource.XXXXXX)
uri="${base_url}/site/oauth2/access_token"
curl -XPOST -sSL --fail -u "${oauth_id}:${oauth_secret}" -d grant_type=client_credentials $uri | jq -r '.access_token' > "${oauth_response}"
curl -XPOST -sSL -u "${oauth_id}:${oauth_secret}" -d grant_type=client_credentials $uri | jq -r '.access_token' > "${oauth_response}"
authentication=(-H "Authorization: Bearer `cat $oauth_response`")
fi
uri="${base_url}/2.0/repositories/${project}/${repository}/pullrequests?limit=${limit}&state=OPEN"

# write response to file as feeding it to jq from a variable doesnt work properly: JSON looses linefeed format in variable
response=$(mktemp /tmp/resource.XXXXXX)
curl -sSL --fail "${authentication[@]}" $uri | jq -r ".values[0:$limit]" > "${response}"
curl -sSL "${authentication[@]}" $uri | jq -r ".values[0:$limit]" > "${response}"
if [[ "${direction}" == "incoming" ]]; then
branch_object="destination"
else
Expand All @@ -134,11 +134,7 @@ elif [[ "$bitbucket_type" == "cloud" ]]; then
commit=$(echo "$pullrequest" | jq -r '.source.commit.hash')
commit_url=$(echo "$pullrequest" | jq -r '.source.commit.links.self.href')

# get the commit date, which is when the PR last got updated code-wise.
# the updated_on field in the PR also changes when comment added etc
date=$(curl -sL --fail "${authentication[@]}" $commit_url | jq -r '.date')

pr=$(jq -n --arg id "${id}" --arg title "${title}" --arg branch "${branch}" --arg commit "${commit}" --arg date "${date}" '[{id: $id, title: $title, branch: $branch, commit: $commit, updated_at: $date}]')
pr=$(jq -n --arg id "${id}" --arg title "${title}" --arg branch "${branch}" --arg commit "${commit}" '[{id: $id, title: $title, branch: $branch, commit: $commit}]')
prs=$(jq -n --argjson prs "${prs}" --argjson pr "${pr}" '$prs + $pr')
done < <(jq -c '.[]' "${response}")

Expand All @@ -147,10 +143,8 @@ elif [[ "$bitbucket_type" == "cloud" ]]; then
exit
fi

# take the list of PRs | filter out containing "wip" in title | sort by update-date of commits | remove the date | pick latest PR, wrap as array for concourse
# take the list of PRs | filter out containing "wip" in title, wrap as array for concourse
jq -n --argjson prs "${prs}" '[ $prs
| map(select(if .title | test("wip"; "i") then false else true end))
| sort_by(.updated_at)
| map(del(.updated_at))
| .[-1] ]' >&3
| .[] ]' >&3
fi