Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

mobile-fingerprint-label.yml

187 lines7.1 KB

1name: Mobile Fingerprint Label
2​
3# Trusted half of mobile-fingerprint-check.yml. That workflow runs PR code
4# (fork PRs included) with a read-only token and records which platforms'
5# fingerprints changed. This one runs from main with a write token and syncs
6# the "πŸ“± Native Change" label. It never checks out or runs PR code.
7#
8# A PR can edit the check workflow it runs under, so the recorded result is
9# untrusted data: it is validated, and at worst a PR mislabels itself.
10​
11on:
12 workflow_run:
13 workflows: [Mobile Fingerprint Check]
14 types: [completed]
15​
16permissions:
17 contents: read
18​
19jobs:
20 label:
21 name: Sync native change label
22 # A cancelled run was superseded by a newer push, whose run labels instead.
23 if: >-
24 github.event.workflow_run.event == 'pull_request' &&
25 github.event.workflow_run.conclusion == 'success'
26 runs-on: ubuntu-24.04
27 timeout-minutes: 5
28 permissions:
29 actions: read
30 pull-requests: write
31 steps:
32 - id: pr
33 name: Resolve the pull request behind the check
34 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
35 with:
36 script: |
37 const run = context.payload.workflow_run;
38 const { owner, repo } = context.repo;
39​
40 const artifacts = await github.paginate(github.rest.actions.listWorkflowRunArtifacts, {
41 owner,
42 repo,
43 run_id: run.id,
44 per_page: 100,
45 });
46 const results = artifacts.filter(
47 (artifact) => artifact.name === "fingerprint-result" && !artifact.expired,
48 );
49 if (results.length !== 1) {
50 core.info(`Expected one fingerprint-result artifact; found ${results.length}. Skipping.`);
51 return;
52 }
53​
54 // workflow_run.pull_requests and the commit-to-PR lookup are both
55 // empty for fork PRs, so list open PRs by head owner and branch.
56 // Requiring the PR's current head to be the checked commit keeps a
57 // slow run for an older push from overwriting a newer result.
58 const open = await github.paginate(github.rest.pulls.list, {
59 owner,
60 repo,
61 state: "open",
62 head: `${run.head_repository?.owner?.login}:${run.head_branch}`,
63 per_page: 100,
64 });
65 const matching = open.filter(
66 (candidate) =>
67 candidate.state === "open" &&
68 candidate.head.sha === run.head_sha &&
69 candidate.head.ref === run.head_branch &&
70 candidate.head.repo?.full_name === run.head_repository?.full_name,
71 );
72 if (matching.length !== 1) {
73 core.info(`Expected one open PR at ${run.head_sha}; found ${matching.length}. Skipping.`);
74 return;
75 }
76​
77 core.setOutput("artifact_id", String(results[0].id));
78 core.setOutput("pr_number", String(matching[0].number));
79​
80 # Read the archive as bytes; never extract PR-produced files into a
81 # workspace this job runs code from.
82 - id: result
83 name: Read changed platforms
84 if: steps.pr.outputs.pr_number != ''
85 shell: bash
86 env:
87 GH_TOKEN: ${{ github.token }}
88 ARTIFACT_ID: ${{ steps.pr.outputs.artifact_id }}
89 run: |
90 set -euo pipefail
91 gh api "repos/${GITHUB_REPOSITORY}/actions/artifacts/${ARTIFACT_ID}/zip" > "$RUNNER_TEMP/fingerprint-result.zip"
92 changed="$(unzip -p "$RUNNER_TEMP/fingerprint-result.zip" changed-platforms)"
93 if [[ ! "$changed" =~ ^((ios|android)( (ios|android))?)?$ ]]; then
94 echo "Unexpected changed-platforms value '$changed'." >&2
95 exit 1
96 fi
97 echo "changed_platforms=$changed" >> "$GITHUB_OUTPUT"
98​
99 - name: Sync native change label
100 if: steps.pr.outputs.pr_number != ''
101 uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
102 env:
103 PR_NUMBER: ${{ steps.pr.outputs.pr_number }}
104 CHANGED_PLATFORMS: ${{ steps.result.outputs.changed_platforms }}
105 with:
106 script: |
107 const managedLabel = {
108 name: "πŸ“± Native Change",
109 color: "d93f0b",
110 description:
111 "Changes the native fingerprint; merging blocks production OTAs until a new store build ships.",
112 };
113 const nativeChanged = (process.env.CHANGED_PLATFORMS ?? "").trim() !== "";
114 const issueNumber = Number(process.env.PR_NUMBER);
115​
116 try {
117 const { data: existing } = await github.rest.issues.getLabel({
118 owner: context.repo.owner,
119 repo: context.repo.repo,
120 name: managedLabel.name,
121 });
122​
123 if (
124 existing.color !== managedLabel.color ||
125 (existing.description ?? "") !== managedLabel.description
126 ) {
127 await github.rest.issues.updateLabel({
128 owner: context.repo.owner,
129 repo: context.repo.repo,
130 name: managedLabel.name,
131 color: managedLabel.color,
132 description: managedLabel.description,
133 });
134 }
135 } catch (error) {
136 if (error.status !== 404) {
137 throw error;
138 }
139​
140 try {
141 await github.rest.issues.createLabel({
142 owner: context.repo.owner,
143 repo: context.repo.repo,
144 name: managedLabel.name,
145 color: managedLabel.color,
146 description: managedLabel.description,
147 });
148 } catch (createError) {
149 if (createError.status !== 422) {
150 throw createError;
151 }
152 }
153 }
154​
155 const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
156 owner: context.repo.owner,
157 repo: context.repo.repo,
158 issue_number: issueNumber,
159 per_page: 100,
160 });
161 const hasLabel = currentLabels.some((label) => label.name === managedLabel.name);
162​
163 if (nativeChanged && !hasLabel) {
164 await github.rest.issues.addLabels({
165 owner: context.repo.owner,
166 repo: context.repo.repo,
167 issue_number: issueNumber,
168 labels: [managedLabel.name],
169 });
170 } else if (!nativeChanged && hasLabel) {
171 try {
172 await github.rest.issues.removeLabel({
173 owner: context.repo.owner,
174 repo: context.repo.repo,
175 issue_number: issueNumber,
176 name: managedLabel.name,
177 });
178 } catch (removeError) {
179 if (removeError.status !== 404) {
180 throw removeError;
181 }
182 }
183 }
184​
185 core.info(
186 `PR #${issueNumber}: native fingerprint ${nativeChanged ? `changed (${process.env.CHANGED_PLATFORMS})` : "unchanged"}`,
187 );