From 702fc382b9d638de3b8810abf2c20d93f2a5c0e1 Mon Sep 17 00:00:00 2001 From: Raman Pandey Date: Sun, 27 Jul 2025 20:08:18 +0530 Subject: [PATCH 1/2] Auto comment mistakes --- .github/workflows/validate.yml | 93 +++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 36 deletions(-) diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index 8b486f9ae9c..e751ccf8056 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -48,39 +48,60 @@ jobs: working-directory: cleanup validate-pr-template: - if: github.event_name == 'pull_request' - - runs-on: ubuntu-latest - - steps: - - name: Validate PR template - uses: actions/github-script@v7 - with: - script: | - const body = context.payload.pull_request.body || ""; - - // 1. Check both checkboxes - const checkbox1 = /\[x\].*?There is reasonable content/i.test(body); - const checkbox2 = /\[x\].*?I have read and accepted/i.test(body); - - // 2. URL must be on the exact "The site content can be seen at ..." line - // https://regex101.com/r/N36fsT - const urlLineRegex = /^[ \t]*-[ \t]*The site content can be seen at[ \t]+(?:<)?(?:\[.*?\]\()?https?:\/\/[^\s>()]+(?:\))?(?:>)?/m; - const urlMatch = urlLineRegex.exec(body); - const urlValid = urlMatch !== null; - - // 3. Explanation must follow the blockquote marker - const explanationMatch = />\s*The site content is(?:\s*|\s*\n)(.+)/i.exec(body); - const explanation = explanationMatch && explanationMatch[1].trim().length > 10; - - if (!checkbox1 || !checkbox2 || !urlValid || !explanation) { - core.setFailed( - "❌ PR template is not properly filled:\n" + - `Checkbox1: ${checkbox1 ? '✅' : '❌'}\n` + - `Checkbox2: ${checkbox2 ? '✅' : '❌'}\n` + - `URL on correct line: ${urlValid ? '✅' : '❌'}\n` + - `Explanation: ${explanation ? '✅' : '❌'}` - ); - } else { - console.log("✅ PR template format is valid."); - } + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + + steps: + - name: Validate PR template and comment + uses: actions/github-script@v7 + with: + script: | + const pr = context.payload.pull_request; + const body = pr.body || ""; + + const checkbox1 = /\[x\].*?There is reasonable content/i.test(body); + const checkbox2 = /\[x\].*?I have read and accepted/i.test(body); + const urlLineRegex = /^[ \t]*-[ \t]*The site content can be seen at[ \t]+(?:<)?(?:\[.*?\]\()?https?:\/\/[^\s>()]+(?:\))?(?:>)?/m; + const urlValid = urlLineRegex.exec(body) !== null; + const explanationMatch = />\s*The site content is(?:\s*|\s*\n)(.+)/i.exec(body); + const explanation = explanationMatch && explanationMatch[1].trim().length > 10; + + const failed = !checkbox1 || !checkbox2 || !urlValid || !explanation; + + if (!failed) { + console.log("✅ PR template is valid."); + return; + } + + const commentBody = `❌ **PR template is not properly filled:**\n\n- Checkbox1 (reasonable content): ${checkbox1 ? "✅" : "❌"}\n- Checkbox2 (read and accepted): ${checkbox2 ? "✅" : "❌"}\n- URL on correct line: ${urlValid ? "✅" : "❌"}\n- Explanation (> The site content is...): ${explanation ? "✅" : "❌"}\n\nPlease update your PR description accordingly.`; + + const { data: comments } = await github.rest.issues.listComments({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: pr.number, + }); + + const existingComment = comments.find(c => + c.user.login === 'github-actions[bot]' && + c.body.includes("**PR template is not properly filled:**") + ); + + if (existingComment) { + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: existingComment.id, + body: commentBody + }); + console.log("🔁 Updated existing PR comment."); + } else { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: pr.number, + body: commentBody + }); + console.log("💬 Posted new PR comment."); + } + + core.setFailed("PR template validation failed."); From 27825a6db8fb6b14dc951d350923bf966a54312d Mon Sep 17 00:00:00 2001 From: Raman Pandey Date: Sun, 27 Jul 2025 21:19:38 +0530 Subject: [PATCH 2/2] auto error detection --- .github/workflows/validate.yml | 102 ++++++++++++++++----------------- 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/.github/workflows/validate.yml b/.github/workflows/validate.yml index e751ccf8056..34b6f650172 100644 --- a/.github/workflows/validate.yml +++ b/.github/workflows/validate.yml @@ -48,60 +48,58 @@ jobs: working-directory: cleanup validate-pr-template: - if: github.event_name == 'pull_request' - runs-on: ubuntu-latest - - steps: - - name: Validate PR template and comment - uses: actions/github-script@v7 - with: - script: | - const pr = context.payload.pull_request; - const body = pr.body || ""; - - const checkbox1 = /\[x\].*?There is reasonable content/i.test(body); - const checkbox2 = /\[x\].*?I have read and accepted/i.test(body); - const urlLineRegex = /^[ \t]*-[ \t]*The site content can be seen at[ \t]+(?:<)?(?:\[.*?\]\()?https?:\/\/[^\s>()]+(?:\))?(?:>)?/m; - const urlValid = urlLineRegex.exec(body) !== null; - const explanationMatch = />\s*The site content is(?:\s*|\s*\n)(.+)/i.exec(body); - const explanation = explanationMatch && explanationMatch[1].trim().length > 10; - - const failed = !checkbox1 || !checkbox2 || !urlValid || !explanation; - - if (!failed) { - console.log("✅ PR template is valid."); - return; - } - - const commentBody = `❌ **PR template is not properly filled:**\n\n- Checkbox1 (reasonable content): ${checkbox1 ? "✅" : "❌"}\n- Checkbox2 (read and accepted): ${checkbox2 ? "✅" : "❌"}\n- URL on correct line: ${urlValid ? "✅" : "❌"}\n- Explanation (> The site content is...): ${explanation ? "✅" : "❌"}\n\nPlease update your PR description accordingly.`; - - const { data: comments } = await github.rest.issues.listComments({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: pr.number, - }); - - const existingComment = comments.find(c => - c.user.login === 'github-actions[bot]' && - c.body.includes("**PR template is not properly filled:**") - ); - - if (existingComment) { - await github.rest.issues.updateComment({ - owner: context.repo.owner, - repo: context.repo.repo, - comment_id: existingComment.id, - body: commentBody - }); - console.log("🔁 Updated existing PR comment."); - } else { - await github.rest.issues.createComment({ + if: github.event_name == 'pull_request' + runs-on: ubuntu-latest + + steps: + - name: Validate PR template and comment + uses: actions/github-script@v7 + with: + script: | + const pr = context.payload.pull_request; + const body = pr.body || ""; + + const checkbox1 = /\[x\].*?There is reasonable content/i.test(body); + const checkbox2 = /\[x\].*?I have read and accepted/i.test(body); + const urlLineRegex = /^[ \t]*-[ \t]*The site content can be seen at[ \t]+(?:<)?(?:\[.*?\]\()?https?:\/\/[^\s>()]+(?:\))?(?:>)?/m; + const urlValid = urlLineRegex.exec(body) !== null; + const explanationMatch = />\s*The site content is(?:\s*|\s*\n)(.+)/i.exec(body); + const explanation = explanationMatch && explanationMatch[1].trim().length > 10; + + const failed = !checkbox1 || !checkbox2 || !urlValid || !explanation; + + if (!failed) { + console.log("✅ PR template is valid."); + return; + } + + const commentBody = `❌ **PR template is not properly filled:**\n\n- Checkbox1 (reasonable content): ${checkbox1 ? "✅" : "❌"}\n- Checkbox2 (read and accepted): ${checkbox2 ? "✅" : "❌"}\n- URL on correct line: ${urlValid ? "✅" : "❌"}\n- Explanation (> The site content is...): ${explanation ? "✅" : "❌"}\n\nPlease update your PR description accordingly.`; + + const { data: comments } = await github.rest.issues.listComments({ owner: context.repo.owner, repo: context.repo.repo, issue_number: pr.number, - body: commentBody }); - console.log("💬 Posted new PR comment."); - } - core.setFailed("PR template validation failed."); + const existingComment = comments.find(c => + c.user.login === 'github-actions[bot]' && + c.body.includes("**PR template is not properly filled:**") + ); + + if (existingComment) { + await github.rest.issues.updateComment({ + owner: context.repo.owner, + repo: context.repo.repo, + comment_id: existingComment.id, + body: commentBody + }); + } else { + await github.rest.issues.createComment({ + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: pr.number, + body: commentBody + }); + } + + core.setFailed("PR template validation failed.");