-
Notifications
You must be signed in to change notification settings - Fork 839
77 lines (65 loc) · 2.92 KB
/
prevent_multiple_pr.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
name: Close Issue if Opener has Opened Pull Requests
on:
pull_request_target:
types:
- opened
jobs:
close_pull_request:
runs-on: ubuntu-latest
steps:
- name: Check if opener has multiple open pull requests
id: check_open_pull_requests
uses: actions/github-script@v4
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const pullRequestNumber = context.payload.pull_request.number;
const pullRequestOpener = context.payload.pull_request.user.login;
const previousPullRequestsResponse = await github.pulls.list({
owner,
repo,
state: 'open'
});
const previousOpenPullRequests = previousPullRequestsResponse.data.filter(pr => pr.user.login === pullRequestOpener && pr.number !== pullRequestNumber);
const previousOpenPullRequestNumbers = previousOpenPullRequests.map(pr => `#${pr.number}`);
const openerName = context.payload.pull_request.user.login;
const closePullRequest = previousOpenPullRequests.length > 0;
console.log(`Close pull request: ${closePullRequest}`);
if (closePullRequest) {
const comment = `Hey @${openerName}, you can't open another pull request before completing the previous one 😀\nYou already have the following ${previousOpenPullRequests.length} open pull request(s):\n${previousOpenPullRequestNumbers.join('\n')}`;
core.exportVariable('comment_body', comment);
core.setOutput('close_pull_request', true);
} else {
core.exportVariable('comment_body', '');
core.setOutput('close_pull_request', false);
return; // Return and skip further steps
}
- name: Close the pull request and add a comment
if: always() && ${{ needs.check_open_pull_requests.outputs.close_pull_request }}
uses: actions/github-script@v4
with:
github-token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const pullRequestNumber = context.payload.pull_request.number;
const comment = process.env.comment_body;
if (comment.trim() === '') {
console.log('Comment body is empty. Skipping comment creation.');
return;
}
const closeComment = `${comment}`;
await github.issues.createComment({
owner,
repo,
issue_number: pullRequestNumber,
body: closeComment
});
await github.pulls.update({
owner,
repo,
pull_number: pullRequestNumber,
state: 'closed'
});