forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
129 lines (116 loc) · 5.92 KB
/
Copy pathconflict.yaml
File metadata and controls
129 lines (116 loc) · 5.92 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
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@v8
with:
script: |
async function getPullRequestList() {
return await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
sort: 'created',
per_page: 100,
});
}
async function getPullRequest(prNumber) {
return await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
}
async function addLabel(labels, issue_number) {
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
labels: labels,
});
}
async function removeLabel(labels, issue_number) {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue_number,
name: labels,
});
}
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}`);
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;
}
}
}
}
}
}
}
}
checkPullRequests();