-
Notifications
You must be signed in to change notification settings - Fork 3
87 lines (76 loc) · 2.94 KB
/
assign.yml
File metadata and controls
87 lines (76 loc) · 2.94 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
name: Assign on Comment
on:
issue_comment:
types: [created]
permissions:
issues: write
pull-requests: write
jobs:
assign-on-comment:
runs-on: ubuntu-latest
if: github.event.issue.pull_request == null
steps:
- name: Assign users based on comment
uses: actions/github-script@v7
with:
script: |
const commentBody = context.payload.comment.body.trim();
const commenter = context.payload.comment.user.login;
const issueNumber = context.payload.issue.number;
const repo = context.repo.repo;
const owner = context.repo.owner;
const isMaintainer = async (username) => {
const { data: collaborators } = await github.rest.repos.listCollaborators({
owner,
repo,
affiliation: 'direct'
});
return collaborators.some(user => user.login === username);
};
const postComment = async (body) => {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body
});
};
const commandMatch = commentBody.match(/^\/assign\s*(.*)?$/);
if (!commandMatch) return;
const mentionedUser = commandMatch[1]?.trim();
const commenterIsMaintainer = await isMaintainer(commenter);
if (mentionedUser) {
if (!mentionedUser.startsWith('@')) {
await postComment("❌ Invalid syntax. Use `/assign @username` to assign someone.");
return;
}
const targetUser = mentionedUser.replace('@', '');
if (!commenterIsMaintainer) {
await postComment("❌ You must be a project maintainer to assign others.");
return;
}
try {
await github.rest.issues.addAssignees({
owner,
repo,
issue_number: issueNumber,
assignees: [targetUser]
});
await postComment(`✅ Assigned @${targetUser} as requested by maintainer @${commenter}.`);
} catch (err) {
await postComment(`❌ Failed to assign @${targetUser}. Maybe they don't have permission or aren't a contributor.`);
}
} else {
const assignee = commenter;
try {
await github.rest.issues.addAssignees({
owner,
repo,
issue_number: issueNumber,
assignees: [assignee]
});
await postComment(`✅ Assigned @${assignee} to this issue.`);
} catch (err) {
await postComment(`❌ Failed to assign @${assignee}. Do you have permission or are you a contributor?`);
}
}