-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
93 lines (80 loc) · 2.5 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
const core = require('@actions/core');
const github = require('@actions/github');
const PR_TEMPLATE_PATHS = [
'PULL_REQUEST_TEMPLATE.md',
'pull_request_template.md',
'.github/PULL_REQUEST_TEMPLATE.md',
'.github/pull_request_template.md',
'docs/PULL_REQUEST_TEMPLATE.md',
'docs/pull_request_template.md',
'PULL_REQUEST_TEMPLATE.txt',
'pull_request_template.txt',
'.github/PULL_REQUEST_TEMPLATE.txt',
'.github/pull_request_template.txt',
'docs/PULL_REQUEST_TEMPLATE.txt',
'docs/pull_request_template.txt',
'PULL_REQUEST_TEMPLATE',
'pull_request_template',
'.github/PULL_REQUEST_TEMPLATE',
'.github/pull_request_template',
'docs/PULL_REQUEST_TEMPLATE',
'docs/pull_request_template',
]
const getPrNumber = () => {
const pullRequest = github.context.payload.pull_request;
if (!pullRequest) {
return undefined;
}
return pullRequest.number;
}
const getPrDescription = async (client) => {
const prNumber = getPrNumber();
if (!prNumber) {
core.error("Could not get pull request number from context, exiting");
return;
}
const { data: pullRequest } = await client.pulls.get({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: prNumber
});
// github adds \r\n on pr body
return pullRequest.body.replace(/(\r\n)/g, '\n')
}
const getPrTemplate = async (client, paths) => {
const prTemplatePath = paths.shift();
core.info(`trying pr template path: ${prTemplatePath}`);
try {
const {data: content} = await client.repos.getContent({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
path: prTemplatePath,
mediaType: {
format: 'raw',
}
});
return content;
} catch (error) {
if (!paths.length) {
return undefined
}
core.warning(`error getting pr template (${prTemplatePath}): ${error.message}`)
return getPrTemplate(client, paths)
}
}
async function run() {
try {
const token = core.getInput("repo-token", { required: true });
const client = github.getOctokit(token)
const prDescription = await getPrDescription(client)
const prTemplate = await getPrTemplate(client, PR_TEMPLATE_PATHS)
if (!prDescription) {
core.setFailed("PR description missing");
} else if (prDescription.includes(prTemplate)) {
core.setFailed("PR description includes PR template text verbatim. Please adjust the default PR text to include a more complete description");
}
} catch (error) {
core.setFailed(error.message);
}
}
run();