From ea30a05a7a5c2b11c60d1b4b47b1a695b68bdb56 Mon Sep 17 00:00:00 2001 From: Kenneth Seet <120318851+itstrueitstrueitsrealitsreal@users.noreply.github.com> Date: Tue, 30 Jul 2024 23:16:20 +0800 Subject: [PATCH] [#11383] Add detection of PRs linking to closed issues (#13154) * Add check for closed issue * Update message * Rephrase message * Add logging * Test * Update pr.yml * Update pr.yml * Update pr.yml * Update pr.yml * Update pr.yml * Update pr.yml * Update pr.yml * Add check for closed issue Update message Rephrase message Add logging Test Update pr.yml * [#13147] Didn't Account for the Timezone in Session Edit Form component tests (#13148) Update pr.yml Update pr.yml Update pr.yml * Update pr.yml * Update workflow --------- Co-authored-by: Rayson Yeap <88478542+Respirayson@users.noreply.github.com> --- .github/workflows/pr.yml | 74 +++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 28 deletions(-) diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 7885aa57cd8..ef03f564de3 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -13,31 +13,49 @@ jobs: permissions: pull-requests: write steps: - - uses: actions/github-script@v7 - with: - script: | - const pr = await github.rest.pulls.get({ - owner: context.repo.owner, - repo: context.repo.repo, - pull_number: context.issue.number - }) - const isTitleValid = /^\[#\d+\] /.test(pr.data.title) - const isDescriptionValid = /([Ff]ix(es|ed)?|[Cc]lose(s|d)?|[Rr]esolve(s|d)?|[Pp]art [Oo]f) #\d+/.test(pr.data.body) - if (isTitleValid && isDescriptionValid) { - return - } - let body = `Hi @${pr.data.user.login}, thank you for your interest in contributing to TEAMMATES! - However, your PR does not appear to follow our [contribution guidelines](https://teammates.github.io/teammates/process.html#step-4-submit-a-pr):\n\n` - if (!isTitleValid) { - body += "- Title must start with the issue number the PR is fixing in square brackets, e.g. `[#]`\n" - } - if (!isDescriptionValid) { - body += "- Description must reference the issue number the PR is fixing, e.g. `Fixes #` (or `Part of #` if the PR does not address the issue fully)\n" - } - body += "\nPlease address the above before we proceed to review your PR." - await github.rest.issues.createComment({ - issue_number: context.issue.number, - owner: context.repo.owner, - repo: context.repo.repo, - body, - }) + - uses: actions/github-script@v7 + with: + script: | + const pr = await github.rest.pulls.get({ + owner: context.repo.owner, + repo: context.repo.repo, + pull_number: context.issue.number + }) + const isTitleValid = /^\[#\d+\] /.test(pr.data.title) + const isDescriptionValid = /([Ff]ix(es|ed)?|[Cc]lose(s|d)?|[Rr]esolve(s|d)?|[Pp]art [Oo]f) #\d+/.test(pr.data.body) + const descriptionRegex = /(?:[Ff]ix(?:es|ed)?|[Cc]lose(?:s|d)?|[Rr]esolve(?:s|d)?|[Pp]art [Oo]f) #(\d+)/ + const extractIssueNumber = (description) => { + const match = description.match(descriptionRegex) + return match ? parseInt(match[1]) : null + }; + const issueNumber = extractIssueNumber(pr.data.body) + let isIssueOpen = false + if (issueNumber) { + const issue = await github.rest.issues.get({ + owner: 'TEAMMATES', + repo: 'teammates', + issue_number: issueNumber + }) + isIssueOpen = issue.data.state === 'open' + if (isTitleValid && isDescriptionValid && isIssueOpen) { + return + } + } + let body = `Hi @${pr.data.user.login}, thank you for your interest in contributing to TEAMMATES! + However, your PR does not appear to follow our [contribution guidelines](https://teammates.github.io/teammates/process.html#step-4-submit-a-pr):\n\n` + if (!isTitleValid) { + body += "- Title must start with the issue number the PR is fixing in square brackets, e.g. `[#]`\n" + } + if (!isDescriptionValid) { + body += "- Description must reference the issue number the PR is fixing, e.g. `Fixes #` (or `Part of #` if the PR does not address the issue fully)\n" + } + if (!isIssueOpen && issueNumber) { + body += "- The issue referenced in the description (#" + issueNumber + ") is not open.\n" + } + body += "\nPlease address the above before we proceed to review your PR." + await github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body, + }) \ No newline at end of file