From 5c7844fc70c4c68e2c74cbc56cc525dd9a0f3638 Mon Sep 17 00:00:00 2001 From: Robbie Blaine Date: Tue, 5 Nov 2024 17:00:22 +0200 Subject: [PATCH] Validate emojis against Gitmoji.dev --- .github/workflows/validate-pr.yml | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/.github/workflows/validate-pr.yml b/.github/workflows/validate-pr.yml index 45f441342..a45e3831e 100644 --- a/.github/workflows/validate-pr.yml +++ b/.github/workflows/validate-pr.yml @@ -23,17 +23,26 @@ jobs: script: | // Get PR Title const title = context.payload.pull_request.title; + core.setOutput('title', title); // Get PR Comments const comments = await github.rest.issues.listComments({ owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number - }); - - core.setOutput('title', title); + }); core.setOutput('comments', comments.data); + // Get JSON of valid Gitmojis + const gitmojiResponse = await github.request('GET /repos/{owner}/{repo}/contents/{path}', { + owner: 'carloscuesta', + repo: 'gitmoji', + path: 'packages/gitmojis/src/gitmojis.json' + }); + + const gitmojis = JSON.parse(Buffer.from(gitmojiResponse.data.content, 'base64').toString()).gitmojis; + core.setOutput('gitmojis', gitmojis); + - name: PR title should start with emoji uses: actions/github-script@v7 if: always() @@ -41,11 +50,12 @@ jobs: script: | const prTitle = "${{ steps.pr.outputs.title }}"; const comments = ${{ steps.pr.outputs.comments }}; + const gitmojis = ${{ steps.pr.outputs.gitmojis }}; + const validEmojis = gitmojis.map(g => [g.emoji, g.code]); - // Match either: - // 1. Unicode emoji at start (using Unicode properties) - // 2. GitHub emoji shortcode format (e.g. :fire:) - const emojiRegex = /^(?:[\p{Emoji_Presentation}\p{Extended_Pictographic}]|:[a-z0-9_+-]+:)/u; + const titleStartsWithValidEmoji = validEmojis.some(([emoji, code]) => + prTitle.startsWith(emoji) || prTitle.startsWith(code) + ); // Find our bot's validation comment if it exists const botComment = comments.find(comment => @@ -53,18 +63,19 @@ jobs: comment.body.includes('Your PR title should start with an emoji!') ); - if (!emojiRegex.test(prTitle)) { + if (!titleStartsWithValidEmoji) { // Only add a comment if we haven't already if (!botComment) { await github.rest.issues.createComment({ owner: context.repo.owner, repo: context.repo.repo, issue_number: context.issue.number, - body: `⚠️ Your PR title should start with an emoji!\n\nExample valid titles:\n- ✨ Add new feature (or :sparkles: Add new feature)\n- 🐛 Fix login bug (or :bug: Fix login bug)\n- 📝 Update documentation (or :memo: Update documentation)\n\nNeed emoji suggestions? Check out [gitmoji.dev](https://gitmoji.dev) for a comprehensive list of Git-friendly emojis!\n\nPlease update your PR title and try again.` + body: `⚠️ Your PR title should start with an emoji!\n\nExample valid titles:\n- ✨ Add new feature (or :sparkles: Add new feature)\n- 🐛 Fix login bug (or :bug: Fix login bug)\n- 📝 Update documentation (or :memo: Update documentation)\n\nTo view all valid emojis, check out [gitmoji.dev](https://gitmoji.dev) for a comprehensive list of Git-friendly emojis!\n\nPlease update your PR title and try again.` }); } // Create warning annotation instead of failing + // Change to `core.setFailed()` to fail the check core.warning(`PR title should start with an emoji! Current title: "${prTitle}". Use either Unicode emoji (🔥) or GitHub shortcode format (:fire:)`); } else if (botComment) { // If title is now valid and we have a comment, delete it