Skip to content

Commit 69c1ac7

Browse files
ci👮‍♀️🚨 issue checker (#565)
* ci * ci: * ci: * ci: * ci: * ci: * ci: * ci: add warning if no linked issues * ci: * ci: * chore: remove console log
1 parent c5ad614 commit 69c1ac7

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

.github/helpers/src/issue-checker.ts

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env node
2+
3+
import { Command } from 'commander';
4+
import { getOctokit, context } from '@actions/github';
5+
6+
const program = new Command();
7+
type Octo = ReturnType<typeof getOctokit>;
8+
9+
program.name('PR');
10+
11+
const noLinkedIssueMessage = `
12+
Howdy, wanderer🌵🤠🐴,
13+
14+
Seems you've sauntered into our GitHub saloon with a pull request, but it appears you've forgotten to tie your horse to the hitching post. Now, in this town, we don't take kindly to stray requests, and the GitHub corral is no place for them.
15+
16+
I reckon you best mosey on over and link that pull request to an issue, lest you want the winds of open source trouble blowin' your way. I've got my eye on you, stranger, and a stern warning echoes through these digital canyons.
17+
18+
Now, for those who might be new to these parts or sufferin' from a bout of forgetfulness, fear not. I've rustled up a guide that's as handy as a snake in a boot🐍🥾. Take a peek at [this guide](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue), and it'll show you the way to tether that pull request like a seasoned rancher🤠.
19+
20+
Don't let the sun set on your unlinked pull request, and remember, in these GitHub lands, the code speaks louder than six-shooters.
21+
22+
Sincerely,
23+
The code patrol👮
24+
`;
25+
26+
program
27+
.command('issue')
28+
.option('-T, --token <token>', 'github token')
29+
.option('-P, --pr <pr>', 'Pull request number')
30+
.action(async (args) => {
31+
if (!args.token) {
32+
throw new Error('Missing github token');
33+
}
34+
35+
if (!args.pr) {
36+
throw new Error('Missing pr number');
37+
}
38+
39+
const client = getOctokit(args.token);
40+
checkIssues(client, args.pr);
41+
});
42+
43+
await program.parseAsync();
44+
45+
async function checkIssues(client: Octo, pr: number) {
46+
const pullRequests = await client.graphql(
47+
`query {
48+
repository (owner: "${context.repo.owner}", name: "${context.repo.repo}"){
49+
pullRequest (number: ${pr}) {
50+
closingIssuesReferences (first: 1){
51+
totalCount
52+
}
53+
}
54+
}
55+
}
56+
`
57+
.replaceAll('\n', '')
58+
.trim()
59+
);
60+
61+
const linkedIssues: number = (pullRequests as any).repository.pullRequest.closingIssuesReferences.totalCount;
62+
63+
if (linkedIssues === 0) {
64+
const comment = await client.rest.issues.createComment({
65+
issue_number: pr,
66+
body: noLinkedIssueMessage,
67+
owner: context.issue.owner,
68+
repo: context.issue.repo,
69+
});
70+
}
71+
}

.github/workflows/pr-issue-check.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Verify linked issue
2+
3+
# This action works with pull requests and pushes
4+
on:
5+
pull_request:
6+
7+
jobs:
8+
issue-check:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
with:
15+
# Make sure the actual branch is checked out when running on pull requests
16+
ref: ${{ github.head_ref }}
17+
18+
- run: npm run first-time-setup
19+
20+
- run: pnpm tsx ./.github/helpers/src/issue-checker.ts issue -T ${{ github.token }} -P ${{github.event.number}}

pnpm-lock.yaml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)