From fc2c39dcaf151d0c626c340b251c190640871d9e Mon Sep 17 00:00:00 2001 From: Mahmoud Mazouz Date: Thu, 25 Jan 2024 15:26:21 +0100 Subject: [PATCH] feat: Add enforce-linking-issues workflow --- .github/workflows/enforce-linking-issues.yml | 63 ++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 .github/workflows/enforce-linking-issues.yml diff --git a/.github/workflows/enforce-linking-issues.yml b/.github/workflows/enforce-linking-issues.yml new file mode 100644 index 0000000000..72b91f6636 --- /dev/null +++ b/.github/workflows/enforce-linking-issues.yml @@ -0,0 +1,63 @@ +name: Enforce linking issues to pull requests + +on: + pull_request: + types: [opened, edited, labeled] + workflow_call: + +defaults: + run: + shell: bash + +jobs: + main: + name: Check if pull request has a linked issue + runs-on: ubuntu-latest + steps: + - name: Count closing issue references + id: has-closing-issue + uses: actions/github-script@v7 + with: + result-encoding: string + script: | + const query = `query ($owner: String!, $name: String!, $number: Int!) { + repository(owner: $owner, name: $name) { + pullRequest(number: $number) { + closingIssuesReferences(first: 100) { + totalCount + } + } + } + }`; + + const reply = await github.graphql(query, { + owner: context.repo.owner, + name: context.repo.repo, + number: context.payload.pull_request.number + }); + + return reply + .repository + .pullRequest + .closingIssuesReferences + .totalCount > 0; + + - if: ${{ steps.has-closing-issue.outputs.result != 'true' }} + name: Suggest that the contributor linked an issue + uses: actions/github-script@v7 + with: + github-token: ${{ secrets.TOKEN_BOT_WORKFLOW }} + script: | + const login = "${{ github.event.pull_request.user.login }}"; + const syntaxUrl = "https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue"; + const message = `@${login} Please consider linking this pull request to an issue using \`Closes #ISSUE-NUMBER\` [syntax](syntaxUrl), \ + especially if this pull request contains a bugfix, an enchancement or a new feature.`; + + github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number, + body: message, + }); + + core.setFailed("This pull request has no linked issue")