Skip to content

Commit

Permalink
tools: use gh CLI for CI and commit queue jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
targos committed Nov 6, 2021
1 parent 80b8440 commit e4c662b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 77 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/auto-start-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ jobs:
ncu-config set repo ${{ env.REPOSITORY }}
- name: Start CI
run: ./tools/actions/start-ci.sh ${{ secrets.GITHUB_TOKEN }} ${{ env.OWNER }} ${{ env.REPOSITORY }} $(echo '${{ steps.get_prs_for_ci.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]')
run: ./tools/actions/start-ci.sh $(echo '${{ steps.get_prs_for_ci.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .github/workflows/commit-queue.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,5 @@ jobs:
- name: Start the commit queue
run: ./tools/actions/commit-queue.sh ${OWNER} ${REPOSITORY} ${{ secrets.GITHUB_TOKEN }} $(echo '${{ steps.get_mergable_pull_requests.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]')
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
58 changes: 18 additions & 40 deletions tools/actions/commit-queue.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,39 @@ set -xe

OWNER=$1
REPOSITORY=$2
GITHUB_TOKEN=$3
shift 3
shift 2

UPSTREAM=origin
DEFAULT_BRANCH=master

API_URL=https://api.github.com
COMMIT_QUEUE_LABEL='commit-queue'
COMMIT_QUEUE_FAILED_LABEL='commit-queue-failed'

issueUrl() {
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}"
}
COMMIT_QUEUE_LABEL="commit-queue"
COMMIT_QUEUE_FAILED_LABEL="commit-queue-failed"

mergeUrl() {
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/pulls/${1}/merge"
}

labelsUrl() {
echo "$(issueUrl "${1}")/labels"
}

commentsUrl() {
echo "$(issueUrl "${1}")/comments"
}

gitHubCurl() {
url=$1
method=$2
shift 2

curl -fsL --request "$method" \
--url "$url" \
--header "authorization: Bearer ${GITHUB_TOKEN}" \
--header 'content-type: application/json' "$@"
echo "repos/${OWNER}/${REPOSITORY}/pulls/${1}/merge"
}

commit_queue_failed() {
gitHubCurl "$(labelsUrl "${1}")" POST --data '{"labels": ["'"${COMMIT_QUEUE_FAILED_LABEL}"'"]}'
pr=$1

gh pr edit "$pr" --add-label "${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
body="<details><summary>Commit Queue failed</summary><pre>$(cat output)</pre><a href='$cqurl'>$cqurl</a></details>"
echo "$body"

gitHubCurl "$(commentsUrl "${1}")" POST --data @output.json
gh pr comment "$pr" --body "$body"

rm output output.json
rm output
}

# 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
gitHubCurl "$(labelsUrl "$pr")" GET > labels.json
gh pr view "$pr" --json labels --jq ".labels" > labels.json
# Skip PR if CI was requested
if jq -e 'map(.name) | index("request-ci")' < labels.json; then
echo "pr ${pr} skipped, waiting for CI to start"
Expand All @@ -73,7 +50,7 @@ for pr in "$@"; do
fi

# Delete the commit queue label
gitHubCurl "$(labelsUrl "$pr")"/"$COMMIT_QUEUE_LABEL" DELETE
gh pr edit "$pr" --remove-label "$COMMIT_QUEUE_LABEL"

if jq -e 'map(.name) | index("commit-queue-squash")' < labels.json; then
MULTIPLE_COMMIT_POLICY="--fixupAll"
Expand Down Expand Up @@ -106,13 +83,14 @@ for pr in "$@"; do
continue
fi
else
# If there's only one commit, we can use the Squash and Merge feature from GitHub
# If there's only one commit, we can use the Squash and Merge feature from GitHub.
# TODO: use `gh pr merge` when the GitHub CLI allows to customize the commit title (https://github.com/cli/cli/issues/1023).
jq -n \
--arg title "$(git log -1 --pretty='format:%s')" \
--arg body "$(git log -1 --pretty='format:%b')" \
'{merge_method:"squash",commit_title:$title,commit_message:$body}' > output.json
cat output.json
gitHubCurl "$(mergeUrl "$pr")" PUT --data @output.json > output
gh api -X PUT "$(mergeUrl "$pr")" --input output.json > output
cat output
if ! commits="$(jq -r 'if .merged then .sha else error("not merged") end' < output)"; then
commit_queue_failed "$pr"
Expand All @@ -123,9 +101,9 @@ for pr in "$@"; do

rm output

gitHubCurl "$(commentsUrl "$pr")" POST --data '{"body": "Landed in '"$commits"'"}'
gh pr comment "$pr" --body "Landed in $commits"

[ -z "$MULTIPLE_COMMIT_POLICY" ] && gitHubCurl "$(issueUrl "$pr")" PATCH --data '{"state": "closed"}'
[ -z "$MULTIPLE_COMMIT_POLICY" ] && gh pr close "$pr"
done

rm -f labels.json
44 changes: 8 additions & 36 deletions tools/actions/start-ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,11 @@

set -xe

GITHUB_TOKEN=$1
OWNER=$2
REPOSITORY=$3
API_URL=https://api.github.com
REQUEST_CI_LABEL='request-ci'
REQUEST_CI_FAILED_LABEL='request-ci-failed'
shift 3

issueUrl() {
echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}"
}

labelsUrl() {
echo "$(issueUrl "${1}")/labels"
}

commentsUrl() {
echo "$(issueUrl "${1}")/comments"
}
REQUEST_CI_LABEL="request-ci"
REQUEST_CI_FAILED_LABEL="request-ci-failed"

for pr in "$@"; do
curl -sL --request DELETE \
--url "$(labelsUrl "$pr")"/"$REQUEST_CI_LABEL" \
--header "authorization: Bearer ${GITHUB_TOKEN}" \
--header 'content-type: application/json'
gh pr edit "$pr" --remove-label "$REQUEST_CI_LABEL"

ci_started=yes
rm -f output;
Expand All @@ -35,19 +15,11 @@ for pr in "$@"; do

if [ "$ci_started" = "no" ]; then
# Do we need to reset?
curl -sL --request PUT \
--url "$(labelsUrl "$pr")" \
--header "authorization: Bearer ${GITHUB_TOKEN}" \
--header 'content-type: application/json' \
--data '{"labels": ["'"${REQUEST_CI_FAILED_LABEL}"'"]}'

jq -n --arg content "<details><summary>Couldn't start CI</summary><pre>$(cat output)</pre></details>" '{body: $content}' > output.json

curl -sL --request POST \
--url "$(commentsUrl "$pr")" \
--header "authorization: Bearer ${GITHUB_TOKEN}" \
--header 'content-type: application/json' \
--data @output.json
gh pr edit "$pr" --add-label "$REQUEST_CI_FAILED_LABEL"

jq -n --arg content "<details><summary>Couldn't start CI</summary><pre>$(cat output)</pre></details>" > output.json

gh pr comment "$pr" --body-file output.json

rm output.json;
fi
Expand Down

0 comments on commit e4c662b

Please sign in to comment.