Reputation: 1847
Is there a way to force an issue to be linked to a PR before allowing the user to merge/close the PR? I was able to set up all of the other checks in the branch protection rule, but I cannot find a way to check that there is at least 1 linked issue.
Upvotes: 3
Views: 2311
Reputation: 3339
Here's a rough starting point you could use for our own GitHub Action to require linked issues on PRs (save this as .github/workflows/linked-issues.yml
, for example):
name: Check Linked Issues
on:
pull_request:
types: [opened, edited, synchronize]
jobs:
check-linked-issues:
runs-on: ubuntu-latest
steps:
- name: Check for linked issues
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
QUERY='query ($prNumber: Int!, $repositoryName: String!, $repositoryOwner: String!) {
repository(name: $repositoryName, owner: $repositoryOwner) {
pullRequest(number: $prNumber) {
closingIssuesReferences(first: 10) {
nodes {
number
}
}
}
}
}'
ISSUES=$(gh api graphql -f query="$QUERY" -F prNumber=${{ github.event.pull_request.number }} -F repositoryName=${{ github.event.repository.name }} -F repositoryOwner=${{ github.event.repository.owner.login }} -q '.data.repository.pullRequest.closingIssuesReferences.nodes[] | .number')
if [ -z "$ISSUES" ]
then
echo "No issues linked to this PR. Please link an issue and try again."
exit 1
else
echo "Issue(s) linked: $ISSUES"
fi
You won't find linked issues in the REST API, to my knowledge.
One thing that's important to note is that if you link issues from the PR sidebar instead of the PR description, it doesn't actually trigger any webhooks that you can use for CI, so it's not bulletproof. There are some specific examples of the two different ways to link issues here: https://www.pullapprove.com/docs/examples/verifying-linked-issues-for-traceability/
Upvotes: 0
Reputation: 76794
There is no intrinsic way to require this. However, you can use a CI check, such as a GitHub Action, to read the PR text from the API and require it to mention an open issue, which will prevent it from being merged without containing the expected text.
However, as for closing a pull request without merging it, such as if the submitted decides that the change is no longer necessary or should not have been made, it's not possible to prevent the user from doing so. Such a policy wouldn't be helpful, since it would force users to leave unwanted pull requests open.
Upvotes: 3