From 819e6b7b594f9132e67352fb906ac98a349a93f0 Mon Sep 17 00:00:00 2001 From: Yun-Wu Date: Tue, 15 Oct 2024 23:41:14 +0800 Subject: [PATCH] Enforce PRs to link with Issues (#289) Co-authored-by: atmorling --- .github/workflows/link_issue.yml | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 .github/workflows/link_issue.yml diff --git a/.github/workflows/link_issue.yml b/.github/workflows/link_issue.yml new file mode 100644 index 00000000..82050ab8 --- /dev/null +++ b/.github/workflows/link_issue.yml @@ -0,0 +1,44 @@ +name: Check Linked Issue + +on: + pull_request: + types: [opened, reopened, synchronize, edited] + +jobs: + check-linked-issue: + runs-on: ubuntu-latest + steps: + - name: Verify Linked Issue + uses: actions/github-script@v6 + with: + github-token: ${{secrets.GITHUB_TOKEN}} + script: | + const { pull_request } = context.payload; + const bodyText = pull_request.body || ''; + const issuePattern = /(?:close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved)\s+#(\d+)/i; + const match = bodyText.match(issuePattern); + + if (!match) { + core.setFailed('No linked issue found in the pull request description.'); + return; + } + + const issueNumber = parseInt(match[1], 10); + const { owner, repo } = context.repo; + + try { + const issue = await github.rest.issues.get({ + owner, + repo, + issue_number: issueNumber + }); + const minLength = 30; // Minimum description length + + if (!issue.data.body || issue.data.body.trim().length < minLength) { + core.setFailed(`Linked issue #${issueNumber} does not have a sufficient description (at least ${minLength} characters required).`); + } else { + console.log(`Linked issue #${issueNumber} has a sufficient description.`); + } + } catch (error) { + core.setFailed(`Issue #${issueNumber} not found or inaccessible.`); + }