-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
88 lines (82 loc) · 3.27 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
const core = require('@actions/core');
const github = require('@actions/github');
const {Toolkit} = require('actions-toolkit');
const showdown = require('showdown'),
converter = new showdown.Converter({simplifiedAutoLink: true});
const https = require('https');
const url = require('url');
function escapeForJson(str) {
return str
.replace(/\\/g, '\\\\') // Escape backslashes
.replace(/"/g, '\\"') // Escape straight double quotes
.replace(/'/g, "\\'") // Escape straight single quotes
.replace(/“/g, '\\"') // Escape left double quotation mark
.replace(/”/g, '\\"') // Escape right double quotation mark
.replace(/‘/g, "\\'") // Escape left single quotation mark
.replace(/’/g, "\\'") // Escape right single quotation mark
.replace(/\r/g, '\\r'); // Escape carriage returns
}
Toolkit.run(async tools => {
try {
if (!tools.context.payload.ref) {
tools.log.warn('Not a push skipping verification!');
return;
}
const stripedLines = parseInt(core.getInput('strip_first_lines'));
const ignoreNotes = core.getInput('no_notes') === 'true';
const productId = process.env.PRODUCT_ID || '';
const token = process.env.AUTH_TOKEN || '';
let storeUrl = process.env.STORE_URL || '';
const buildVersion = process.env.BUILD_VERSION || '';
storeUrl = new URL(storeUrl);
tools.log.debug('Starting commit verification!');
let commitMessage = tools.context.payload.head_commit.message || '';
if(ignoreNotes){
commitMessage = '';
}
if (commitMessage.length !== 0) {
tools.log.debug('No commit found!');
commitMessage = commitMessage.split(/\r?\n/);
commitMessage.splice(0, stripedLines);
commitMessage = commitMessage.join("\n");
commitMessage = converter.makeHtml(commitMessage);
}
const data = JSON.stringify({
"version": buildVersion, "id": productId, "body": escapeForJson(commitMessage), no_parsing: "yes"
});
const options = {
hostname: storeUrl.hostname,
path: '/wp-json/edd-so/v1/update_changelog/',
headers: {
'Cache-Control': 'no-cache',
'Content-Length': data.length,
'Content-Type': 'application/json',
'x-themeisle-auth': token
},
method: 'POST',
};
const req = https.request(options, (res) => {
let data = '';
tools.log.debug('Status Code:', res.statusCode);
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
tools.log.debug('Body: ', JSON.parse(data));
});
}).on("error", (err) => {
tools.log.debug("Error: ", err.message);
});
req.write(data);
req.end();
} catch (err) {
const errorMessage = "Error verifying commit."
tools.log.error(errorMessage)
tools.log.error(err)
if (err.errors) tools.log.error(err.errors)
core.setFailed(errorMessage + '\n\n' + err.message)
tools.exit.failure()
}
}, {
secrets: ['GITHUB_TOKEN']
});