2020-11-11 00:26:07 +01:00
|
|
|
#!/bin/sh
|
2020-06-28 19:40:51 -07:00
|
|
|
|
|
|
|
set -xe
|
|
|
|
|
|
|
|
OWNER=$1
|
|
|
|
REPOSITORY=$2
|
|
|
|
GITHUB_TOKEN=$3
|
|
|
|
shift 3
|
|
|
|
|
2020-09-16 10:51:30 +03:00
|
|
|
UPSTREAM=origin
|
|
|
|
DEFAULT_BRANCH=master
|
|
|
|
|
2020-06-28 19:40:51 -07:00
|
|
|
API_URL=https://api.github.com
|
|
|
|
COMMIT_QUEUE_LABEL='commit-queue'
|
|
|
|
COMMIT_QUEUE_FAILED_LABEL='commit-queue-failed'
|
|
|
|
|
2020-11-11 00:26:07 +01:00
|
|
|
issueUrl() {
|
2020-06-28 19:40:51 -07:00
|
|
|
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}"
|
|
|
|
}
|
|
|
|
|
2020-11-11 00:26:07 +01:00
|
|
|
labelsUrl() {
|
2020-06-28 19:40:51 -07:00
|
|
|
echo "$(issueUrl "${1}")/labels"
|
|
|
|
}
|
|
|
|
|
2020-11-11 00:26:07 +01:00
|
|
|
commentsUrl() {
|
2020-06-28 19:40:51 -07:00
|
|
|
echo "$(issueUrl "${1}")/comments"
|
|
|
|
}
|
|
|
|
|
2020-11-11 00:26:07 +01:00
|
|
|
gitHubCurl() {
|
2020-06-28 19:40:51 -07:00
|
|
|
url=$1
|
|
|
|
method=$2
|
|
|
|
shift 2
|
|
|
|
|
|
|
|
curl -fsL --request "$method" \
|
|
|
|
--url "$url" \
|
|
|
|
--header "authorization: Bearer ${GITHUB_TOKEN}" \
|
|
|
|
--header 'content-type: application/json' "$@"
|
|
|
|
}
|
|
|
|
|
2021-01-10 10:04:05 +01:00
|
|
|
commit_queue_failed() {
|
|
|
|
gitHubCurl "$(labelsUrl "${1}")" POST --data '{"labels": ["'"${COMMIT_QUEUE_FAILED_LABEL}"'"]}'
|
|
|
|
|
|
|
|
# shellcheck disable=SC2154
|
|
|
|
cqurl="${GITHUB_SERVER_URL}/${OWNER}/${REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
|
|
|
|
jq -n --arg content "<details><summary>Commit Queue failed</summary><pre>$(cat output)</pre><a href='$cqurl'>$cqurl</a></details>" '{body: $content}' > output.json
|
|
|
|
cat output.json
|
|
|
|
|
|
|
|
gitHubCurl "$(commentsUrl "${1}")" POST --data @output.json
|
|
|
|
|
|
|
|
rm output output.json
|
|
|
|
}
|
2020-06-28 19:40:51 -07:00
|
|
|
|
|
|
|
# TODO(mmarchini): should this be set with whoever added the label for each PR?
|
|
|
|
git config --local user.email "github-bot@iojs.org"
|
|
|
|
git config --local user.name "Node.js GitHub Bot"
|
|
|
|
|
|
|
|
for pr in "$@"; do
|
|
|
|
# Skip PR if CI was requested
|
|
|
|
if gitHubCurl "$(labelsUrl "$pr")" GET | jq -e 'map(.name) | index("request-ci")'; then
|
|
|
|
echo "pr ${pr} skipped, waiting for CI to start"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Skip PR if CI is still running
|
|
|
|
if ncu-ci url "https://github.com/${OWNER}/${REPOSITORY}/pull/${pr}" 2>&1 | grep "^Result *PENDING"; then
|
|
|
|
echo "pr ${pr} skipped, CI still running"
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Delete the commit queue label
|
|
|
|
gitHubCurl "$(labelsUrl "$pr")"/"$COMMIT_QUEUE_LABEL" DELETE
|
|
|
|
|
2020-08-29 12:38:56 +03:00
|
|
|
git node land --autorebase --yes "$pr" >output 2>&1 || echo "Failed to land #${pr}"
|
2020-06-28 19:40:51 -07:00
|
|
|
# cat here otherwise we'll be supressing the output of git node land
|
|
|
|
cat output
|
|
|
|
|
|
|
|
# TODO(mmarchini): workaround for ncu not returning the expected status code,
|
|
|
|
# if the "Landed in..." message was not on the output we assume land failed
|
2021-01-10 10:04:05 +01:00
|
|
|
if ! grep -q '. Post "Landed in .*/pull/'"${pr}" output; then
|
|
|
|
commit_queue_failed "$pr"
|
2020-06-28 19:40:51 -07:00
|
|
|
# If `git node land --abort` fails, we're in unknown state. Better to stop
|
|
|
|
# the script here, current PR was removed from the queue so it shouldn't
|
2021-06-10 18:24:30 -04:00
|
|
|
# interfere again in the future.
|
2020-06-28 19:40:51 -07:00
|
|
|
git node land --abort --yes
|
2021-01-10 10:04:05 +01:00
|
|
|
continue
|
|
|
|
fi
|
|
|
|
|
|
|
|
commits="$(git rev-parse $UPSTREAM/$DEFAULT_BRANCH)...$(git rev-parse HEAD)"
|
2020-06-28 19:40:51 -07:00
|
|
|
|
2021-01-10 10:04:05 +01:00
|
|
|
if ! git push $UPSTREAM $DEFAULT_BRANCH >> output 2>&1; then
|
|
|
|
commit_queue_failed "$pr"
|
|
|
|
continue
|
|
|
|
fi
|
2020-09-16 10:51:30 +03:00
|
|
|
|
2021-01-10 10:04:05 +01:00
|
|
|
rm output
|
2020-09-16 10:51:30 +03:00
|
|
|
|
2021-01-10 10:04:05 +01:00
|
|
|
gitHubCurl "$(commentsUrl "$pr")" POST --data '{"body": "Landed in '"$commits"'"}'
|
2020-06-28 19:40:51 -07:00
|
|
|
|
2021-01-10 10:04:05 +01:00
|
|
|
gitHubCurl "$(issueUrl "$pr")" PATCH --data '{"state": "closed"}'
|
2020-06-28 19:40:51 -07:00
|
|
|
done
|