Unverified Commit 80385399 authored by John DiSanti's avatar John DiSanti Committed by GitHub
Browse files

Improve the codegen diff bot (#918)

parent d4bc9d60
Loading
Loading
Loading
Loading
+37 −45
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.
# This job will generate a codegen diff, upload it to S3, and link to it in a comment on the PR.
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' }}
  generate-diff:
    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' }}
    name: Generate diff and upload to S3
    env:
      AWS_REGION: us-west-2
      S3_BUCKET_NAME: ${{ secrets.SMITHY_RS_PULL_REQUEST_CDN_S3_BUCKET_NAME }}
    permissions:
      id-token: write
      contents: read
      pull-requests: write
    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
@@ -54,29 +30,45 @@ jobs:
        key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
        restore-keys: |
          ${{ runner.os }}-gradle-
    # JDK is needed to generate code
    - 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
    # Node is needed to run diff2html
    - name: Set up NodeJS
      uses: actions/setup-node@v2
      with:
        node-version: '16'
    - name: Install diff2html-cli
      run: npm install -g diff2html-cli@5.1.11
    - name: Generate diff
      id: generate-diff
      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
        ./tools/codegen-diff-revisions.py . ${{ github.event.pull_request.base.sha }}
        echo "::set-output name=bot-message::$(cat tmp-codegen-diff/bot-message)"
    - uses: aws-actions/configure-aws-credentials@v1
      name: Acquire credentials for uploading to S3
      with:
        role-to-assume: ${{ secrets.SMITHY_RS_PULL_REQUEST_CDN_ROLE_ARN }}
        role-session-name: GitHubActions
        aws-region: us-west-2
    - name: Upload diff to S3
      run: |
        if [[ -d tmp-codegen-diff/${{ github.event.pull_request.base.sha }} ]]; then
            aws s3 cp tmp-codegen-diff/${{ github.event.pull_request.base.sha }} \
                "s3://${S3_BUCKET_NAME}/codegen-diff/${{ github.event.pull_request.base.sha }}" --recursive
        fi
    - uses: actions/github-script@v5
      # NOTE: if comments on each commit become bothersome, add a check that github.event.pull_request.action == "opened"
      if: ${{ github.head_ref != null }}
      with:
        script: |
          const { DIFF_FILE_NAME } = process.env;

          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 }}`
            body: '${{ steps.generate-diff.outputs.bot-message }}'
          })
+16 −0
Original line number Diff line number Diff line
{
    "env": {
        "browser": false,
        "es2021": true
    },
    "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
        "ecmaVersion": 13,
        "sourceType": "module"
    },
    "plugins": ["@typescript-eslint"],
    "rules": {
        "@typescript-eslint/no-empty-function": "off"
    }
}
+9 −0
Original line number Diff line number Diff line
*.js
!jest.config.js
*.d.ts
node_modules
build

# CDK asset staging directory
.cdk.staging
cdk.out
+6 −0
Original line number Diff line number Diff line
*.ts
!*.d.ts

# CDK asset staging directory
.cdk.staging
cdk.out
+5 −0
Original line number Diff line number Diff line
tabWidth: 4
singleQuote: false
bracketSpacing: true
trailingComma: all
printWidth: 100
Loading