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

tools: simplify and fix commit queue #40742

Merged
merged 4 commits into from Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/auto-start-ci.yml
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 }}
4 changes: 3 additions & 1 deletion .github/workflows/commit-queue.yml
Expand Up @@ -78,4 +78,6 @@ jobs:
ncu-config set owner "${OWNER}"

- 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) | .[]')
run: ./tools/actions/commit-queue.sh ${OWNER} ${REPOSITORY} $(echo '${{ steps.get_mergable_pull_requests.outputs.data }}' | jq '.repository.pullRequests.nodes | map(.number) | .[]')
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
targos marked this conversation as resolved.
Show resolved Hide resolved
58 changes: 18 additions & 40 deletions tools/actions/commit-queue.sh
Expand Up @@ -4,62 +4,39 @@ set -xe

OWNER=$1
REPOSITORY=$2
GITHUB_TOKEN=$3
shift 3
shift 2
aduh95 marked this conversation as resolved.
Show resolved Hide resolved

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
targos marked this conversation as resolved.
Show resolved Hide resolved
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
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