Skip to content

Commit 22bbcd2

Browse files
authored
add a GH Actions workflow to check and see if any generated files need to be updated (#432)
* add a GH Actions workflow to check and see if any generated files need to be updated * need protoc to run go generate * looks like we do need to run with `sudo` * also need `protoc-gen-go` binary * pass the diff output as a file; it was too long for an env var * pin protoc-gen-go@1.34.2 * fix version number * don't care about exact protoc version & also don't fail the build
1 parent fac0bec commit 22bbcd2

1 file changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
name: Check Generated Code
2+
3+
on:
4+
pull_request:
5+
branches: [ main ]
6+
7+
permissions:
8+
pull-requests: write # Needed for commenting on PRs
9+
contents: read # Needed for checking out code
10+
11+
jobs:
12+
check-generated:
13+
name: Check Generated Code
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout code
17+
uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0 # Required for git diff to work properly
20+
21+
- name: Set up Go
22+
uses: actions/setup-go@v5
23+
with:
24+
go-version: '1.21'
25+
26+
- name: Install protoc and protoc-gen-go
27+
run: |
28+
sudo apt-get install -y protobuf-compiler
29+
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.34.2
30+
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
31+
32+
- name: Run go generate
33+
run: |
34+
go generate ./...
35+
36+
- name: Check for changes
37+
id: check_changes
38+
run: |
39+
# Create a temporary file for the diff, ignoring footer lines and protoc versions
40+
git diff --ignore-matching-lines="Auto generated by spf13/cobra on" --ignore-matching-lines="protoc v[0-9]" > changes.diff
41+
42+
# Check if there are any changes
43+
if [ -s changes.diff ]; then
44+
echo "has_changes=true" >> $GITHUB_OUTPUT
45+
fi
46+
47+
- name: Comment on PR
48+
uses: actions/github-script@v6
49+
if: steps.check_changes.outputs.has_changes == 'true'
50+
with:
51+
script: |
52+
// Find existing comment from this workflow
53+
const { data: comments } = await github.rest.issues.listComments({
54+
owner: context.repo.owner,
55+
repo: context.repo.repo,
56+
issue_number: context.issue.number,
57+
});
58+
59+
const existingComment = comments.find(comment =>
60+
comment.user.login === 'github-actions[bot]' &&
61+
comment.body.includes('## Generated Code Changes Required')
62+
);
63+
64+
// Read the diff file
65+
const fs = require('fs');
66+
const diff = fs.readFileSync('changes.diff', 'utf8');
67+
68+
// Limit diff to 50,000 characters (leaving room for the rest of the comment)
69+
const MAX_DIFF_LENGTH = 50000;
70+
const isTruncated = diff.length > MAX_DIFF_LENGTH;
71+
const truncatedDiff = isTruncated ? diff.substring(0, MAX_DIFF_LENGTH) + '\n... (diff truncated)' : diff;
72+
73+
const body = '## Generated Code Changes Required\n\n' +
74+
'The following files may need to be regenerated. Please run `go generate ./...` and commit the changes:\n\n' +
75+
'```diff\n' +
76+
truncatedDiff +
77+
'\n```' +
78+
(isTruncated ? '\n\n*Note: The diff has been truncated. Please run `go generate ./...` locally to see all changes.*' : '');
79+
80+
if (existingComment) {
81+
// Update existing comment
82+
await github.rest.issues.updateComment({
83+
owner: context.repo.owner,
84+
repo: context.repo.repo,
85+
comment_id: existingComment.id,
86+
body: body
87+
});
88+
} else {
89+
// Create new comment
90+
await github.rest.issues.createComment({
91+
owner: context.repo.owner,
92+
repo: context.repo.repo,
93+
issue_number: context.issue.number,
94+
body: body
95+
});
96+
}
97+
98+
- name: Update success comment
99+
uses: actions/github-script@v6
100+
if: steps.check_changes.outputs.has_changes != 'true'
101+
with:
102+
script: |
103+
// Find existing comment from this workflow
104+
const { data: comments } = await github.rest.issues.listComments({
105+
owner: context.repo.owner,
106+
repo: context.repo.repo,
107+
issue_number: context.issue.number,
108+
});
109+
110+
const existingComment = comments.find(comment =>
111+
comment.user.login === 'github-actions[bot]' &&
112+
comment.body.includes('## Generated Code Changes Required')
113+
);
114+
115+
if (existingComment) {
116+
// Update existing comment to show success
117+
await github.rest.issues.updateComment({
118+
owner: context.repo.owner,
119+
repo: context.repo.repo,
120+
comment_id: existingComment.id,
121+
body: '## Generated Code Check ✅\n\nAll generated code is up to date.'
122+
});
123+
}

0 commit comments

Comments
 (0)