-
-
Notifications
You must be signed in to change notification settings - Fork 34
102 lines (81 loc) · 3.46 KB
/
close-completed-ocwm.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
name: Close issues with no tasks and a specific label
on:
schedule:
- cron: '0 0 15-21 * 0' # Runs at midnight on the third Sunday of the month
repository_dispatch:
types: close-issues-ocwm
jobs:
close-issues:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Node 20
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Get Token
uses: actions/create-github-app-token@v1
id: get_workflow_token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.PRIVATE_KEY }}
- name: Install dependencies
run: npm install @octokit/[email protected]
- name: Close issues
env:
MY_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
MY_LABEL: ${{ vars.OCWM_LABEL }}
OWNER: ${{ vars.ORGANISATION }}
REPO_NAMES: ${{ vars.REPOSITORIES }}
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const octokit = require('@octokit/core').Octokit
const mygithub = new octokit({
request: { fetch: fetch,},
auth: process.env.MY_TOKEN
});
const label = process.env.MY_LABEL;
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);
const dateString = sevenDaysAgo.toISOString().split('T')[0];
const pattern = `label:"${label}" is:open created:<${dateString} repo:${process.env.REPO_NAMES} org:${process.env.OWNER}`;
try {
const response = await mygithub.request('GET /search/issues', {
q: pattern,
per_page: 100,
});
const issues = response.data.items;
console.log(`Number of issues found: ${issues.length}`);
for (const issue of issues) {
console.log('Issue:' + JSON.stringify(issue));
const repo = issue.repository_url.split('/').slice(-1)[0];
const issue_number = issue.number;
console.log('Repo:' + repo);
const taskListRegex = /^\s*- \[[\sX]\]\s+/gm;
const incompleteTaskRegex = /-\s+\[[ ]\]\s+.+/g;
const hasTasks = taskListRegex.test(issue.body);
const hasIncompleteTasks = incompleteTaskRegex.test(issue.body);
if (!hasTasks || !hasIncompleteTasks) {
console.log(`Closing issue: ${issue.number}`);
try {
await mygithub.request(`POST /repos/${process.env.OWNER}/${repo}/issues/${issue.number}/comments`, {
body: 'Closing this issue as all tasks are completed. Thanks for your contributions!',
});
console.log(`POST /repos/${process.env.OWNER}/${repo}/issues/${issue.number}/comments`);
await mygithub.request(`PATCH /repos/${process.env.OWNER}/${repo}/issues/${issue.number}`, {
state: 'closed',
});
console.log(`PATCH /repos/${process.env.OWNER}/${repo}/issues/${issue.number}`);
console.log(`Issue updated`);
} catch (error) {
console.error('Error occurred updating issue:', error);
process.exit(1);
}
}
}
} catch (error) {
console.error('Error occurred:', error.message);
process.exit(1);
}