Skip to content

Shoot_Duck_Game

Shoot_Duck_Game #1265

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'
});