Skip to content

Commit 087783c

Browse files
authored
chore(ci): fix codegen diff (#1063)
1 parent 43cca3a commit 087783c

File tree

3 files changed

+112
-78
lines changed

3 files changed

+112
-78
lines changed

.github/scripts/codegen-diff-revisions.py

Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import tempfile
3434
import shlex
3535

36-
HEAD_BRANCH_NAME = "__tmp-localonly-head"
37-
BASE_BRANCH_NAME = "__tmp-localonly-base"
3836
OUTPUT_PATH = "tmp-codegen-diff/"
3937

4038
COMMIT_AUTHOR_NAME = "GitHub Action (generated codegen diff)"
@@ -138,9 +136,11 @@ def write_html_template(title, subtitle, tmp_file):
138136
tmp_file.flush()
139137

140138

141-
def make_diff(title, path_to_diff, base_sha, head_sha, suffix, ignore_whitespace):
139+
def make_diff(opts, title, path_to_diff, suffix, ignore_whitespace):
140+
base_sha = opts.base_sha
141+
head_sha = opts.head_sha
142142
ws_flag = "-b" if ignore_whitespace else ""
143-
diff_exists = get_cmd_status(f"git diff --quiet {ws_flag} {BASE_BRANCH_NAME} {HEAD_BRANCH_NAME} -- {path_to_diff}")
143+
diff_exists = get_cmd_status(f"git diff --quiet {ws_flag} {opts.base_branch} {opts.head_branch} -- {path_to_diff}")
144144

145145
if diff_exists == 0:
146146
eprint(f"No diff output for {base_sha}..{head_sha}")
@@ -156,7 +156,7 @@ def make_diff(title, path_to_diff, base_sha, head_sha, suffix, ignore_whitespace
156156
# All arguments after the first `--` go to the `git diff` command.
157157
diff_cmd = f"diff2html -s line -f html -d word -i command --hwt " \
158158
f"{tmp_file.name} -F {OUTPUT_PATH}/{dest_path} -- " \
159-
f"-U20 {ws_flag} {BASE_BRANCH_NAME} {HEAD_BRANCH_NAME} -- {path_to_diff}"
159+
f"-U20 {ws_flag} {opts.base_branch} {opts.head_branch} -- {path_to_diff}"
160160
eprint(f"Running diff cmd: {diff_cmd}")
161161
run(diff_cmd)
162162
return dest_path
@@ -169,29 +169,46 @@ def diff_link(diff_text, empty_diff_text, diff_location, alternate_text, alterna
169169
return f"[{diff_text}]({CDN_URL}/codegen-diff/{diff_location}) ([{alternate_text}]({CDN_URL}/codegen-diff/{alternate_location}))"
170170

171171

172-
def make_diffs(base_sha, head_sha):
173-
sdk_ws = make_diff('AWS SDK', f'{OUTPUT_PATH}/services', base_sha, head_sha, 'aws-sdk', ignore_whitespace=False)
174-
sdk_no_ws = make_diff('AWS SDK', f'{OUTPUT_PATH}/services', base_sha, head_sha, 'aws-sdk-ignore-ws',
175-
ignore_whitespace=True)
176-
172+
def make_diffs(opts):
173+
sdk_ws = make_diff(opts, 'AWS SDK', f'{OUTPUT_PATH}/services', 'aws-sdk', ignore_whitespace=False)
174+
sdk_no_ws = make_diff(opts, 'AWS SDK', f'{OUTPUT_PATH}/services', 'aws-sdk-ignore-ws', ignore_whitespace=True)
177175
sdk_links = diff_link('AWS SDK', 'No codegen difference in the AWS SDK', sdk_ws, 'ignoring whitespace', sdk_no_ws)
178176

179177
return f'A new generated diff is ready to view.\\n\\n- {sdk_links}\\n'
180178

181179

180+
def _codegen_cmd(opts):
181+
generate_and_commit_generated_code(opts.head_sha, opts.bootstrap)
182+
183+
184+
def _generate_diffs_cmd(opts):
185+
bot_message = make_diffs(opts)
186+
with open(f"{OUTPUT_PATH}/bot-message", 'w') as f:
187+
f.write(bot_message)
188+
189+
182190
def create_cli():
183191
parser = argparse.ArgumentParser(
184192
prog="codegen-diff-revisions",
185193
description="Generate HTML diffs of codegen output",
186194
formatter_class=argparse.ArgumentDefaultsHelpFormatter
187195
)
188196

189-
parser.add_argument("repo_root", help="repository root")
190-
parser.add_argument("base_sha", help="base commit to diff against (SHA-like)")
191-
parser.add_argument("--bootstrap", help="services to pass to bootstrap and include in diff output",
192-
default="+dynamodb,+codebuild,+sts,+ec2,+polly,+s3")
193197
parser.add_argument("--head-sha", help="head commit to use (defaults to whatever current HEAD) is")
194198

199+
subparsers = parser.add_subparsers()
200+
codegen = subparsers.add_parser("codegen", help="generate and commit generated code")
201+
codegen.add_argument("--bootstrap", help="services to pass to bootstrap and include in diff output",
202+
default="+dynamodb,+codebuild,+sts,+ec2,+polly,+s3")
203+
codegen.set_defaults(cmd=_codegen_cmd)
204+
205+
generate_diffs = subparsers.add_parser("generate-diffs",
206+
help="generate diffs between two branches and output bot message")
207+
generate_diffs.add_argument("--base-sha", help="base commit to diff against (SHA-like)")
208+
generate_diffs.add_argument("base_branch", help="name of the base branch to diff against")
209+
generate_diffs.add_argument("head_branch", help="name of the head branch to diff against")
210+
generate_diffs.set_defaults(cmd=_generate_diffs_cmd)
211+
195212
return parser
196213

197214

@@ -200,39 +217,12 @@ def main():
200217
opts = cli.parse_args()
201218
print(opts)
202219

203-
os.chdir(opts.repo_root)
204-
205220
if opts.head_sha is None:
206-
head_sha = get_cmd_output("git rev-parse HEAD")
207-
else:
208-
head_sha = opts.head_sha
209-
210-
print(f"using head sha is {head_sha}")
211-
212-
# Make sure the working tree is clean
213-
if get_cmd_status("git diff --quiet") != 0:
214-
eprint("working tree is not clean. aborting")
215-
sys.exit(1)
221+
opts.head_sha = get_cmd_output("git rev-parse HEAD")
216222

217-
# Generate code for HEAD
218-
print(f"Creating temporary branch with generated code for the HEAD revision {head_sha}")
219-
run(f"git checkout {head_sha} -b {HEAD_BRANCH_NAME}")
220-
generate_and_commit_generated_code(head_sha, opts.bootstrap)
221-
222-
# Generate code for base
223-
print(f"Creating temporary branch with generated code for the base revision {opts.base_sha}")
224-
run(f"git checkout {opts.base_sha} -b {BASE_BRANCH_NAME}")
225-
generate_and_commit_generated_code(opts.base_sha, opts.bootstrap)
226-
227-
bot_message = make_diffs(opts.base_sha, head_sha)
228-
with open(f"{OUTPUT_PATH}/bot-message", 'w') as f:
229-
f.write(bot_message)
223+
print(f"using head sha: {opts.head_sha}")
230224

231-
# cleanup
232-
if not running_in_github_action():
233-
run(f"git checkout main")
234-
run(f"git branch -D {BASE_BRANCH_NAME}")
235-
run(f"git branch -D {HEAD_BRANCH_NAME}")
225+
opts.cmd(opts)
236226

237227

238228
if __name__ == '__main__':

.github/workflows/codegen-preview.yml

Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ concurrency:
2020

2121
env:
2222
JAVA_VERSION: 11
23-
BUILDER_VERSION: v0.8.22
24-
BUILDER_SOURCE: releases
25-
BUILDER_HOST: https://d19elf31gohf1l.cloudfront.net
26-
PACKAGE_NAME: aws-sdk-kotlin
2723
RUN: ${{ github.run_id }}-${{ github.run_number }}
2824
DIFF2HTML_VERSION: 5.2.5
2925
# Below is the set of services that are generated for codegen preview
@@ -35,6 +31,8 @@ env:
3531
# - @restJson1: polly
3632
# - @restXml: s3
3733
PREVIEW_SERVICES: +dynamodb,+codebuild,+sts,+ec2,+polly,+s3
34+
HEAD_BRANCH_NAME: __tmp-localonly-head
35+
BASE_BRANCH_NAME: __tmp-localonly-base
3836

3937
jobs:
4038

@@ -44,9 +42,20 @@ jobs:
4442
outputs:
4543
bot-message: ${{ steps.generate-diff.outputs.codegen-diff-msg }}
4644
steps:
47-
- uses: actions/checkout@v2
45+
- uses: actions/checkout@v4
4846
with:
4947
fetch-depth: 0
48+
path: 'aws-sdk-kotlin'
49+
- uses: actions/checkout@v4
50+
with:
51+
repository: 'awslabs/aws-kotlin-repo-tools'
52+
ref: 'ci-utils'
53+
path: 'aws-kotlin-repo-tools'
54+
- uses: actions/checkout@v4
55+
with:
56+
repository: 'awslabs/smithy-kotlin'
57+
fetch-depth: 0
58+
path: 'smithy-kotlin'
5059
- uses: actions/cache@v2
5160
name: Gradle Cache
5261
with:
@@ -60,47 +69,87 @@ jobs:
6069
uses: actions/setup-java@v1
6170
with:
6271
java-version: ${{ env.JAVA_VERSION }}
63-
- name: Install and build deps
64-
# abuse crt builder to download and build upstream dependencies like smithy-kotlin
72+
73+
- name: Install deps and setup environment
6574
run: |
6675
npm install -g diff2html-cli@${{ env.DIFF2HTML_VERSION }}
67-
python3 -c "from urllib.request import urlretrieve; urlretrieve('${{ env.BUILDER_HOST }}/${{ env.BUILDER_SOURCE }}/${{ env.BUILDER_VERSION }}/builder.pyz?run=${{ env.RUN }}', 'builder.pyz')"
68-
chmod a+x builder.pyz
69-
./builder.pyz build -p ${{ env.PACKAGE_NAME }} --variant codegen-preview
70-
- name: Generate diff
71-
id: generate-diff
76+
env | sort
77+
# store off a copy of head ref of ci.py, otherwise base ref generation will use a different version of this script
78+
CODEGEN_DIFF_REVISIONS=${{ runner.temp }}/codegen-diff-revisions.py
79+
cp $GITHUB_WORKSPACE/aws-sdk-kotlin/.github/scripts/codegen-diff-revisions.py $CODEGEN_DIFF_REVISIONS
80+
echo "CODEGEN_DIFF_REVISIONS=$CODEGEN_DIFF_REVISIONS" >> "$GITHUB_ENV"
81+
echo "REPO_TOOLS=$GITHUB_WORKSPACE/aws-kotlin-repo-tools" >> "$GITHUB_ENV"
82+
echo "SMITHY_KOTLIN_DIR=$GITHUB_WORKSPACE/smithy-kotlin" >> "$GITHUB_ENV"
83+
echo "SDK_DIR=$GITHUB_WORKSPACE/aws-sdk-kotlin" >> "$GITHUB_ENV"
84+
85+
- name: Generate code for head ref
7286
run: |
73-
# codegen-diff-revisions requires a clean index, set-upstream-versions.py can modify local repo state
74-
# we don't push these branches/commits anywhere so just commit it if necessary and move on
75-
if ! git diff --quiet gradle.properties
87+
branch=$(python3 $REPO_TOOLS/scripts/ci.py get-branch $SDK_DIR)
88+
pushd $SMITHY_KOTLIN_DIR
89+
if git ls-remote --quiet --exit-code --heads origin refs/heads/$branch
7690
then
77-
echo "gradle.properties is dirty, committing before generating diffs"
78-
git add gradle.properties
79-
PRE_COMMIT_ALLOW_NO_CONFIG=1 git \
80-
-c "user.name=GitHub Action (generated code preview)" \
81-
82-
commit -m "codegen diff autocommit" --allow-empty
91+
echo "using smithy-kotlin branch $branch for head ref codegen"
92+
else
93+
# in the case of feature branches with tentacles we may or may not have a corresponding base ref in smithy-kotlin
94+
# if we do let's use that, otherwise fallback to main
95+
echo "$branch does not exist in smithy-kotlin, will attempt base ref $GITHUB_BASE_REF"
96+
branch=$GITHUB_BASE_REF
8397
fi
84-
.github/scripts/codegen-diff-revisions.py \
85-
--bootstrap ${{ env.PREVIEW_SERVICES }} \
98+
python3 $REPO_TOOLS/scripts/ci.py -v set-branch --branch $branch
99+
popd
100+
pushd $SDK_DIR
101+
git checkout -b $HEAD_BRANCH_NAME
102+
$CODEGEN_DIFF_REVISIONS codegen --bootstrap ${{ env.PREVIEW_SERVICES }}
103+
popd
104+
105+
- name: Generate code for base ref
106+
run: |
107+
branch=$GITHUB_BASE_REF
108+
echo "checkout smithy-kotlin at base ref: $branch"
109+
pushd $SMITHY_KOTLIN_DIR
110+
git switch -f main
111+
python3 $REPO_TOOLS/scripts/ci.py -v set-branch --branch $branch
112+
popd
113+
echo "resetting aws-sdk-kotlin"
114+
pushd $SDK_DIR
115+
git switch -f main
116+
python3 $REPO_TOOLS/scripts/ci.py -v set-branch --branch $branch
117+
git checkout -b $BASE_BRANCH_NAME
118+
$CODEGEN_DIFF_REVISIONS codegen --bootstrap ${{ env.PREVIEW_SERVICES }}
119+
popd
120+
121+
- name: Generate diffs
122+
id: generate-diff
123+
run: |
124+
pushd $SDK_DIR
125+
$CODEGEN_DIFF_REVISIONS \
86126
--head-sha ${{ github.event.pull_request.head.sha }} \
87-
. ${{ github.event.pull_request.base.sha }}
88-
echo "codegen-diff-msg<<EOF" >> $GITHUB_OUTPUT
89-
cat ./tmp-codegen-diff/bot-message) >> $GITHUB_OUTPUT
90-
echo "EOF" >> $GITHUB_OUTPUT
127+
generate-diffs \
128+
--base-sha ${{ github.event.pull_request.base.sha }} \
129+
$BASE_BRANCH_NAME $HEAD_BRANCH_NAME
130+
cat ./tmp-codegen-diff/bot-message
131+
{
132+
echo 'codegen-diff-msg<<DIFF_MSG_EOF'
133+
echo "$(cat ./tmp-codegen-diff/bot-message)"
134+
echo 'DIFF_MSG_EOF'
135+
} >> "$GITHUB_OUTPUT"
136+
echo $GITHUB_OUTPUT
137+
91138
- name: Configure AWS Credentials
92139
uses: aws-actions/configure-aws-credentials@v2
93140
with:
94141
role-to-assume: ${{ secrets.CI_AWS_ROLE_ARN }}
95142
aws-region: us-west-2
143+
96144
- name: Upload diff to S3
97145
run: |
146+
SDK_DIR=$GITHUB_WORKSPACE/aws-sdk-kotlin
147+
pushd $SDK_DIR
98148
if [[ -d ./tmp-codegen-diff/${{ github.event.pull_request.base.sha }} ]]; then
99149
aws s3 cp ./tmp-codegen-diff/${{ github.event.pull_request.base.sha }} \
100150
"s3://${{ secrets.CDN_S3_BUCKET_NAME }}/codegen-diff/${{ github.event.pull_request.base.sha }}" --recursive
101151
fi
102152
103-
104153
# TODO - generate doc preview for N services and upload and link as well
105154
post-bot-comment:
106155
name: Post bot comment
@@ -117,4 +166,4 @@ jobs:
117166
owner: context.repo.owner,
118167
repo: context.repo.repo,
119168
body: '${{ needs.generate-codegen-diff.outputs.bot-message }}\n\n'
120-
})
169+
})

builder.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@
5757
"!test_steps": [
5858
"{gradlew} test allTests"
5959
]
60-
},
61-
"codegen-preview": {
62-
"!imports": [],
63-
"!build_steps": [],
64-
"!test_steps": []
6560
}
6661
}
6762
}

0 commit comments

Comments
 (0)