-
Notifications
You must be signed in to change notification settings - Fork 723
138 lines (120 loc) · 4.89 KB
/
label-waiting-for-response.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
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
name: Auto Label Issues
on:
issue_comment:
types: [created]
workflow_dispatch:
inputs:
label-all-issues:
description: 'Add label to all open issues'
type: boolean
default: false
required: false
jobs:
add-label-single:
runs-on: ubuntu-latest
if: |
github.event_name == 'issue_comment' &&
github.event.issue.user.login != github.event.comment.user.login
steps:
- name: Check if commenter is maintainer
id: check-maintainer
uses: actions/github-script@v7
with:
script: |
const response = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: context.payload.comment.user.login
});
const isMaintianer = ['admin', 'write'].includes(response.data.permission);
return isMaintianer;
- name: Add waiting-for-response label
if: steps.check-maintainer.outputs.result == 'true'
uses: actions/github-script@v7
with:
script: |
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
labels: ['waiting-for-response']
});
add-label-all:
runs-on: ubuntu-latest
if: |
github.event_name == 'workflow_dispatch' &&
github.event.inputs.label-all-issues == 'true'
steps:
- name: Process all open issues
uses: actions/github-script@v7
with:
script: |
async function isMaintianer(username) {
try {
const response = await github.rest.repos.getCollaboratorPermissionLevel({
owner: context.repo.owner,
repo: context.repo.repo,
username: username
});
return ['admin', 'write'].includes(response.data.permission);
} catch (error) {
console.error(`Error checking permissions for ${username}:`, error);
return false;
}
}
async function getLastComment(issueNumber) {
try {
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
per_page: 100
});
// Return the most recent comment, or null if no comments
return comments.length > 0 ? comments[comments.length - 1] : null;
} catch (error) {
console.error(`Error fetching comments for issue #${issueNumber}:`, error);
return null;
}
}
// Get all open issues
const issues = await github.paginate(github.rest.issues.listForRepo, {
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
per_page: 100
});
for (const issue of issues) {
try {
console.log(`Processing issue #${issue.number}...`);
// Get the last comment
const lastComment = await getLastComment(issue.number);
// Skip if no comments
if (!lastComment) {
console.log(`No comments found on issue #${issue.number}, skipping`);
continue;
}
// Check if last commenter is a maintainer
const lastCommenterIsMaintainer = await isMaintianer(lastComment.user.login);
// Skip if last commenter is not a maintainer
if (!lastCommenterIsMaintainer) {
console.log(`Last comment on issue #${issue.number} is not from a maintainer, skipping`);
continue;
}
// Skip if last commenter is the issue author
if (lastComment.user.login === issue.user.login) {
console.log(`Last comment on issue #${issue.number} is from the issue author, skipping`);
continue;
}
// Add the label
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
labels: ['waiting-for-response']
});
console.log(`Added label to issue #${issue.number}`);
} catch (error) {
console.error(`Error processing issue #${issue.number}:`, error);
}
}