forked from ankidroid/Anki-Android
-
-
Notifications
You must be signed in to change notification settings - Fork 0
178 lines (160 loc) · 8.03 KB
/
conflict.yaml
File metadata and controls
178 lines (160 loc) · 8.03 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
176
177
178
name: 🛠️ Conflict Scan
on:
push:
schedule:
- cron: "0 * * * *"
jobs:
scan_conflicts:
# Do not run the scheduled jobs on forks
if: (github.event_name == 'schedule' && github.repository == 'ankidroid/Anki-Android') || (github.event_name != 'schedule')
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: Check for conflicts and label conflict
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
// Helper function for retrying GitHub API calls
async function withRetry(apiCall, operation = 'API call') {
const retries = 3;
const retryInitialDelay = 5000;
for (let attempt = 1; attempt <= retries; attempt++) {
try {
return await apiCall();
} catch (error) {
// Don't retry client errors (4xx)
if (error.status >= 400 && error.status < 500) {
throw error;
}
// If this is the last attempt, throw the error
if (attempt === retries) {
throw new Error(`${operation} failed after ${retries} attempts: ${error.message}`);
}
// Wait with linear backoff before retrying
const delay = retryInitialDelay * attempt;
console.warn(
`${operation} failed (attempt ${attempt}/${retries}). Retrying in ${delay}ms... ` +
`Error: ${error.status} ${error.message}`
);
await wait(delay);
}
}
}
async function getPullRequestList() {
return withRetry( () =>
github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
sort: 'created',
per_page: 100,
}),
"List pull requests"
);
}
async function getPullRequest(prNumber) {
return withRetry(() =>
github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
}),
`Get PR #${prNumber}`
);
}
async function addLabel(labels, issue_number) {
return withRetry(() =>
github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
labels: labels,
}),
`Add labels to PR #${issue_number}`
);
}
async function removeLabel(labels, issue_number) {
return withRetry(() =>
github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
name: labels,
}),
`Remove label from PR #${issue_number}`
);
}
async function wait(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
const maxIterations = 10;
async function checkPullRequests() {
const pullRequestList = await getPullRequestList();
if (pullRequestList.data !== null && pullRequestList.data.length > 0) {
for (let pullRequest of pullRequestList.data) {
const prNumber = pullRequest.number;
console.log(`Checking PR #${prNumber}`);
try {
let pullRequestData;
let iterations = 0;
do {
pullRequestData = await getPullRequest(prNumber);
// introduce 1-second delay before the next check
await wait(1000);
iterations++;
// Check for terminal conditions
if (pullRequestData.data.mergeable_state !== 'unknown' || iterations >= maxIterations) {
break;
}
} while (pullRequestData.data.mergeable_state === 'unknown');
if (pullRequestData.data.mergeable_state === 'dirty') {
console.log(`Conflict exists in PR #${prNumber}`);
if (pullRequestData.data.labels.find(label => label.name === 'Has Conflicts')) {
console.log(`'Has Conflicts' label already exists on PR #${prNumber}`);
} else {
// Only add label if PR is not already stale to prevent resetting stale timer
if (!pullRequestData.data.labels.find(label => label.name === 'Stale')) {
console.log(`Adding 'Has Conflicts' label to PR #${prNumber} (not stale)`);
await addLabel(['Has Conflicts'], prNumber);
} else {
console.warn(`PR #${prNumber} is stale, skipping label addition to prevent timer reset`);
}
}
} else if (pullRequestData.data.mergeable_state === 'clean') {
// if PR has no conflicts, remove the label
if (pullRequestData.data.labels.find(label => label.name === 'Has Conflicts')) {
console.log(`Removing 'Has Conflicts' label from PR #${prNumber}`);
await removeLabel(['Has Conflicts'], prNumber);
}
} else if (pullRequestData.data.mergeable_state === 'unstable') {
console.log(`PR #${prNumber} is unstable`);
const mergeable = pullRequestData.data.mergeable;
if (mergeable) {
console.log(`PR #${prNumber} is mergeable`);
// if PR has no conflicts, remove the label
if (pullRequestData.data.labels.find(label => label.name === 'Has Conflicts')) {
console.log(`Removing 'Has Conflicts' label from PR #${prNumber}`);
try {
await removeLabel(['Has Conflicts'], prNumber);
} catch(err) {
if (err.status === 404) {
console.warn("(Has Conflicts) label no longer found when trying to remove it");
} else {
console.log("Unexpected error while removing (Has Conflicts) label: " + err);
throw err;
}
}
}
}
}
} catch (err) {
console.error(`Error while processing PR #${prNumber}`);
console.error(err);
throw err;
}
}
}
}
checkPullRequests();