Skip to content

Commit

Permalink
Working on auto-regeneration (#10936)
Browse files Browse the repository at this point in the history
Adding first shot at auto regeneration
  • Loading branch information
woody-apple authored and pull[bot] committed Nov 4, 2021
1 parent 8bfb83f commit 2652086
Show file tree
Hide file tree
Showing 2 changed files with 149 additions and 64 deletions.
88 changes: 24 additions & 64 deletions .github/workflows/zap_regeneration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
name: ZAP - Regenerate

on:
# push:
# pull_request:
workflow_dispatch:
issue_comment:
types: [created]

concurrency:
group: ${{ github.ref }}-${{ github.workflow }}-${{ (github.event_name == 'pull_request' && github.event.number) || (github.event_name == 'workflow_dispatch' && github.run_number) || github.sha }}
Expand All @@ -29,67 +29,27 @@ jobs:
timeout-minutes: 60

runs-on: ubuntu-18.04
if: github.actor != 'restyled-io[bot]'
if: (github.event.issue.pull_request != '' && contains(github.event.comment.body, '/regenerate') || (github.event_name == 'workflow_dispatch')

# container:
# image: connectedhomeip/chip-build:latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
# token: ${{ secrets.APPLE_PERSONAL_ACCESS_TOKEN }}
# ref: ${{ github.head_ref }}
submodules: true
- name: Use Node.js 14.x
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Use Java
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '11'
java-package: jre
- run: sudo apt-get update
- run: sudo apt-get install -fy --fix-missing libpixman-1-dev libcairo-dev libsdl-pango-dev libjpeg-dev libgif-dev python-autopep8
- name: Setup ZAP
timeout-minutes: 5
run: |
cd third_party/zap/repo/
npm ci
npm run version-stamp
npm rebuild canvas --update-binary
npm run build-spa
- name: Generate all
timeout-minutes: 5
run: scripts/tools/zap_regen_all.py
- name: Check for uncommited changes
run: git status
# git diff
# git add .
# git diff-index HEAD --
# git diff-index --quiet HEAD --
- name: Add generated paths
run: |
git add zzz_generated/*
git add src/darwin/Framework/*
git add src/controller/python/chip/clusters/*
git add src/controller/java/zap-generated/*
- uses: GuillaumeFalourd/git-commit-push@v1
with:
# email: ${{ github.actor }}@users.noreply.github.com
# name: ${{ github.actor }}
commit_message: Adding ZAP Generated Files
# target_branch: target_branch_name
# files: file1 file2 directory1 directory2/file3
# remote_repository: https://github.com/owner/another_repository
access_token: ${{ secrets.APPLE_PERSONAL_ACCESS_TOKEN }}
# force: true
# empty: true
# tags: true
# - name: Commit newly generated changes
# uses: stefanzweifel/git-auto-commit-action@v4
# with:
# commit_message: Auto-regenerated ZAP Changes
# file_pattern: "zzz_generated/* src/darwin/Framework/* src/controller/python/chip/clusters/* src/controller/java/zap-generated/*"
# # branch: ${{ github.head_ref }}
- name: Checkout
uses: actions/checkout@v2
with:
token: ${{ secrets.APPLE_PERSONAL_ACCESS_TOKEN }}
fetch-depth: 0 # otherwise, you will fail to push refs to dest repo
- name: Use Node.js 14.x
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Use Java
uses: actions/setup-java@v2
with:
distribution: 'zulu'
java-version: '11'
java-package: jre
- run: sudo apt-get update
- run: sudo apt-get install -fy --fix-missing libpixman-1-dev libcairo-dev libsdl-pango-dev libjpeg-dev libgif-dev python-autopep8
- name: Rebase and regenerate
run: scripts/helpers/rebase_and_regenerate_zap.sh
env:
GITHUB_TOKEN: ${{ secrets.APPLE_PERSONAL_ACCESS_TOKEN }}
125 changes: 125 additions & 0 deletions scripts/helpers/rebase_and_regenerate_zap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/bin/bash
# Credit to https://github.com/cirrus-actions/rebase for the base functionality here

set -e

if [ -z "$PR_NUMBER" ]; then
PR_NUMBER=$(jq -r ".pull_request.number" "$GITHUB_EVENT_PATH")
if [[ "$PR_NUMBER" == "null" ]]; then
PR_NUMBER=$(jq -r ".issue.number" "$GITHUB_EVENT_PATH")
fi
if [[ "$PR_NUMBER" == "null" ]]; then
echo "Failed to determine PR Number."
exit 1
fi
fi

echo "Collecting information about PR #$PR_NUMBER of $GITHUB_REPOSITORY..."

if [[ -z "$GITHUB_TOKEN" ]]; then
echo "Set the GITHUB_TOKEN env variable."
exit 1
fi

URI=https://api.github.com
API_HEADER="Accept: application/vnd.github.v3+json"
AUTH_HEADER="Authorization: token $GITHUB_TOKEN"

MAX_RETRIES=${MAX_RETRIES:-6}
RETRY_INTERVAL=${RETRY_INTERVAL:-10}
REBASEABLE=""
pr_resp=""
for ((i = 0; i < "$MAX_RETRIES"; i++)); do
pr_resp=$(curl -X GET -s -H "$AUTH_HEADER" -H "$API_HEADER" \
"$URI/repos/$GITHUB_REPOSITORY/pulls/$PR_NUMBER")
REBASEABLE=$(echo "$pr_resp" | jq -r .rebaseable)
if [[ "$REBASEABLE" == "null" ]]; then
echo "The PR is not ready to rebase, retry after $RETRY_INTERVAL seconds"
sleep "$RETRY_INTERVAL"
continue
else
break
fi
done

# if [[ "$REBASEABLE" != "true" ]] ; then
# echo "GitHub doesn't think that the PR is rebaseable!"
# exit 1
# fi

BASE_REPO=$(echo "$pr_resp" | jq -r .base.repo.full_name)
BASE_BRANCH=$(echo "$pr_resp" | jq -r .base.ref)

USER_LOGIN=$(jq -r ".comment.user.login" "$GITHUB_EVENT_PATH")

if [[ "$USER_LOGIN" == "null" ]]; then
USER_LOGIN=$(jq -r ".pull_request.user.login" "$GITHUB_EVENT_PATH")
fi

user_resp=$(curl -X GET -s -H "$AUTH_HEADER" -H "$API_HEADER" \
"$URI/users/$USER_LOGIN")

USER_NAME=$(echo "$user_resp" | jq -r ".name")
if [[ "$USER_NAME" == "null" ]]; then
USER_NAME=$USER_LOGIN
fi
USER_NAME="$USER_NAME (Rebase PR Action)"

USER_EMAIL=$(echo "$user_resp" | jq -r ".email")
if [[ "$USER_EMAIL" == "null" ]]; then
USER_EMAIL="$USER_LOGIN@users.noreply.github.com"
fi

if [[ -z "$BASE_BRANCH" ]]; then
echo "Cannot get base branch information for PR #$PR_NUMBER!"
exit 1
fi

HEAD_REPO=$(echo "$pr_resp" | jq -r .head.repo.full_name)
HEAD_BRANCH=$(echo "$pr_resp" | jq -r .head.ref)

echo "Base branch for PR #$PR_NUMBER is $BASE_BRANCH"

USER_TOKEN=${USER_LOGIN//-/_}_TOKEN
UNTRIMMED_COMMITTER_TOKEN=${!USER_TOKEN:-$GITHUB_TOKEN}
COMMITTER_TOKEN="$(echo -e "$UNTRIMMED_COMMITTER_TOKEN" | tr -d '[:space:]')"

git remote set-url origin https://x-access-token:"$COMMITTER_TOKEN@github.com/$GITHUB_REPOSITORY".git
git config --global user.email "$USER_EMAIL"
git config --global user.name "$USER_NAME"

git remote add fork https://x-access-token:"$COMMITTER_TOKEN@github.com/$HEAD_REPO".git

set -o xtrace

# make sure branches are up-to-date
git fetch origin "$BASE_BRANCH"
git fetch fork "$HEAD_BRANCH"

# do the rebase
git checkout -b fork/"$HEAD_BRANCH" fork/"$HEAD_BRANCH"
git rebase origin/"$BASE_BRANCH"

git submodule update --init --recursive third_party/zap/

cd third_party/zap/repo/
npm ci
npm run version-stamp
npm rebuild canvas --update-binary
npm run build-spa

cd ../../../

scripts/tools/zap_regen_all.py

git status

git add zzz_generated/*
git add src/darwin/Framework/*
git add src/controller/python/chip/clusters/*
git add src/controller/java/zap-generated/*

git commit -m "Regenerating ZAP"

# push back
git push --force-with-lease fork fork/"$HEAD_BRANCH:$HEAD_BRANCH"

0 comments on commit 2652086

Please sign in to comment.