Skip to content

Commit 4f470a2

Browse files
authored
fix(ci): do not comment from linter.yml in forks and enable exponential backoff for workflow functions in common.mjs (#1523)
1 parent be44325 commit 4f470a2

File tree

2 files changed

+70
-33
lines changed

2 files changed

+70
-33
lines changed

.github/scripts/common.mjs

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,54 @@ export async function hidePriorCommentsWithPrefix({
66
resolved = true,
77
user = 'github-actions[bot]',
88
}) {
9-
const comments = await github.rest.issues.listComments({
10-
owner: context.repo.owner,
11-
repo: context.repo.repo,
12-
issue_number: context.issue.number,
13-
});
9+
const comments = await withRetry(() =>
10+
github.rest.issues.listComments({
11+
owner: context.repo.owner,
12+
repo: context.repo.repo,
13+
issue_number: context.issue.number,
14+
})
15+
);
16+
await exec.exec('sleep 0.5s');
1417
for (const comment of comments.data) {
15-
const isHidden = (await github.graphql(`
16-
query($nodeId: ID!) {
17-
node(id: $nodeId) {
18-
... on IssueComment {
19-
isMinimized
18+
const commentData = await withRetry(() =>
19+
github.graphql(`
20+
query($nodeId: ID!) {
21+
node(id: $nodeId) {
22+
... on IssueComment {
23+
isMinimized
24+
}
2025
}
2126
}
22-
}
23-
`, { nodeId: comment.node_id }))?.node?.isMinimized;
27+
`, { nodeId: comment.node_id })
28+
);
2429
await exec.exec('sleep 0.5s');
30+
const isHidden = commentData?.node?.isMinimized;
2531
if (isHidden) { continue; }
2632
if (
2733
comment.user.login === user &&
2834
comment.body.startsWith(prefix)
2935
) {
3036
console.log('Comment node_id:', comment.node_id);
31-
console.log(await github.graphql(`
32-
mutation($subjectId: ID!, $classifier: ReportedContentClassifiers!) {
33-
minimizeComment(input: {
34-
subjectId: $subjectId,
35-
classifier: $classifier
36-
}) {
37-
minimizedComment {
38-
isMinimized
39-
minimizedReason
37+
const commentStatus = await withRetry(() =>
38+
github.graphql(`
39+
mutation($subjectId: ID!, $classifier: ReportedContentClassifiers!) {
40+
minimizeComment(input: {
41+
subjectId: $subjectId,
42+
classifier: $classifier
43+
}) {
44+
minimizedComment {
45+
isMinimized
46+
minimizedReason
47+
}
4048
}
4149
}
42-
}
43-
`, {
44-
subjectId: comment.node_id,
45-
classifier: resolved ? 'RESOLVED' : 'OUTDATED',
46-
}));
50+
`, {
51+
subjectId: comment.node_id,
52+
classifier: resolved ? 'RESOLVED' : 'OUTDATED',
53+
})
54+
);
4755
await exec.exec('sleep 0.5s');
56+
console.log(commentStatus);
4857
}
4958
}
5059
}
@@ -55,10 +64,35 @@ export async function createComment({
5564
exec, // injected by GitHub
5665
body = '',
5766
}) {
58-
await github.rest.issues.createComment({
59-
owner: context.repo.owner,
60-
repo: context.repo.repo,
61-
issue_number: context.issue.number,
62-
body: body,
63-
});
67+
await withRetry(() =>
68+
github.rest.issues.createComment({
69+
owner: context.repo.owner,
70+
repo: context.repo.repo,
71+
issue_number: context.issue.number,
72+
body: body,
73+
})
74+
);
75+
}
76+
77+
/** @param fn {() => Promise<any>} */
78+
async function withRetry(fn, maxRetries = 3, baseDelayMs = 1500) {
79+
let lastError;
80+
for (let attempt = 1; attempt <= maxRetries; attempt += 1) {
81+
try {
82+
return await fn();
83+
} catch (error) {
84+
// Don't retry on 4xx errors (client errors), only on 5xx or network issues
85+
if (error.status && error.status >= 400 && error.status < 500) {
86+
throw error;
87+
}
88+
lastError = error;
89+
90+
// Exponential backoff
91+
const delay = baseDelayMs * Math.pow(2, attempt - 1);
92+
console.log(`Attempt ${attempt} failed, retrying in ${delay / 1000}s...`);
93+
await new Promise((resolve) => setTimeout(resolve, delay));
94+
}
95+
}
96+
// Did not produce results after multiple retries
97+
throw lastError;
6498
}

.github/workflows/linter.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,10 @@ jobs:
8686
!cancelled() &&
8787
steps.changed-files.conclusion == 'success' &&
8888
github.event_name == 'pull_request' &&
89-
github.event_name != 'pull_request_target'
89+
(
90+
github.event.pull_request.head.repo.fork == true ||
91+
github.event.pull_request.head.repo.full_name != github.repository
92+
)
9093
)
9194
env:
9295
ALL_CHANGED_FILES: ${{ steps.changed-files.outputs.all_changed_files }}

0 commit comments

Comments
 (0)