-
Notifications
You must be signed in to change notification settings - Fork 13
83 lines (72 loc) · 3.39 KB
/
close-stale-needs-more-info.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
name: 'Close Issues with Stale Needs More Info Label'
on:
schedule:
# Run this workflow at 00:00 every day
- cron: '0 0 * * *'
workflow_dispatch:
permissions:
issues: write
contents: read
jobs:
close-stale-issues:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Close Stale Issues with Needs More Info Label and Comment
uses: actions/github-script@v6
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const labelName = 'needs-more-info';
const excludedLabel = 'enhancement';
const staleDays = 14;
const closingComment = "This issue has been automatically closed due to inactivity and having the 'needs-more-info' label for more than 14 days. If the issue still persists, please reopen the issue with the requested information.";
const currentDate = new Date();
const issues = await github.paginate(github.rest.issues.listForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
labels: labelName + ', -' + excludedLabel, // Excludes issues with 'enhancement' label
state: 'open',
});
for (const issue of issues) {
let labelAddedDate = null;
// Fetch events to find when the label was added
const events = await github.paginate(github.rest.issues.listEventsForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
});
// Look for the event where the label was added and capture the date
for (const event of events) {
if (event.event === 'labeled' && event.label.name === labelName) {
labelAddedDate = new Date(event.created_at);
break;
}
}
if (labelAddedDate) {
const issueUpdatedDate = new Date(issue.updated_at);
const labelAddedTime = labelAddedDate.getTime();
const issueUpdatedTime = issueUpdatedDate.getTime();
const diffTime = Math.abs(currentDate - labelAddedDate);
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
// Check if the issue was updated after the label was added and if it has been more than 'staleDays' since the label was added
if (issueUpdatedTime > labelAddedTime && diffDays > staleDays) {
// Post a closing comment before closing the issue
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body: closingComment,
});
// Close the issue
await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
state: 'closed',
});
console.log(`Issue #${issue.number} has been closed with a comment as it has had the '${labelName}' label for more than ${staleDays} days and was updated since the label was applied.`);
}
}
}