-
Notifications
You must be signed in to change notification settings - Fork 141
83 lines (69 loc) Β· 3.02 KB
/
prevent_multiple_issues.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 Issue if Opener has Opened Issues
on:
issues:
types:
- opened
jobs:
close_issue:
runs-on: ubuntu-latest
steps:
- name: Check if opener has multiple open issues
id: check_open_issues
uses: actions/github-script@v4
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
const { context, github } = require('@actions/github');
const core = require('@actions/core');
const owner = context.repo.owner;
const repo = context.repo.repo;
const issueNumber = context.issue.number;
const issueOpener = context.payload.issue.user.login;
const previousIssuesResponse = await github.request('GET /repos/{owner}/{repo}/issues', {
owner,
repo,
state: 'open',
creator: issueOpener
});
const previousOpenIssues = previousIssuesResponse.data.filter(issue => issue.number !== issueNumber && !issue.pull_request);
const previousOpenIssueNumbers = previousOpenIssues.map(issue => `#${issue.number}`);
const openerName = context.payload.issue.user.login;
const closeIssue = previousOpenIssues.length > 0;
console.log(`Close issue: ${closeIssue}`);
if (closeIssue) {
const comment = `Hey @${openerName}, You can't have another issue before completing the previous one π \n you already have the following ${previousOpenIssues.length} open issues π ! :\n\n${previousOpenIssueNumbers.join('\n')}`;
core.exportVariable('comment_body', comment); // Export the variable
core.setOutput('close_issue', true);
} else {
core.exportVariable('comment_body', '');
core.setOutput('close_issue', false);
}
- name: Close the issue and add a comment
if: always() && ${{ needs.check_open_issues.outputs.close_issue }}
uses: actions/github-script@v4
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
const { context, github } = require('@actions/github');
const core = require('@actions/core');
const owner = context.repo.owner;
const repo = context.repo.repo;
const issueNumber = context.issue.number;
const comment = process.env.comment_body; // Retrieve the exported variable
if (comment.trim() === '') {
console.log('Comment body is empty. Skipping comment creation.');
return;
}
const closeComment = `${comment}`;
await github.issues.createComment({
owner,
repo,
issue_number: issueNumber,
body: closeComment
});
await github.issues.update({
owner,
repo,
issue_number: issueNumber,
state: 'closed'
});