-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
71 lines (61 loc) · 2.12 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
const core = require('@actions/core');
const github = require('@actions/github');
async function run() {
try {
const inputs = {
token: core.getInput('repo-token', {required: true}),
}
const title = github.context.payload.pull_request.title;
core.info(title);
const isAdhoc = title.includes('ADHOC') || title.includes('STORYBOOK');
if (isAdhoc) {
core.info("PR is adhoc or storybook -- no updates made");
return;
}
core.info(title);
const matches = title.match(/(\w+-\d+)/)
if (!(matches)) {
core.warning("Jira ticket not in PR title");
return;
}
const jiraTicketKey = matches[0];
core.info(`Jira Ticket Key: ${jiraTicketKey}`);
let body = github.context.payload.pull_request.body;
// core.info(body);
if (!body) {
core.warning('Body is blank. Jira ticket will over-write any previous body.');
body = ""
}
if (body.includes(jiraTicketKey)) {
core.info('PR body is prefixed already - no updates made');
return;
}
const request = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: github.context.payload.pull_request.number,
}
const linkRegex = /^(?=.*?\bJIRA\b)(?=.*?\bticket\b).*$/m
let lineToAdd = `:ticket: [JIRA ticket: ${jiraTicketKey}](https://notarize.atlassian.net/browse/${jiraTicketKey}) :ticket:`
const lineExists = body.match(linkRegex)
if (lineExists) {
core.info("'JIRA ticket' exists in PR body without ticket number; replacing that line.");
request.body = body.replace(linkRegex, lineToAdd)
} else {
core.info("'JIRA ticket' not found. Adding line to PR body.");
request.body = lineToAdd.concat('\n', body);
}
core.debug(`new body: ${request.body}`)
const client = github.getOctokit(inputs.token);
const response = await client.rest.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()