Skip to content

Commit afccf41

Browse files
authored
ci: add job to generate preview of differences to generated code (#403)
1 parent e1c9e42 commit afccf41

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed

.github/scripts/mk-generated.sh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/bin/bash
2+
#
3+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
# SPDX-License-Identifier: Apache-2.0.
5+
#
6+
7+
# Create a branch containing exclusively generated code
8+
#
9+
# This script creates a new branch `__generated-$current_branch` that contains the results of the smoke-test codegen
10+
# This script outputs the name of the new branch to stdout
11+
#
12+
13+
set -e
14+
15+
# Redirect stdout to stderr for all the code in `{ .. }`
16+
{
17+
git diff --quiet || (echo 'working tree not clean, aborting' && exit 1)
18+
gh_branch=${GITHUB_HEAD_REF##*/}
19+
base_branch=${GITHUB_BASE_REF##*/}
20+
if [ -n "$base_branch" ]; then
21+
base_branch=__generated-$base_branch
22+
else
23+
base_branch=__generated__
24+
fi
25+
echo "Loaded branch from GitHub: $gh_branch ($GITHUB_HEAD_REF). Base branch: $base_branch"
26+
current_branch="${gh_branch:-$(git rev-parse --abbrev-ref HEAD)}"
27+
echo "Current branch resolved to: $current_branch"
28+
gen_branch="__generated-$current_branch"
29+
repo_root=$(git rev-parse --show-toplevel)
30+
cd "$repo_root" && ./gradlew :codegen:sdk:bootstrap
31+
target="$(mktemp -d)"
32+
mv "$repo_root"/services "$target"
33+
# checkout and reset $gen_branch to be based on the __generated__ history
34+
git fetch origin "$base_branch"
35+
git checkout -B "$gen_branch" origin/"$base_branch"
36+
cd "$repo_root" && git rm -rf .
37+
mv "$target"/services "$repo_root"/services
38+
git add "$repo_root"/services
39+
PRE_COMMIT_ALLOW_NO_CONFIG=1 git \
40+
-c "user.name=GitHub Action (generated code preview)" \
41+
42+
commit -m "Generated code for $current_branch" --allow-empty
43+
} >&2
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: codegen diff preview
2+
# This job will generate a branch containing exclusively codegen output and push it to GitHub
3+
# once the branch is deployed, it will comment on GitHub with a link where you can see the generated diff.
4+
on:
5+
push:
6+
branches:
7+
# this is a load-bearing branch filter: if this isn't here, you may
8+
# end up generating diffs for __generated-* branches which would lead to infinite recursion...
9+
- main
10+
pull_request:
11+
types:
12+
- opened
13+
- reopened
14+
- closed
15+
- synchronize
16+
env:
17+
JAVA_VERSION: 11
18+
BUILDER_VERSION: v0.8.22
19+
BUILDER_SOURCE: releases
20+
BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net
21+
PACKAGE_NAME: aws-sdk-kotlin
22+
RUN: ${{ github.run_id }}-${{ github.run_number }}
23+
# Below is the set of services that are generated for codegen preview
24+
# These are carefully selected to exercise every Smithy protocol.
25+
# - @awsJson1_0: dynamodb
26+
# - @awsJson1_1: config
27+
# - @awsQuery: sts
28+
# - @ec2Query: ec2
29+
# - @restJson1: polly
30+
# - @restXml: s3
31+
PREVIEW_SERVICES: +dynamodb,+config,+sts,+ec2,+polly,+s3
32+
33+
jobs:
34+
cleanup-branch:
35+
if: ${{ github.event.action == 'closed' }}
36+
runs-on: ubuntu-latest
37+
name: cleanup generated code branch
38+
steps:
39+
- name: gen branch output
40+
run: echo "::set-output name=branchname::${GITHUB_HEAD_REF##*/}"
41+
id: branch_output
42+
- uses: actions/github-script@v5
43+
with:
44+
script: |
45+
console.log("deleting the generated code branch");
46+
await github.rest.git.deleteRef({
47+
owner: context.repo.owner,
48+
repo: context.repo.repo,
49+
ref: "heads/__generated-${{ steps.branch_output.outputs.branchname }}"
50+
})
51+
52+
push-generated-code:
53+
runs-on: ubuntu-latest
54+
name: Push generated code to a branch
55+
if: ${{ github.event.action != 'closed' }}
56+
steps:
57+
# this is not technically necessary because of the branch filter above, but better to check
58+
# twice than have an infinitely recursing PR job
59+
- name: Assert we aren't already on a generated branch
60+
run: |
61+
[[ ${GITHUB_HEAD_REF:-$GITHUB_REF} != "*__generated*" ]]
62+
- uses: actions/checkout@v2
63+
- uses: actions/cache@v2
64+
name: Gradle Cache
65+
with:
66+
path: |
67+
~/.gradle/caches
68+
~/.gradle/wrapper
69+
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
70+
restore-keys: |
71+
${{ runner.os }}-gradle-
72+
- name: Set up JDK
73+
uses: actions/setup-java@v1
74+
with:
75+
java-version: ${{ env.JAVA_VERSION }}
76+
- name: download-deps
77+
# abuse crt builder to download and build upstream dependencies like smithy-kotlin
78+
run: |
79+
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder.pyz')"
80+
chmod a+x builder.pyz
81+
./builder.pyz build -p ${{ env.PACKAGE_NAME }} --variant codegen-preview
82+
- name: mk-generated
83+
run: |
84+
echo "aws.services=${{ env.PREVIEW_SERVICES }}" >> local.properties
85+
.github/scripts/mk-generated.sh
86+
- name: push generated branch
87+
run: |
88+
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
89+
git push -f origin "$(git rev-parse --abbrev-ref HEAD)"
90+
- name: finalize
91+
run: echo "generated output pushed to $(git rev-parse --abbrev-ref HEAD)"
92+
- name: gen branch output
93+
run: echo "::set-output name=branchname::$(git rev-parse --abbrev-ref HEAD)"
94+
id: branch_output
95+
- uses: actions/github-script@v5
96+
# NOTE: if comments on each commit become bothersome, add a check that github.event.pull_request.action == "opened"
97+
if: ${{ github.head_ref != null }}
98+
with:
99+
script: |
100+
await github.rest.issues.createComment({
101+
issue_number: context.issue.number,
102+
owner: context.repo.owner,
103+
repo: context.repo.repo,
104+
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 }}`
105+
})

builder.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
"!test_steps": [
3939
"{gradlew} test allTests"
4040
]
41+
},
42+
"codegen-preview": {
43+
"!imports": [],
44+
"!build_steps": [],
45+
"!test_steps": []
4146
}
4247
}
4348
}

0 commit comments

Comments
 (0)