Debian Codegraph @

.github/workflows/resource-diff.yml

raw symbols SCIP
1 name: Resource Diff
2
3 on:
4 pull_request:
5 branches: [master]
6 workflow_dispatch:
7 inputs:
8 pull_request:
9 description: "Pull request number to compare. Enables manual maintainer-approved diff for fork PRs."
10 required: false
11 type: string
12 before_ref:
13 description: "Base ref/SHA to compare from. Defaults to the PR base or master."
14 required: false
15 type: string
16 after_ref:
17 description: "Head ref/SHA to compare. Defaults to the PR head or current ref."
18 required: false
19 type: string
20
21 concurrency:
22 group: ${{ github.workflow }}-${{ github.ref }}
23 cancel-in-progress: true
24
25 jobs:
26 diff:
27 permissions:
28 contents: read
29 pull-requests: read
30 runs-on: ubuntu-latest
31
32 steps:
33 - name: Resolve refs
34 id: refs
35 shell: bash
36 env:
37 EVENT_NAME: ${{ github.event_name }}
38 INPUT_PULL_REQUEST: ${{ inputs.pull_request }}
39 INPUT_BEFORE_REF: ${{ inputs.before_ref }}
40 INPUT_AFTER_REF: ${{ inputs.after_ref }}
41 PR_BASE_SHA: ${{ github.event.pull_request.base.sha }}
42 PR_HEAD_SHA: ${{ github.event.pull_request.head.sha }}
43 PR_HEAD_REPO: ${{ github.event.pull_request.head.repo.full_name }}
44 PR_HEAD_REPO_URL: ${{ github.event.pull_request.head.repo.clone_url }}
45 GITHUB_SHA_VALUE: ${{ github.sha }}
46 GH_TOKEN: ${{ github.token }}
47 REPOSITORY: ${{ github.repository }}
48 run: |
49 pr_number=""
50 pr_head_repo=""
51 pr_head_repo_url=""
52 manual_pr=0
53
54 if [ "$EVENT_NAME" = "workflow_dispatch" ] && [ -n "$INPUT_PULL_REQUEST" ]; then
55 pr_number="$INPUT_PULL_REQUEST"
56 before_ref="${INPUT_BEFORE_REF:-$(gh api "repos/${REPOSITORY}/pulls/${pr_number}" --jq '.base.sha')}"
57 after_ref="${INPUT_AFTER_REF:-$(gh api "repos/${REPOSITORY}/pulls/${pr_number}" --jq '.head.sha')}"
58 pr_head_repo="$(gh api "repos/${REPOSITORY}/pulls/${pr_number}" --jq '.head.repo.full_name')"
59 pr_head_repo_url="$(gh api "repos/${REPOSITORY}/pulls/${pr_number}" --jq '.head.repo.clone_url')"
60 manual_pr=1
61 elif [ "$EVENT_NAME" = "pull_request" ]; then
62 before_ref="${INPUT_BEFORE_REF:-$PR_BASE_SHA}"
63 after_ref="${INPUT_AFTER_REF:-$PR_HEAD_SHA}"
64 pr_head_repo="$PR_HEAD_REPO"
65 pr_head_repo_url="$PR_HEAD_REPO_URL"
66 else
67 before_ref="${INPUT_BEFORE_REF:-master}"
68 after_ref="${INPUT_AFTER_REF:-$GITHUB_SHA_VALUE}"
69 pr_head_repo="$REPOSITORY"
70 fi
71
72 echo "before_ref=$before_ref" >> "$GITHUB_OUTPUT"
73 echo "after_ref=$after_ref" >> "$GITHUB_OUTPUT"
74 echo "pr_number=$pr_number" >> "$GITHUB_OUTPUT"
75 echo "pr_head_repo=$pr_head_repo" >> "$GITHUB_OUTPUT"
76 echo "pr_head_repo_url=$pr_head_repo_url" >> "$GITHUB_OUTPUT"
77 echo "manual_pr=$manual_pr" >> "$GITHUB_OUTPUT"
78 echo "Before: $before_ref"
79 echo "After: $after_ref"
80 if [ -n "$pr_number" ]; then
81 echo "Pull request: #$pr_number"
82 fi
83 if [ -n "$pr_head_repo" ]; then
84 echo "PR head repository: $pr_head_repo"
85 fi
86
87 - name: Check resource diff safety
88 id: safety
89 shell: bash
90 env:
91 EVENT_NAME: ${{ github.event_name }}
92 PR_HEAD_REPO: ${{ steps.refs.outputs.pr_head_repo }}
93 MANUAL_PR: ${{ steps.refs.outputs.manual_pr }}
94 REPOSITORY: ${{ github.repository }}
95 run: |
96 enabled=1
97 use_buildbuddy=1
98 if [ "$EVENT_NAME" = "pull_request" ] && [ "$PR_HEAD_REPO" != "$REPOSITORY" ]; then
99 enabled=0
100 fi
101 if [ "$PR_HEAD_REPO" != "$REPOSITORY" ]; then
102 use_buildbuddy=0
103 fi
104 echo "enabled=$enabled" >> "$GITHUB_OUTPUT"
105 echo "use_buildbuddy=$use_buildbuddy" >> "$GITHUB_OUTPUT"
106
107 if [ "$enabled" = "0" ]; then
108 {
109 echo "## Resource Diff Skipped"
110 echo
111 echo "Resource diff is skipped for fork pull requests because building"
112 echo "the resource zip executes repository Bazel/Python code from the PR."
113 echo
114 echo "- Repository: \`${REPOSITORY}\`"
115 echo "- PR head repository: \`${PR_HEAD_REPO}\`"
116 echo "- Before ref: \`${{ steps.refs.outputs.before_ref }}\`"
117 echo "- After ref: \`${{ steps.refs.outputs.after_ref }}\`"
118 echo
119 echo "A maintainer can run this workflow manually with the PR number after review."
120 } >> "$GITHUB_STEP_SUMMARY"
121 fi
122
123 if [ "$enabled" = "1" ] && [ "$MANUAL_PR" = "1" ]; then
124 {
125 echo "## Resource Diff Manually Approved"
126 echo
127 echo "This workflow was manually triggered for PR #${{ steps.refs.outputs.pr_number }}."
128 echo "- PR head repository: \`${PR_HEAD_REPO}\`"
129 echo "- BuildBuddy enabled: \`${use_buildbuddy}\`"
130 echo
131 } >> "$GITHUB_STEP_SUMMARY"
132 fi
133
134 - name: Checkout repository
135 if: steps.safety.outputs.enabled == '1'
136 uses: actions/checkout@v5
137 with:
138 fetch-depth: 0
139
140 - name: Create comparison worktrees
141 if: steps.safety.outputs.enabled == '1'
142 shell: bash
143 env:
144 EVENT_NAME: ${{ github.event_name }}
145 PR_HEAD_REPO_URL: ${{ steps.refs.outputs.pr_head_repo_url }}
146 run: |
147 before_ref="${{ steps.refs.outputs.before_ref }}"
148 after_ref="${{ steps.refs.outputs.after_ref }}"
149
150 git fetch --no-tags origin "${before_ref}:refs/resource-diff/before"
151 if [ -n "$PR_HEAD_REPO_URL" ]; then
152 git fetch --no-tags "$PR_HEAD_REPO_URL" "${after_ref}:refs/resource-diff/after"
153 else
154 git fetch --no-tags origin "${after_ref}:refs/resource-diff/after"
155 fi
156
157 git worktree add "$RUNNER_TEMP/opencc-before" refs/resource-diff/before
158 git worktree add "$RUNNER_TEMP/opencc-after" refs/resource-diff/after
159
160 - name: Verify checked out refs
161 if: steps.safety.outputs.enabled == '1'
162 shell: bash
163 run: |
164 before_head="$(git -C "$RUNNER_TEMP/opencc-before" rev-parse HEAD)"
165 after_head="$(git -C "$RUNNER_TEMP/opencc-after" rev-parse HEAD)"
166 {
167 echo "## Checked Out Refs"
168 echo
169 echo "- Expected before: \`${{ steps.refs.outputs.before_ref }}\`"
170 echo "- Actual before: \`${before_head}\`"
171 echo "- Expected after: \`${{ steps.refs.outputs.after_ref }}\`"
172 echo "- Actual after: \`${after_head}\`"
173 echo
174 } >> "$GITHUB_STEP_SUMMARY"
175
176 test "$before_head" = "${{ steps.refs.outputs.before_ref }}"
177 test "$after_head" = "${{ steps.refs.outputs.after_ref }}"
178
179 - uses: bazel-contrib/[email protected]
180 if: steps.safety.outputs.enabled == '1'
181 with:
182 bazelisk-cache: true
183
184 - name: Set up Python
185 if: steps.safety.outputs.enabled == '1'
186 uses: actions/setup-python@v6
187 with:
188 python-version: "3.12"
189
190 - name: Configure BuildBuddy
191 if: steps.safety.outputs.enabled == '1' && steps.safety.outputs.use_buildbuddy == '1'
192 shell: bash
193 env:
194 BUILDBUDDY_API_KEY: ${{ secrets.BUILDBUDDY_API_KEY }}
195 run: |
196 if [ -n "${BUILDBUDDY_API_KEY}" ]; then
197 {
198 echo "build --config=buildbuddy"
199 echo "build --remote_header=x-buildbuddy-api-key=${BUILDBUDDY_API_KEY}"
200 echo "build --bes_header=x-buildbuddy-api-key=${BUILDBUDDY_API_KEY}"
201 } >> "$RUNNER_TEMP/opencc-before/.bazelrc.user"
202 echo "BuildBuddy enabled for before worktree."
203 else
204 echo "BUILDBUDDY_API_KEY not set; skipping BuildBuddy config."
205 fi
206
207 - name: Build before resource zip
208 if: steps.safety.outputs.enabled == '1'
209 shell: bash
210 working-directory: ${{ runner.temp }}/opencc-before
211 env:
212 MSYS_NO_PATHCONV: 1
213 run: |
214 args=()
215 if [ -x ./scripts/bazel-workspace-status.sh ]; then
216 args+=(--workspace_status_command=./scripts/bazel-workspace-status.sh)
217 fi
218 bazel build "${args[@]}" //data:opencc_resources_zip
219 mkdir -p "$GITHUB_WORKSPACE/dist"
220 cp -f bazel-bin/data/opencc-resources.zip "$GITHUB_WORKSPACE/dist/opencc-resources-before.zip"
221
222 - name: Build after resource zip
223 if: steps.safety.outputs.enabled == '1'
224 shell: bash
225 working-directory: ${{ runner.temp }}/opencc-after
226 env:
227 MSYS_NO_PATHCONV: 1
228 run: |
229 args=()
230 if [ -x ./scripts/bazel-workspace-status.sh ]; then
231 args+=(--workspace_status_command=./scripts/bazel-workspace-status.sh)
232 fi
233 bazel build "${args[@]}" //data:opencc_resources_zip
234 mkdir -p "$GITHUB_WORKSPACE/dist"
235 cp -f bazel-bin/data/opencc-resources.zip "$GITHUB_WORKSPACE/dist/opencc-resources-after.zip"
236
237 - name: Capture resource zip metadata
238 if: steps.safety.outputs.enabled == '1'
239 shell: bash
240 run: |
241 python3 - <<'PY' >> "$GITHUB_STEP_SUMMARY"
242 import hashlib
243 import json
244 import zipfile
245
246 print("## Resource Zip Metadata")
247 print()
248 for label, path in [
249 ("Before", "dist/opencc-resources-before.zip"),
250 ("After", "dist/opencc-resources-after.zip"),
251 ]:
252 data = open(path, "rb").read()
253 with zipfile.ZipFile(path) as archive:
254 manifest_data = archive.read("opencc-resource-manifest.json")
255 manifest = json.loads(manifest_data)
256 print(f"- {label} zip SHA256: `{hashlib.sha256(data).hexdigest()}`")
257 print(
258 f"- {label} manifest SHA256: "
259 f"`{hashlib.sha256(manifest_data).hexdigest()}`"
260 )
261 print(f"- {label} manifest commit: `{manifest.get('commit_id')}`")
262 print(f"- {label} manifest built: `{manifest.get('build_time_utc')}`")
263 print()
264 PY
265
266 - name: Generate diff report
267 if: steps.safety.outputs.enabled == '1'
268 shell: bash
269 run: |
270 mkdir -p dist
271 python3 "$RUNNER_TEMP/opencc-before/data/scripts/opencc_resources_zip_diff.py" \
272 dist/opencc-resources-before.zip \
273 dist/opencc-resources-after.zip \
274 --markdown dist/opencc-resource-diff.md
275 cat dist/opencc-resource-diff.md >> "$GITHUB_STEP_SUMMARY"
276
277 - name: Upload diff report
278 if: steps.safety.outputs.enabled == '1'
279 uses: actions/upload-artifact@v7
280 with:
281 path: dist/opencc-resource-diff.md
282 archive: false
283 if-no-files-found: error