• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2
3set -xe
4
5OWNER=$1
6REPOSITORY=$2
7GITHUB_TOKEN=$3
8shift 3
9
10UPSTREAM=origin
11DEFAULT_BRANCH=master
12
13API_URL=https://api.github.com
14COMMIT_QUEUE_LABEL='commit-queue'
15COMMIT_QUEUE_FAILED_LABEL='commit-queue-failed'
16
17issueUrl() {
18  echo "$API_URL/repos/${OWNER}/${REPOSITORY}/issues/${1}"
19}
20
21labelsUrl() {
22  echo "$(issueUrl "${1}")/labels"
23}
24
25commentsUrl() {
26  echo "$(issueUrl "${1}")/comments"
27}
28
29gitHubCurl() {
30  url=$1
31  method=$2
32  shift 2
33
34  curl -fsL --request "$method" \
35       --url "$url" \
36       --header "authorization: Bearer ${GITHUB_TOKEN}" \
37       --header 'content-type: application/json' "$@"
38}
39
40commit_queue_failed() {
41  gitHubCurl "$(labelsUrl "${1}")" POST --data '{"labels": ["'"${COMMIT_QUEUE_FAILED_LABEL}"'"]}'
42
43  # shellcheck disable=SC2154
44  cqurl="${GITHUB_SERVER_URL}/${OWNER}/${REPOSITORY}/actions/runs/${GITHUB_RUN_ID}"
45  jq -n --arg content "<details><summary>Commit Queue failed</summary><pre>$(cat output)</pre><a href='$cqurl'>$cqurl</a></details>" '{body: $content}' > output.json
46  cat output.json
47
48  gitHubCurl "$(commentsUrl "${1}")" POST --data @output.json
49
50  rm output output.json
51}
52
53# TODO(mmarchini): should this be set with whoever added the label for each PR?
54git config --local user.email "github-bot@iojs.org"
55git config --local user.name "Node.js GitHub Bot"
56
57for pr in "$@"; do
58  # Skip PR if CI was requested
59  if gitHubCurl "$(labelsUrl "$pr")" GET | jq -e 'map(.name) | index("request-ci")'; then
60    echo "pr ${pr} skipped, waiting for CI to start"
61    continue
62  fi
63
64  # Skip PR if CI is still running
65  if ncu-ci url "https://github.com/${OWNER}/${REPOSITORY}/pull/${pr}" 2>&1 | grep "^Result *PENDING"; then
66    echo "pr ${pr} skipped, CI still running"
67    continue
68  fi
69
70  # Delete the commit queue label
71  gitHubCurl "$(labelsUrl "$pr")"/"$COMMIT_QUEUE_LABEL" DELETE
72
73  git node land --autorebase --yes "$pr" >output 2>&1 || echo "Failed to land #${pr}"
74  # cat here otherwise we'll be supressing the output of git node land
75  cat output
76
77  # TODO(mmarchini): workaround for ncu not returning the expected status code,
78  # if the "Landed in..." message was not on the output we assume land failed
79  if ! grep -q '. Post "Landed in .*/pull/'"${pr}" output; then
80    commit_queue_failed "$pr"
81    # If `git node land --abort` fails, we're in unknown state. Better to stop
82    # the script here, current PR was removed from the queue so it shouldn't
83    # interfere again in the future.
84    git node land --abort --yes
85    continue
86  fi
87
88  commits="$(git rev-parse $UPSTREAM/$DEFAULT_BRANCH)...$(git rev-parse HEAD)"
89
90  if ! git push $UPSTREAM $DEFAULT_BRANCH >> output 2>&1; then
91    commit_queue_failed "$pr"
92    continue
93  fi
94
95  rm output
96
97  gitHubCurl "$(commentsUrl "$pr")" POST --data '{"body": "Landed in '"$commits"'"}'
98
99  gitHubCurl "$(issueUrl "$pr")" PATCH --data '{"state": "closed"}'
100done
101