-
Notifications
You must be signed in to change notification settings - Fork 23
175 lines (145 loc) · 6.04 KB
/
Copy pathdiff-generated-code.yml
File metadata and controls
175 lines (145 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: Diff Generated Code
on:
pull_request:
jobs:
diff-generated:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Checkout PR branch
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
ref: ${{ github.event.pull_request.head.sha }}
path: pr-branch
- name: Checkout target branch
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
with:
ref: ${{ github.event.pull_request.base.sha }}
path: base-branch
- name: Setup .NET
uses: actions/setup-dotnet@9a946fdbd5fb07b82b2f5a4466058b876ab72bb2 # v5.3.0
with:
dotnet-version: '10.0.x'
- name: Build base branch to generate KnownMimeTypes.cs
working-directory: base-branch
run: dotnet build src/MimeMapping/MimeMapping.csproj
- name: Build PR branch to generate KnownMimeTypes.cs
working-directory: pr-branch
run: dotnet build src/MimeMapping/MimeMapping.csproj
- name: Generate diff
id: diff
run: |
echo "=== DEBUG: Searching for generated files ==="
echo "Base branch files:"
find base-branch -name "KnownMimeTypes.cs" -o -name "KnownMimeTypes.g.cs" 2>/dev/null || echo "No files found"
echo ""
echo "PR branch files:"
find pr-branch -name "KnownMimeTypes.cs" -o -name "KnownMimeTypes.g.cs" 2>/dev/null || echo "No files found"
echo ""
# Find the generated file (supports both old T4 and new source generator locations)
BASE_FILE=$(find base-branch -name "KnownMimeTypes.cs" -o -name "KnownMimeTypes.g.cs" 2>/dev/null | head -1)
PR_FILE=$(find pr-branch -name "KnownMimeTypes.cs" -o -name "KnownMimeTypes.g.cs" 2>/dev/null | head -1)
echo "=== DEBUG: Selected files ==="
echo "BASE_FILE: $BASE_FILE"
echo "PR_FILE: $PR_FILE"
echo ""
if [ -z "$BASE_FILE" ] || [ -z "$PR_FILE" ]; then
echo "ERROR: Could not find generated files!"
echo "Listing all .cs files in obj directories:"
find base-branch -path "*/obj/*" -name "*.cs" 2>/dev/null | head -20
find pr-branch -path "*/obj/*" -name "*.cs" 2>/dev/null | head -20
exit 1
fi
echo "=== DEBUG: File sizes ==="
wc -l "$BASE_FILE" "$PR_FILE"
echo ""
# Extract only the public const string lines and diff them
grep 'public const string .* = "' "$BASE_FILE" | sort > base-consts.txt
grep 'public const string .* = "' "$PR_FILE" | sort > pr-consts.txt
echo "=== DEBUG: Extracted constants count ==="
wc -l base-consts.txt pr-consts.txt
echo ""
diff -u base-consts.txt pr-consts.txt > diff.txt || true
if [ -s diff.txt ]; then
echo "has_diff=true" >> $GITHUB_OUTPUT
# Truncate if too large for GitHub comment (max ~65k chars)
if [ $(wc -c < diff.txt) -gt 60000 ]; then
head -c 60000 diff.txt > diff_truncated.txt
echo -e "\n\n... (diff truncated, too large to display)" >> diff_truncated.txt
mv diff_truncated.txt diff.txt
fi
else
echo "has_diff=false" >> $GITHUB_OUTPUT
fi
- name: Post diff as PR comment
if: steps.diff.outputs.has_diff == 'true'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
script: |
const fs = require('fs');
const diff = fs.readFileSync('diff.txt', 'utf8');
const body = `## Generated Code Diff
This PR changes the generated \`KnownMimeTypes.cs\` file:
<details>
<summary>Click to expand diff</summary>
\`\`\`diff
${diff}
\`\`\`
</details>`;
// Find existing comment to update
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' &&
c.body.includes('## Generated Code Diff')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body,
});
}
- name: Post no-diff comment
if: steps.diff.outputs.has_diff == 'false'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
script: |
const body = `## Generated Code Diff
No changes to generated \`KnownMimeTypes.cs\` file.`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' &&
c.body.includes('## Generated Code Diff')
);
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body,
});
}