-
-
Notifications
You must be signed in to change notification settings - Fork 40
102 lines (87 loc) · 3.25 KB
/
assign-issue.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: Issue Assignment Manager
on:
issue_comment:
types:
- created
jobs:
assign-issue:
runs-on: ubuntu-latest
steps:
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Create assignment script
run: |
cat > assign.js << 'EOL'
const github = require('@actions/github');
const core = require('@actions/core');
async function handleAssignCommand() {
try {
const token = process.env.GITHUB_TOKEN;
const octokit = github.getOctokit(token);
const context = github.context;
const { issue, comment } = context.payload;
const { owner, repo } = context.repo;
// Check if the comment contains /assign
const commentBody = comment.body.trim();
if (commentBody !== '/assign') {
console.log('Comment does not contain /assign. Skipping.');
return;
}
const commenter = comment.user.login;
// Fetch all open issues assigned to the commenter
const { data: issues } = await octokit.rest.issues.listForRepo({
owner,
repo,
state: 'open',
assignee: commenter,
});
if (issues.length > 0) {
console.log(`${commenter} is already assigned to an issue.`);
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: `@${commenter}, you are already assigned to an issue. You can only work on one issue at a time.`,
});
return;
}
// Check if the issue is already assigned to someone
if (issue.assignees && issue.assignees.length > 0) {
console.log(`Issue #${issue.number} is already assigned.`);
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: 'This issue is already assigned to someone else.',
});
return;
}
// Assign the issue to the commenter
await octokit.rest.issues.addAssignees({
owner,
repo,
issue_number: issue.number,
assignees: [commenter],
});
console.log(`Assigned issue #${issue.number} to @${commenter}.`);
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: `@${commenter}, the issue has been assigned to you. Happy coding!`,
});
} catch (error) {
console.error(error);
core.setFailed(error.message);
}
}
handleAssignCommand();
EOL
- name: Install dependencies
run: npm install @actions/github @actions/core
- name: Handle /assign command
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: node assign.js