forked from tzkhan/pr-update-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
93 lines (77 loc) · 3.44 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');
async function run() {
try {
const tokenRegex = new RegExp('%branch%', "g");
const inputs = {
token: core.getInput('repo-token', {required: true}),
branchRegex: core.getInput('branch-regex', {required: true}),
lowercaseBranch: (core.getInput('lowercase-branch').toLowerCase() === 'true'),
titleTemplate: core.getInput('title-template', {required: true}),
replaceTitle: (core.getInput('replace-title').toLowerCase() === 'true'),
titlePrefixSpace: (core.getInput('title-prefix-space').toLowerCase() === 'true'),
uppercaseTitle: (core.getInput('uppercase-title').toLowerCase() === 'true'),
bodyTemplate: core.getInput('body-template', {required: true}),
replaceBody: (core.getInput('replace-body').toLowerCase() === 'true'),
bodyPrefixNewlineCount: parseInt(core.getInput('body-prefix-newline-count', {required: true})),
uppercaseBody: (core.getInput('uppercase-body').toLowerCase() === 'true'),
}
const branchName = github.context.payload.pull_request.head.ref;
const branch = inputs.lowercaseBranch ? branchName.toLowerCase() : branchName;
core.debug(`branch: ${branch}`);
const matches = branch.match(new RegExp(inputs.branchRegex));
if (!matches) {
core.setFailed('Branch name does not match given regex');
return;
}
const match = (upperCase) => upperCase ? matches[0].toUpperCase() : matches[0];
core.info(`Matched branch text: ${match(false)}`);
const request = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: github.context.payload.pull_request.number,
}
const title = github.context.payload.pull_request.title || '';
const processedTitle = inputs.titleTemplate.replace(tokenRegex, match(inputs.uppercaseTitle));
core.debug(`processedTitle: ${processedTitle}`);
const updateTitle = inputs.replaceTitle
? title.toLowerCase() !== processedTitle.toLowerCase()
: !title.toLowerCase().startsWith(processedTitle.toLowerCase());
if (updateTitle) {
request.title = inputs.replaceTitle
? processedTitle
: processedTitle.concat(inputs.titlePrefixSpace ? ' ': '', title);
core.debug(`new title: ${request.title}`);
} else {
core.warning('PR title is up to date already - no updates made');
}
const body = github.context.payload.pull_request.body || '';
const processedBody = inputs.bodyTemplate.replace(tokenRegex, match(inputs.uppercaseBody));
core.debug(`processedBody: ${processedBody}`);
const updateBody = inputs.replaceBody
? body.toLowerCase() !== processedBody.toLowerCase()
: !body.toLowerCase().startsWith(processedBody.toLowerCase());
if (updateBody) {
request.body = inputs.replaceBody
? processedBody
: processedBody.concat('\n'.repeat(inputs.bodyPrefixNewlineCount), body);
core.debug(`new body: ${request.body}`);
} else {
core.warning('PR body is up to date already - no updates made');
}
if (!updateTitle && !updateBody) {
return;
}
const client = new github.GitHub(inputs.token);
const response = await client.pulls.update(request);
core.info(`response: ${response.status}`);
if (response.status !== 200) {
core.error('Updating the pull request has failed');
}
}
catch (error) {
core.error(error);
core.setFailed(error.message);
}
}
run()