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