chore: link issue pipeline time reduce #8
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Check Linked Issue | |
on: | |
pull_request: | |
types: [opened, edited, synchronize, reopened] | |
jobs: | |
check-linked-issue: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Check for linked issue | |
uses: actions/github-script@v7 | |
with: | |
script: | | |
const query = ` | |
query($owner: String!, $repo: String!, $number: Int!) { | |
repository(owner: $owner, name: $repo) { | |
pullRequest(number: $number) { | |
title | |
body | |
closingIssuesReferences(first: 10) { | |
nodes { | |
number | |
} | |
} | |
} | |
} | |
} | |
`; | |
const variables = { | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
number: context.issue.number, | |
}; | |
const result = await github.graphql(query, variables); | |
const pr = result.repository.pullRequest; | |
const textToCheck = `${pr.title} ${pr.body || ''}`; | |
const issuePatterns = [ | |
/(?:close[sd]?|fix(?:e[sd])?|resolve[sd]?)\s+#(\d+)/gi, | |
/#(\d+)/g, | |
/(?:issue|issues)\s+#?(\d+)/gi, | |
]; | |
let linkedIssues = new Set(); | |
for (const pattern of issuePatterns) { | |
for (const match of textToCheck.matchAll(pattern)) { | |
if (match[1]) linkedIssues.add(match[1]); | |
} | |
} | |
if (pr.closingIssuesReferences.nodes.length > 0) { | |
pr.closingIssuesReferences.nodes.forEach(issue => linkedIssues.add(issue.number.toString())); | |
} | |
if (linkedIssues.size === 0) { | |
await github.rest.issues.createComment({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
issue_number: context.issue.number, | |
body: `❌ This PR needs to be linked to an issue.\nAdd \`Fixes #123\` or link via the UI.` | |
}); | |
core.setFailed('No linked issue found.'); | |
} else { | |
console.log(`✅ Linked to issues: ${Array.from(linkedIssues).join(', ')}`); | |
} |