-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpull_request.js
172 lines (127 loc) · 3.97 KB
/
pull_request.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const github = require('@actions/github');
const crypto = require('crypto');
const utils = require('./utils');
const assert = require('assert').strict;
const fs = require('fs');
const execSync = require('child_process').execSync;
async function readArchivedFile(octokit, run, branch, archive_name, file, modifier) {
const workflowId = run["workflow_id"]
const runData = await octokit.actions.listWorkflowRuns({
...github.context.repo,
workflow_id: workflowId,
branch,
event: "push",
status: "completed"
});
if (!("workflow_runs" in runData.data)) {
return "No Workflow Runs";
}
const runs = runData.data["workflow_runs"];
if (runs.length <= 0) {
return "No Workflow Runs";
}
const runId = runs[0]["id"];
const artifactResp = await octokit.actions.listWorkflowRunArtifacts({
...github.context.repo,
run_id: runId,
});
const artifactData = artifactResp.data;
if (!("artifacts" in artifactData)) {
return "No Artifacts";
}
var download_url = null
for (const artifact of artifactData["artifacts"]) {
if (artifact["name"] == archive_name) {
if (artifact["expired"]) {
return "Artifact has expired";
} else {
download_url = artifact["archive_download_url"];
break;
}
}
}
if (!download_url) {
return "No Artifacts";
}
const token = utils.getToken();
const tempFile = 'tempfile'+crypto.randomBytes(4).readUInt32LE(0);
execSync(`curl -L -H "Authorization: token ${token}" ${download_url} -o ${tempFile}`)
var cmd = `unzip -p ${tempFile}`
if (modifier != null) {
cmd += ` | ${modifier}`
}
const output = execSync(cmd).toString()
fs.unlinkSync(tempFile)
return output
}
async function getPrMessageBlock(octokit, run, definition) {
var message = "";
message += "# " + definition["title"] + "\n";
for (const branch of definition["compare_branches"]) {
message += `## Previous ${branch} branch:\n\n`;
const data = await readArchivedFile(octokit, run, branch,
definition.artifact_name,
definition.message_file,
definition.modifier)
message += utils.formatMarkdownBlock(
data,
definition.collapsible
);
}
message += "\n## This change:\n\n";
const data = fs.readFileSync(definition["message_file"], 'utf8')
message += utils.formatMarkdownBlock(
utils.applyMessageModifier(data, definition["modifier"]),
definition.collapsible
);
return message
}
function processDefinition(definition) {
assert(
"message_file" in definition &&
"title" in definition,
"message_file & title must be included in the json definition"
)
if (!("artifact_name" in definition)) {
definition["artifact_name"] = definition["title"]
.replace(/[^0-9a-z ]/gi, "")
.replace(/ /g, "-")
.toLowerCase();
}
if (!("compare_branches" in definition)) {
definition["compare_branches"] = ["master"];
}
if (!("modifier" in definition)) {
definition["modifier"] = null;
}
if (!("collapsible" in definition)) {
definition["collapsible"] = false;
}
return definition
}
async function getPrMessage(octokit, definitions) {
const run = await utils.getRun(octokit);
var prMessage = ""
for (const definition of definitions) {
prMessage += await getPrMessageBlock(
octokit,
run,
definition)
}
return prMessage
}
async function postPrMessage(octokit, prNumber, prMessage) {
const res = await octokit.issues.createComment({
...github.context.repo,
issue_number: prNumber,
body: prMessage,
});
return res.data;
}
module.exports = {
readArchivedFile,
getPrMessageBlock,
getPrMessage,
processDefinition,
postPrMessage
}