Merge pull request #17728 from SimonRichardson/move-testing-to-internal #595
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: "Generate" | |
on: | |
push: | |
branches: [2.*, 3.*, 4.*, main] | |
pull_request: | |
types: [opened, synchronize, reopened, ready_for_review] | |
workflow_dispatch: | |
permissions: | |
contents: read | |
jobs: | |
Generate: | |
name: Generate | |
runs-on: [self-hosted, linux, arm64, aws, xxlarge] | |
if: github.event.pull_request.draft == false | |
steps: | |
# Since this check is marked as "required", the action needs to run on | |
# every PR. However, if there were no changes to Go files, there will | |
# be no changes to the generated files, hence we don't need to check. | |
# So this step checks which files have been changed, and allows the | |
# rest of the workflow to be skipped if no Go files were changed. | |
- name: Check changed files | |
id: should-run | |
uses: dorny/paths-filter@v3 | |
with: | |
filters: | | |
go: | |
- '**.go' | |
- 'go.mod' | |
- '.github/workflows/gen.yml' | |
- name: "Checkout" | |
if: steps.should-run.outputs.go == 'true' | |
uses: actions/checkout@v4 | |
- name: "Set up Go" | |
if: steps.should-run.outputs.go == 'true' | |
uses: actions/setup-go@v5 | |
with: | |
go-version-file: 'go.mod' | |
cache: true | |
- name: "Delete all mocks" | |
if: steps.should-run.outputs.go == 'true' | |
shell: bash | |
# Ideally we'd delete all generated files, but we can't because some of | |
# the Go code depends on generated files for go:generate to actually work. | |
run: | | |
for FILE in $(grep '// Code generated by MockGen. DO NOT EDIT.' -r . -l --include \*.go); do | |
rm $FILE | |
done | |
- name: "Regenerate code" | |
if: steps.should-run.outputs.go == 'true' | |
shell: bash | |
run: | | |
# Running go generate by itself is slow over a large codebase, where | |
# all generate directives are dispersed over many files. Instead, the | |
# following uses tools for locating and extracting the directives, | |
# before piping them to go generate in parallel. | |
# | |
# 1. grep for go generate directive in the go files recursively. | |
# 2. Grab the file name of each select file. | |
# 3. Unique every file, so we only go generate the file once. | |
# 4. Using xargs perform go generate in parallel. | |
# | |
grep -ir "//go:generate" --include '*.go' . | awk -F : '{ print $1 }' | uniq | xargs -n 1 -P 8 -I% go generate -x $(realpath %) | |
- name: "Check diff" | |
if: steps.should-run.outputs.go == 'true' | |
shell: bash | |
run: | | |
git add -A | |
if [[ -n $(git diff HEAD) ]]; then | |
# Print the full diff for debugging purposes | |
git diff HEAD | |
echo "*****" | |
echo "The following generated files have been modified:" | |
git diff --name-status HEAD | |
echo "Please regenerate these files and check in the changes." | |
echo "*****" | |
exit 1 | |
fi |