forked from luigibertaco/dynamic-template-action
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
96 lines (77 loc) · 2.93 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
const core = require('@actions/core');
const github = require('@actions/github');
const mustache = require('mustache');
async function run(){
try {
// requires GitHub Token to allow PR updates
const ghToken = core.getInput('token', {required: true})
// custom data to render besides pull_request context
const customInput = getCustomInput();
// replacement for {{ opening tag
const openingTag = getOpeningMustacheTag();
// replacement for }} closing tag
const closingTag = getClosingMustacheTag();
// Get the JSON webhook payload for the event that triggered the workflow
const payload = JSON.stringify(github.context.payload, undefined, 2)
//core.info(`The event payload: ${payload}`);
// Show PR details
const pr = github.context.payload.pull_request;
core.info(`Pull request body: ${pr.body}`);
core.info(`Pull request title: ${pr.title}`);
if(customInput){
Object.entries(customInput).forEach(([key, value]) => {
core.info(`Custom Property ${openingTag} custom.${key} ${closingTag} will render ${value}`);
})
}
const viewData = {...pr, custom:customInput}
mustache.tags = [openingTag,closingTag]
// update PR body
const octokit = github.getOctokit(ghToken);
const request = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
pull_number: github.context.payload.pull_request.number,
body: mustache.render(pr.body, viewData),
title: mustache.render(pr.title, viewData),
}
core.info(`update request: ${request}`);
const response = await octokit.rest.pulls.update(request);
core.info(`Response: ${response.status}`);
if (response.status !== 200) {
core.error(`Updating the pull request has failed: ${response.text}`);
}
} catch (error) {
core.setFailed(error.message);
}
}
function getOpeningMustacheTag(){
const customTag = core.getInput("openingTag", {required: false})
return customTag ? customTag : "{{";
}
function getClosingMustacheTag(){
const customTag = core.getInput("closingTag", {required: false})
return customTag ? customTag : "}}";
}
function getCustomInput(){
const customInput = core.getInput("customInput", {
required: false,
});
try {
return parseArray(customInput).reduce((customInputObject, keyValueString) => {
if(!keyValueString) return customInputObject;
const indexOfSeparator = keyValueString.indexOf(":");
const key = keyValueString.substring(0, indexOfSeparator);
const value = keyValueString.substring(indexOfSeparator + 1);
return {...customInputObject, [key]:value }
},{})
}catch(error){
core.error(`Failed to parse custom input: ${error}`);
}
return null;
}
const parseArray = (val) => {
const array = val.split('\n')
const filtered = array.filter((n) => n)
return filtered.map((n) => n.trim())
}
run();