From 85f82065a870f44e67b748697bf54b53256c34e2 Mon Sep 17 00:00:00 2001 From: simar7 <1254783+simar7@users.noreply.github.com> Date: Thu, 14 Sep 2023 21:44:12 -0600 Subject: [PATCH] Create auto-close-issues.yml (#2104) --- .github/workflows/auto-close-issues.yml | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 .github/workflows/auto-close-issues.yml diff --git a/.github/workflows/auto-close-issues.yml b/.github/workflows/auto-close-issues.yml new file mode 100644 index 0000000000..beaa0bfe54 --- /dev/null +++ b/.github/workflows/auto-close-issues.yml @@ -0,0 +1,46 @@ +name: Auto-close issues + +on: + issues: + types: [opened] + +jobs: + close_issue: + runs-on: ubuntu-latest + steps: + - name: Close issue if user does not have write or admin permissions + uses: actions/github-script@v6 + with: + script: | + // Get the issue creator's username + const issueCreator = context.payload.issue.user.login; + + // Check the user's permissions for the repository + const repoPermissions = await github.rest.repos.getCollaboratorPermissionLevel({ + owner: context.repo.owner, + repo: context.repo.repo, + username: issueCreator + }); + + const permission = repoPermissions.data.permission; + + // If the user does not have write or admin permissions, leave a comment and close the issue + if (permission !== 'write' && permission !== 'admin') { + const commentBody = "Please see https://aquasecurity.github.io/trivy/latest/community/contribute/issue/"; + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.issue.number, + body: commentBody + }); + + await github.rest.issues.update({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.issue.number, + state: 'closed', + state_reason: 'not_planned' + }); + + console.log(`Issue #${context.payload.issue.number} closed because ${issueCreator} does not have sufficient permissions.`); + }