|
| 1 | +#!/bin/bash |
| 2 | +# Credit to https://github.com/cirrus-actions/rebase for the base functionality here |
| 3 | + |
| 4 | +set -e |
| 5 | + |
| 6 | +if [ -z "$PR_NUMBER" ]; then |
| 7 | + PR_NUMBER=$(jq -r ".pull_request.number" "$GITHUB_EVENT_PATH") |
| 8 | + if [[ "$PR_NUMBER" == "null" ]]; then |
| 9 | + PR_NUMBER=$(jq -r ".issue.number" "$GITHUB_EVENT_PATH") |
| 10 | + fi |
| 11 | + if [[ "$PR_NUMBER" == "null" ]]; then |
| 12 | + echo "Failed to determine PR Number." |
| 13 | + exit 1 |
| 14 | + fi |
| 15 | +fi |
| 16 | + |
| 17 | +echo "Collecting information about PR #$PR_NUMBER of $GITHUB_REPOSITORY..." |
| 18 | + |
| 19 | +if [[ -z "$GITHUB_TOKEN" ]]; then |
| 20 | + echo "Set the GITHUB_TOKEN env variable." |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | + |
| 24 | +URI=https://api.github.com |
| 25 | +API_HEADER="Accept: application/vnd.github.v3+json" |
| 26 | +AUTH_HEADER="Authorization: token $GITHUB_TOKEN" |
| 27 | + |
| 28 | +MAX_RETRIES=${MAX_RETRIES:-6} |
| 29 | +RETRY_INTERVAL=${RETRY_INTERVAL:-10} |
| 30 | +REBASEABLE="" |
| 31 | +pr_resp="" |
| 32 | +for ((i = 0; i < "$MAX_RETRIES"; i++)); do |
| 33 | + pr_resp=$(curl -X GET -s -H "$AUTH_HEADER" -H "$API_HEADER" \ |
| 34 | + "$URI/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER") |
| 35 | + REBASEABLE=$(echo "$pr_resp" | jq -r .rebaseable) |
| 36 | + if [[ "$REBASEABLE" == "null" ]]; then |
| 37 | + echo "The PR is not ready to rebase, retry after $RETRY_INTERVAL seconds" |
| 38 | + sleep "$RETRY_INTERVAL" |
| 39 | + continue |
| 40 | + else |
| 41 | + break |
| 42 | + fi |
| 43 | +done |
| 44 | + |
| 45 | +# if [[ "$REBASEABLE" != "true" ]] ; then |
| 46 | +# echo "GitHub doesn't think that the PR is rebaseable!" |
| 47 | +# exit 1 |
| 48 | +# fi |
| 49 | + |
| 50 | +BASE_REPO=$(echo "$pr_resp" | jq -r .base.repo.full_name) |
| 51 | +BASE_BRANCH=$(echo "$pr_resp" | jq -r .base.ref) |
| 52 | + |
| 53 | +USER_LOGIN=$(jq -r ".comment.user.login" "$GITHUB_EVENT_PATH") |
| 54 | + |
| 55 | +if [[ "$USER_LOGIN" == "null" ]]; then |
| 56 | + USER_LOGIN=$(jq -r ".pull_request.user.login" "$GITHUB_EVENT_PATH") |
| 57 | +fi |
| 58 | + |
| 59 | +user_resp=$(curl -X GET -s -H "$AUTH_HEADER" -H "$API_HEADER" \ |
| 60 | + "$URI/users/$USER_LOGIN") |
| 61 | + |
| 62 | +USER_NAME=$(echo "$user_resp" | jq -r ".name") |
| 63 | +if [[ "$USER_NAME" == "null" ]]; then |
| 64 | + USER_NAME=$USER_LOGIN |
| 65 | +fi |
| 66 | +USER_NAME="$USER_NAME (Rebase PR Action)" |
| 67 | + |
| 68 | +USER_EMAIL=$(echo "$user_resp" | jq -r ".email") |
| 69 | +if [[ "$USER_EMAIL" == "null" ]]; then |
| 70 | + USER_EMAIL="$USER_LOGIN@users.noreply.github.com" |
| 71 | +fi |
| 72 | + |
| 73 | +if [[ -z "$BASE_BRANCH" ]]; then |
| 74 | + echo "Cannot get base branch information for PR #$PR_NUMBER!" |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +HEAD_REPO=$(echo "$pr_resp" | jq -r .head.repo.full_name) |
| 79 | +HEAD_BRANCH=$(echo "$pr_resp" | jq -r .head.ref) |
| 80 | + |
| 81 | +echo "Base branch for PR #$PR_NUMBER is $BASE_BRANCH" |
| 82 | + |
| 83 | +USER_TOKEN=${USER_LOGIN//-/_}_TOKEN |
| 84 | +UNTRIMMED_COMMITTER_TOKEN=${!USER_TOKEN:-$GITHUB_TOKEN} |
| 85 | +COMMITTER_TOKEN="$(echo -e "$UNTRIMMED_COMMITTER_TOKEN" | tr -d '[:space:]')" |
| 86 | + |
| 87 | +git remote set-url origin https://x-access-token:"$COMMITTER_TOKEN@github.com/$GITHUB_REPOSITORY".git |
| 88 | +git config --global user.email "$USER_EMAIL" |
| 89 | +git config --global user.name "$USER_NAME" |
| 90 | + |
| 91 | +git remote add fork https://x-access-token:"$COMMITTER_TOKEN@github.com/$HEAD_REPO".git |
| 92 | + |
| 93 | +set -o xtrace |
| 94 | + |
| 95 | +# make sure branches are up-to-date |
| 96 | +git fetch origin "$BASE_BRANCH" |
| 97 | +git fetch fork "$HEAD_BRANCH" |
| 98 | + |
| 99 | +# do the rebase |
| 100 | +git checkout -b fork/"$HEAD_BRANCH" fork/"$HEAD_BRANCH" |
| 101 | +git rebase origin/"$BASE_BRANCH" |
| 102 | + |
| 103 | +git submodule update --init --recursive third_party/zap/ |
| 104 | + |
| 105 | +cd third_party/zap/repo/ |
| 106 | +npm ci |
| 107 | +npm run version-stamp |
| 108 | +npm rebuild canvas --update-binary |
| 109 | +npm run build-spa |
| 110 | + |
| 111 | +cd ../../../ |
| 112 | + |
| 113 | +scripts/tools/zap_regen_all.py |
| 114 | + |
| 115 | +git status |
| 116 | + |
| 117 | +git add zzz_generated/* |
| 118 | +git add src/darwin/Framework/* |
| 119 | +git add src/controller/python/chip/clusters/* |
| 120 | +git add src/controller/java/zap-generated/* |
| 121 | + |
| 122 | +git commit -m "Regenerating ZAP" |
| 123 | + |
| 124 | +# push back |
| 125 | +git push --force-with-lease fork fork/"$HEAD_BRANCH:$HEAD_BRANCH" |
0 commit comments