|
| 1 | +# This GitHub Action workflow checks if a new or updated pull request |
| 2 | +# references a GitHub issue in its title or body. If no reference is found, |
| 3 | +# it automatically creates a new issue. This helps ensure all work is |
| 4 | +# tracked, especially when syncing with tools like Linear. |
| 5 | + |
| 6 | +name: Create issue for unreferenced PR |
| 7 | + |
| 8 | +# This action triggers on pull request events |
| 9 | +on: |
| 10 | + pull_request: |
| 11 | + types: [opened, edited, reopened, synchronize, ready_for_review] |
| 12 | + |
| 13 | +# Cancel in progress workflows on pull_requests. |
| 14 | +# https://docs.github.com/en/actions/using-jobs/using-concurrency#example-using-a-fallback-value |
| 15 | +concurrency: |
| 16 | + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +jobs: |
| 20 | + check_for_issue_reference: |
| 21 | + runs-on: ubuntu-latest |
| 22 | + steps: |
| 23 | + - name: Check PR Body and Title for Issue Reference |
| 24 | + uses: actions/github-script@v8 |
| 25 | + with: |
| 26 | + script: | |
| 27 | + const pr = context.payload.pull_request; |
| 28 | + if (!pr) { |
| 29 | + core.setFailed('Could not get PR from context.'); |
| 30 | + return; |
| 31 | + } |
| 32 | +
|
| 33 | + // Don't create an issue for draft PRs |
| 34 | + if (pr.draft) { |
| 35 | + console.log(`PR #${pr.number} is a draft, skipping issue creation.`); |
| 36 | + return; |
| 37 | + } |
| 38 | +
|
| 39 | + // Check if the PR is already approved |
| 40 | + const reviewsResponse = await github.rest.pulls.listReviews({ |
| 41 | + owner: context.repo.owner, |
| 42 | + repo: context.repo.repo, |
| 43 | + pull_number: pr.number, |
| 44 | + }); |
| 45 | +
|
| 46 | + if (reviewsResponse.data.some(review => review.state === 'APPROVED')) { |
| 47 | + console.log(`PR #${pr.number} is already approved, skipping issue creation.`); |
| 48 | + return; |
| 49 | + } |
| 50 | +
|
| 51 | + const prBody = pr.body || ''; |
| 52 | + const prTitle = pr.title || ''; |
| 53 | + const prAuthor = pr.user.login; |
| 54 | + const prUrl = pr.html_url; |
| 55 | + const prNumber = pr.number; |
| 56 | +
|
| 57 | + // Regex for GitHub issue references (e.g., #123, fixes #456) |
| 58 | + const issueRegexGitHub = /(?:(?:close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved):?\s*)?#\d+/i; |
| 59 | +
|
| 60 | + // Regex for Linear issue references (e.g., ENG-123, resolves ENG-456) |
| 61 | + const issueRegexLinear = /(?:(?:close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved):?\s*)?[A-Z]+-\d+/i; |
| 62 | +
|
| 63 | + const contentToCheck = `${prTitle} ${prBody}`; |
| 64 | + const hasIssueReference = issueRegexGitHub.test(contentToCheck) || issueRegexLinear.test(contentToCheck); |
| 65 | +
|
| 66 | + if (hasIssueReference) { |
| 67 | + console.log(`PR #${prNumber} contains a valid issue reference.`); |
| 68 | + return; |
| 69 | + } |
| 70 | +
|
| 71 | + // Check if there's already an issue created by this automation for this PR |
| 72 | + // Search for issues that mention this PR and were created by github-actions bot |
| 73 | + const existingIssuesResponse = await github.rest.search.issuesAndPullRequests({ |
| 74 | + q: `repo:${context.repo.owner}/${context.repo.repo} is:issue is:open author:app/github-actions "${prUrl}" in:title in:body`, |
| 75 | + }); |
| 76 | +
|
| 77 | + if (existingIssuesResponse.data.total_count > 0) { |
| 78 | + const existingIssue = existingIssuesResponse.data.items[0]; |
| 79 | + console.log(`An issue (#${existingIssue.number}) already exists for PR #${prNumber}, skipping creation.`); |
| 80 | + return; |
| 81 | + } |
| 82 | +
|
| 83 | + core.warning(`PR #${prNumber} does not have an issue reference. Creating a new issue so it can be tracked in Linear.`); |
| 84 | +
|
| 85 | + // Construct the title and body for the new issue |
| 86 | + const issueTitle = `${prTitle}`; |
| 87 | + const issueBody = `> [!NOTE] |
| 88 | + > The pull request "[${prTitle}](${prUrl})" was created by @${prAuthor} but did not reference an issue. Therefore this issue was created for better visibility in external tools like Linear. |
| 89 | +
|
| 90 | + ${prBody} |
| 91 | + `; |
| 92 | +
|
| 93 | + // Create the issue using the GitHub API |
| 94 | + const newIssue = await github.rest.issues.create({ |
| 95 | + owner: context.repo.owner, |
| 96 | + repo: context.repo.repo, |
| 97 | + title: issueTitle, |
| 98 | + body: issueBody, |
| 99 | + assignees: [prAuthor] |
| 100 | + }); |
| 101 | +
|
| 102 | + const issueID = newIssue.data.number; |
| 103 | + console.log(`Created issue #${issueID}.`); |
| 104 | +
|
| 105 | + // Update the PR body to reference the new issue |
| 106 | + const updatedPrBody = `${prBody}\n\nCloses #${issueID}`; |
| 107 | +
|
| 108 | + await github.rest.pulls.update({ |
| 109 | + owner: context.repo.owner, |
| 110 | + repo: context.repo.repo, |
| 111 | + pull_number: prNumber, |
| 112 | + body: updatedPrBody |
| 113 | + }); |
| 114 | +
|
| 115 | + console.log(`Updated PR #${prNumber} to reference newly created issue #${issueID}.`); |
0 commit comments