forked from copilotmoney/linear-pr-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (88 loc) · 3.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const core = require('@actions/core');
const github = require('@actions/github');
const issuecheck = require('./issuecheck.js')
async function run() {
// Check if the event is a merge queue event
if (github.context.eventName === 'merge_group') {
core.info('This is a merge queue event. Skipping issue check.');
return;
}
const authToken = core.getInput('github_token', {required: true});
const client = new github.GitHub(authToken);
const owner = github.context.payload.pull_request.base.user.login;
const repo = github.context.payload.pull_request.base.repo.name;
const pr_number = github.context.payload.pull_request.number;
try {
const {data: pullRequest} = await client.pulls.get({
owner,
repo,
pull_number: pr_number
});
const title = pullRequest.title;
// Skip the check if the title starts with "no-issue:"
if (title.toLowerCase().includes('[no-issue]')) {
core.info('PR title contains "[no-issue]". Skipping issue check.');
return;
}
const description = pullRequest.body;
const branch = pullRequest.head.ref;
const issue = issuecheck.findIssue(core.getInput("prefix"), title, description, branch);
core.info(`Issue ${issue} found`);
// Remove existing comment if any
await removeExistingComment(client, owner, repo, pr_number);
} catch (error) {
core.setFailed("Issue not found in PR: All PRs must have an associated issue");
const errorMessage = `
Linear supports four ways to link issues with your pull requests:
1. Include *issue ID* in the branch name
2. Include *issue ID* in the PR title
3. Include *issue ID* with a magic word in the PR description (e.g., Fixes ENG-123) similar to GitHub Issues
4. Include the issue URL in the PR description (e.g., https://linear.app/yourteam/issue/ENG-123/issue-title)
`;
core.info(errorMessage);
// Check if comment already exists, if not, create one
const commentExists = await checkExistingComment(client, owner, repo, pr_number);
if (!commentExists) {
await client.issues.createComment({
owner,
repo,
issue_number: pr_number,
body: "Issue not found in PR: All PRs must have an associated issue.\n\n" + errorMessage
});
core.info('Added comment about missing issue');
} else {
core.info('Comment about missing issue already exists');
}
}
}
async function checkExistingComment(client, owner, repo, pr_number) {
const {data: comments} = await client.issues.listComments({
owner,
repo,
issue_number: pr_number
});
return comments.some(comment =>
comment.user.type === 'Bot' &&
comment.body.startsWith("Issue not found in PR: All PRs must have an associated issue.")
);
}
async function removeExistingComment(client, owner, repo, pr_number) {
const {data: comments} = await client.issues.listComments({
owner,
repo,
issue_number: pr_number
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' &&
comment.body.startsWith("Issue not found in PR: All PRs must have an associated issue.")
);
if (botComment) {
await client.issues.deleteComment({
owner,
repo,
comment_id: botComment.id
});
core.info('Removed previous error comment');
}
}
run();