Unverified Commit 5f83efa9 authored by Russell Cohen's avatar Russell Cohen Committed by GitHub
Browse files

GitHub action to show generated diff preview (#778)



* Add `mk-generated.sh`

`tools/mk-generated.sh` will run code generation and create an orphan branch containing the codegen results.

* Add GitHub action to push the generated code to a branch

* Add cleanup job

* backout accidental changes

* Apply suggestions from code review

Co-authored-by: default avatarZelda Hessler <zelda.hessler@pm.me>

Co-authored-by: default avatarZelda Hessler <zelda.hessler@pm.me>
parent 2e7cb459
Loading
Loading
Loading
Loading
+82 −0
Original line number Diff line number Diff line
name: codegen diff preview
# This job will generate a branch containing exclusively codegen output and push it to GitHub
# once the branch is deployed, it will comment on GitHub with a link where you can see the generated diff.
on:
  push:
    branches:
    # this is a load-bearing branch filter: if this isn't here, you may
    # end up generating diffs for __generated-* branches which would lead to infinite recursion...
    - main
  pull_request:
    types:
    - opened
    - reopened
    - closed
    - synchronize
env:
  java_version: 11
jobs:
  cleanup-branch:
    if: ${{ github.event.action == 'closed' }}
    runs-on: ubuntu-latest
    name: cleanup generated code branch
    steps:
    - name: gen branch output
      run: echo "::set-output name=branchname::${GITHUB_HEAD_REF##*/}"
      id: branch_output
    - uses: actions/github-script@v5
      with:
        script: |
          console.log("deleting the generated code branch");
          await github.rest.git.deleteRef({
            owner: context.repo.owner,
            repo: context.repo.repo,
            ref: "heads/__generated-${{ steps.branch_output.outputs.branchname }}"
          })

  push-generated-code:
    runs-on: ubuntu-latest
    name: Push generated code to a branch
    if: ${{ github.event.action != 'closed' }}
    steps:
    # this is not technically necessary because of the branch filter above, but better to check
    # twice than have an infinitely recursing PR job
    - name: Assert we aren't already on a generated branch
      run: |
        [[ ${GITHUB_HEAD_REF:-$GITHUB_REF} != "*__generated*" ]]
    - uses: actions/checkout@v2
    - uses: actions/cache@v2
      name: Gradle Cache
      with:
        path: |
          ~/.gradle/caches
          ~/.gradle/wrapper
        key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
        restore-keys: |
          ${{ runner.os }}-gradle-
    - name: Set up JDK
      uses: actions/setup-java@v1
      with:
        java-version: ${{ env.java_version }}
    - name: mk-generated
      run: ./tools/mk-generated.sh
    - name: push generated branch
      run: |
        git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
        git push -f origin "$(git rev-parse --abbrev-ref HEAD)"
    - name: finalize
      run: echo "generated output pushed to $(git rev-parse --abbrev-ref HEAD)"
    - name: gen branch output
      run: echo "::set-output name=branchname::$(git rev-parse --abbrev-ref HEAD)"
      id: branch_output
    - uses: actions/github-script@v5
      # NOTE: if comments on each commit become bothersome, add a check that github.event.pull_request.action == "opened"
      if: ${{ env.GITHUB_HEAD_REF }} != null
      with:
        script: |
          await github.rest.issues.createComment({
            issue_number: context.issue.number,
            owner: context.repo.owner,
            repo: context.repo.repo,
            body: `A new generated diff is ready to view: https://github.com/${context.repo.owner}/${context.repo.repo}/compare/__generated-main...${{ steps.branch_output.outputs.branchname }}`
          })

tools/mk-generated.sh

0 → 100755
+33 −0
Original line number Diff line number Diff line
#!/bin/bash
#
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.
#

# Create a branch containing exclusively generated code
#
# This script creates a new branch `__generated-$current_branch` that contains the results of the smoke-test codegen
# This script outputs the name of the new branch to stdout

set -e

# Redirect stdout to stderr for all the code in `{ .. }`
{
	git diff --quiet || (echo 'working tree not clean, aborting' && exit 1)
	gh_branch=${GITHUB_HEAD_REF##*/}
	echo "Loaded branch from GitHub: $gh_branch ($GITHUB_HEAD_REF)"
	current_branch="${gh_branch:-$(git rev-parse --abbrev-ref HEAD)}"
	echo "Current branch resolved to: $current_branch"
	gen_branch="__generated-$current_branch"
	git branch -D "$gen_branch" || echo "no branch named $gen_branch yet"
	repo_root=$(git rev-parse --show-toplevel)
	cd "$repo_root" && ./gradlew :aws:sdk:assemble
	target="$(mktemp -d)"
	mv "$repo_root"/aws/sdk/build/aws-sdk "$target"
	git checkout --orphan "$gen_branch"
	cd "$repo_root" && git rm -rf .
	rm -rf "$repo_root/aws-sdk"
	mv "$target"/aws-sdk "$repo_root"/.
	git add "$repo_root"/aws-sdk
	PRE_COMMIT_ALLOW_NO_CONFIG=1 git -c "user.name=GitHub Action (generated code preview)" -c "user.email=generated-code-action@github.com" commit -m "Generated code for $current_branch"
} >&2