forked from claudiamatosa/github-action-replace-pr-body
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
52 lines (40 loc) · 1.47 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
const core = require('@actions/core');
const github = require('@actions/github');
// Toolkit docs: https://github.com/actions/toolkit
async function run() {
try {
const inputs = {
token: core.getInput('github-token', {required: true})
};
// Pull-request format: https://developer.github.com/v3/pulls/#response
const variables = {
prNumber: github.context.payload.pull_request.number,
branchName: github.context.payload.pull_request.head.ref,
};
const body = github.context.payload.pull_request.body;
console.log('Initial description: ', body);
if (!body) return;
const newBody = (body.match(/{{\w+}}/g) || []).reduce((contents, placeholder) => {
const variableName = placeholder.replace(/({|})/g, '');
const value = variables[variableName];
console.log(`Replacing ${placeholder} with ${value}`);
return contents.replace(placeholder, value);
}, body);
console.log('New description: ', newBody);
const request = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: github.context.payload.pull_request.number,
body: newBody
};
const client = new github.GitHub(inputs.token);
const response = await client.pulls.update(request);
if (response.status !== 200) {
core.error('There was an issue while trying to update the pull-request.');
}
} catch (error) {
core.error(error);
core.setFailed(error.message);
}
}
run()