Skip to content

Commit 6513163

Browse files
committed
fix(ci): tolerate periphery comment permission denials
1 parent c05766b commit 6513163

2 files changed

Lines changed: 65 additions & 11 deletions

File tree

.github/workflows/ios-periphery-comment.yml

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -464,19 +464,31 @@ jobs:
464464
return;
465465
}
466466
467-
if (existing) {
468-
await github.rest.issues.updateComment({
467+
const writeComment = async () => {
468+
if (existing) {
469+
await github.rest.issues.updateComment({
470+
owner,
471+
repo,
472+
comment_id: existing.id,
473+
body,
474+
});
475+
return;
476+
}
477+
478+
await github.rest.issues.createComment({
469479
owner,
470480
repo,
471-
comment_id: existing.id,
481+
issue_number: livePull.data.number,
472482
body,
473483
});
474-
return;
475-
}
484+
};
476485
477-
await github.rest.issues.createComment({
478-
owner,
479-
repo,
480-
issue_number: livePull.data.number,
481-
body,
482-
});
486+
try {
487+
await writeComment();
488+
} catch (error) {
489+
if (error && typeof error === "object" && error.status === 403) {
490+
core.warning(`Skipping Periphery PR comment for #${pr.number}; GitHub token cannot write issue comments for this workflow_run.`);
491+
return;
492+
}
493+
throw error;
494+
}

test/scripts/ios-periphery-comment-workflow.test.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ async function runCommenter(
147147
archiveData: Buffer,
148148
options: {
149149
existingComments?: ExistingComment[];
150+
commentErrorStatus?: number;
150151
liveHeadSha?: string;
151152
liveHeadShaAfter?: string;
152153
runHeadSha?: string;
@@ -187,9 +188,19 @@ async function runCommenter(
187188
issues: {
188189
listComments() {},
189190
async createComment(params: { body: string }) {
191+
if (options.commentErrorStatus) {
192+
throw Object.assign(new Error("comment write failed"), {
193+
status: options.commentErrorStatus,
194+
});
195+
}
190196
createdBodies.push(params.body);
191197
},
192198
async updateComment(params: { body: string }) {
199+
if (options.commentErrorStatus) {
200+
throw Object.assign(new Error("comment write failed"), {
201+
status: options.commentErrorStatus,
202+
});
203+
}
193204
updatedBodies.push(params.body);
194205
},
195206
},
@@ -924,6 +935,37 @@ describe("iOS Periphery comment workflow", () => {
924935
expect(result.createdBodies[0]?.length).toBeLessThanOrEqual(60_000);
925936
});
926937

938+
it("warns without failing when workflow_run token cannot write comments", async () => {
939+
const archive = makeZip({
940+
"periphery.json": JSON.stringify([
941+
{
942+
kind: "function",
943+
location: "Sources/Test.swift:12",
944+
name: "unused",
945+
},
946+
]),
947+
"periphery.status": "1\n",
948+
});
949+
const result = await runCommenter(
950+
{
951+
expired: false,
952+
id: 77,
953+
name: ARTIFACT_NAME,
954+
size_in_bytes: archive.length,
955+
},
956+
archive,
957+
{
958+
commentErrorStatus: 403,
959+
},
960+
);
961+
962+
expect(result.createdBodies).toEqual([]);
963+
expect(result.updatedBodies).toEqual([]);
964+
expect(result.core.warnings).toContain(
965+
"Skipping Periphery PR comment for #123; GitHub token cannot write issue comments for this workflow_run.",
966+
);
967+
});
968+
927969
it("does not overwrite a marker comment owned by another bot", async () => {
928970
const archive = makeZip({
929971
"periphery.json": JSON.stringify([

0 commit comments

Comments
 (0)