-
Notifications
You must be signed in to change notification settings - Fork 22
218 lines (188 loc) · 8.28 KB
/
Copy pathauto_assign_issue.yml
File metadata and controls
218 lines (188 loc) · 8.28 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
name: auto_assign_issue
on:
issue_comment:
types: [created]
permissions:
issues: write
pull-requests: read
contents: read
jobs:
assign:
if: github.event.issue.pull_request == null
runs-on: ubuntu-latest
steps:
- name: Assign commenter
uses: actions/github-script@v7
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const issue_number = context.payload.issue.number;
const assignee = context.payload.comment.user.login;
const comment_id = context.payload.comment.id;
const body = (context.payload.comment.body || '').toLowerCase();
// keyword check for auto-assigning
const hasAssignKeyword =
body.includes('assign me') ||
body.includes('assign it to me') ||
body.includes('assign this to me') ||
body.includes('assign this issue to me') ||
body.includes('assign the issue to me');
if (!hasAssignKeyword) {
core.info('No assign keyword found.');
return;
}
// Helper: react to the triggering comment
async function react(content) {
try {
await github.rest.reactions.createForIssueComment({
owner,
repo,
comment_id,
content, // "+1", "confused", "rocket", etc.
});
} catch (e) {
// If reactions aren't permitted for some reason, don't fail the whole workflow.
core.info(`Could not add reaction (${content}): ${e.message}`);
}
}
// 1) Don't reassign if already assigned
const current = context.payload.issue.assignees?.map(a => a.login) || [];
if (current.length > 0) {
core.info(`Already assigned to: ${current.join(', ')}`);
await react('confused');
return;
}
// Helper: format issue as a single markdown link line (prevents duplicate link/title rendering)
function formatIssueLine(i) {
return `- [#${i.number} — ${i.title}](${i.url})`;
}
// Helper: suggest other unblocked issues
async function getSuggestions(limit = 5) {
// open issues, unassigned, not blocked
const q = `repo:${owner}/${repo} is:issue is:open no:assignee -label:blocked`;
const res = await github.rest.search.issuesAndPullRequests({ q, per_page: limit });
return (res.data.items || []).map(i => ({
number: i.number,
title: i.title,
url: i.html_url,
}));
}
// 2) Block if label "blocked" exists (and suggest alternatives)
const labels = (context.payload.issue.labels || [])
.map(l => (typeof l === 'string' ? l : l.name))
.filter(Boolean)
.map(l => l.toLowerCase());
if (labels.includes('blocked')) {
const suggestions = await getSuggestions(5);
const suggestionText = suggestions.length
? suggestions.map(formatIssueLine).join('\n')
: '_No unblocked, unassigned issues found right now._';
await github.rest.issues.createComment({
owner, repo, issue_number,
body: [
`⛔ Sorry @${assignee} — this issue is currently **blocked** and can’t be claimed yet.`,
``,
`Here are some other **unblocked** issues you can pick up instead:`,
suggestionText,
].join('\n'),
});
await react('confused');
return;
}
// 3) Capacity check:
// If the candidate has 2 assigned issues that do NOT have PRs, then do not assign a new issue.
//
// "Linked" here means: a PR cross-references the issue (GitHub timeline cross-reference),
// and the PR is authored by the candidate.
async function hasLinkedPRForIssue(issueNum) {
const query = `
query($owner: String!, $repo: String!, $number: Int!) {
repository(owner: $owner, name: $repo) {
issueOrPullRequest(number: $number) {
__typename
... on Issue {
timelineItems(last: 50, itemTypes: [CROSS_REFERENCED_EVENT]) {
nodes {
__typename
... on CrossReferencedEvent {
source {
__typename
... on PullRequest {
number
url
state
isDraft
author { login }
}
}
}
}
}
}
}
}
}
`;
const data = await github.graphql(query, { owner, repo, number: issueNum });
const iop = data?.repository?.issueOrPullRequest;
if (!iop || iop.__typename !== 'Issue') return false;
const nodes = iop.timelineItems?.nodes || [];
for (const n of nodes) {
if (n?.__typename !== 'CrossReferencedEvent') continue;
const pr = n?.source;
if (pr?.__typename !== 'PullRequest') continue;
if ((pr?.author?.login || '').toLowerCase() === assignee.toLowerCase()) {
// Counts any PR state (OPEN/MERGED/CLOSED) as "has a PR for it".
return true;
}
}
return false;
}
async function getOpenAssignedIssuesForUser() {
// Find open issues assigned to this user in this repo
const q = `repo:${owner}/${repo} is:issue is:open assignee:${assignee}`;
const res = await github.rest.search.issuesAndPullRequests({ q, per_page: 20 });
return (res.data.items || []).map(i => ({
number: i.number,
title: i.title,
url: i.html_url,
}));
}
const assignedIssues = await getOpenAssignedIssuesForUser();
// Count assigned issues that do NOT have PRs by this assignee
const issuesWithoutPR = [];
for (const it of assignedIssues) {
const hasPR = await hasLinkedPRForIssue(it.number);
if (!hasPR) issuesWithoutPR.push(it);
if (issuesWithoutPR.length >= 2) break; // early exit
}
if (issuesWithoutPR.length >= 2) {
const noPRText = issuesWithoutPR
.slice(0, 10)
.map(formatIssueLine)
.join('\n');
await github.rest.issues.createComment({
owner, repo, issue_number,
body: [
`⛔ Sorry @${assignee} — you already have **2 or more** assigned issues with **no pull request(s)**.`,
``,
`Please open a PR for one of your assigned issues before claiming a new one:`,
noPRText || '_Could not list the issues (unexpected)._',
``,
`Tip: opening a **draft PR** is fine—once a PR is linked, you'll be able to claim another issue.`,
].join('\n'),
});
await react('confused');
return;
}
// 4) Assign
await github.rest.issues.addAssignees({
owner, repo, issue_number,
assignees: [assignee],
});
await github.rest.issues.createComment({
owner, repo, issue_number,
body: `🎉 Thank you for your interest in contributing!\n\nThe issue has been assigned to @${assignee}. Happy coding!`,
});
await react('rocket');