forked from SUGAM-ARORA/UniCollab
-
Notifications
You must be signed in to change notification settings - Fork 0
104 lines (88 loc) · 2.91 KB
/
close-old-prs.yml
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
name: Unassign Stale Issues
on:
schedule:
- cron: '0 0 * * *' # Runs daily at midnight
permissions:
issues: write
jobs:
unassign_stale_issues:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '16'
- name: Unassign stale issues
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
npm install axios
node -e "
const axios = require('axios');
const token = process.env.GITHUB_TOKEN;
const owner = 'YOUR_GITHUB_USERNAME_OR_ORG';
const repo = 'YOUR_REPOSITORY_NAME';
const daysStale = 40;
const headers = {
headers: {
Authorization: `token ${token}`,
'Content-Type': 'application/json',
},
};
async function getStaleIssues() {
const now = new Date();
const staleDate = new Date(now.setDate(now.getDate() - daysStale));
const issues = await axios.get(\`https://api.github.com/repos/${owner}/${repo}/issues?state=open&per_page=100\`, headers);
return issues.data.filter(issue => {
const lastUpdate = new Date(issue.updated_at);
return lastUpdate < staleDate && issue.assignees.length > 0;
});
}
async function unassignAndReassign(issue) {
const assignees = issue.assignees.map(assignee => assignee.login);
const nextAssignee = issue.body.match(/@(\w+)/g)?.[1];
if (nextAssignee) {
await axios.post(
\`https://api.github.com/repos/${owner}/${repo}/issues/${issue.number}/assignees\`,
{
assignees: [nextAssignee.replace('@', '')],
},
headers
);
await axios.delete(
\`https://api.github.com/repos/${owner}/${repo}/issues/${issue.number}/assignees\`,
{
data: {
assignees,
},
headers: {
Authorization: `token ${token}`,
'Content-Type': 'application/json',
},
}
);
} else {
await axios.delete(
\`https://api.github.com/repos/${owner}/${repo}/issues/${issue.number}/assignees\`,
{
data: {
assignees,
},
headers: {
Authorization: `token ${token}`,
'Content-Type': 'application/json',
},
}
);
}
}
async function main() {
const staleIssues = await getStaleIssues();
for (const issue of staleIssues) {
await unassignAndReassign(issue);
}
}
main().catch(error => console.error(error));
"