-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
323 lines (302 loc) · 9.63 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
const core = require("@actions/core");
const github = require("@actions/github");
const { GitHub } = require("@actions/github/lib/utils");
const { Client } = require("@notionhq/client");
const { createTokenAuth } = require("@octokit/auth-token");
const { Octokit } = require("@octokit/core");
const {
restEndpointMethods,
} = require("@octokit/plugin-rest-endpoint-methods");
async function createCommit(notion, commits) {
let fileFormat = core.getInput("files_format");
if (core.getInput("token") === "") fileFormat = "none";
var files = await getFiles();
commits.forEach((commit) => {
const array = commit.message.split(/\r?\n/);
const title = array.shift();
let description = "";
array.forEach((element) => {
description += " " + element;
});
const task = commit.message.substring(commit.message.indexOf("atnt:") + 6);
// search for a page in the Notion database "Tasks" given the task name
const page = notion.pages.filter(
(page) => page.properties.title === task
)[0];
let filesBlock;
switch (fileFormat) {
case "text-list":
core.info("Formatting Notion Block for:");
core.info(files);
filesBlock = {
object: "block",
type: "toggle",
toggle: {
text: [
{
type: "text",
text: {
content: "Files",
link: null,
},
annotations: {
bold: true,
italic: false,
strikethrough: false,
underline: false,
code: false,
color: "default",
},
},
],
children: [
{
type: "paragraph",
paragraph: {
text: [
{
type: "text",
text: {
content: files,
},
},
],
},
},
],
},
};
break;
case "none":
core.info("No file will be listed");
break;
default:
core.setFailed(
"Other files list tipes not supported or file type not specified."
);
break;
}
notion.pages.create({
parent: {
database_id: core.getInput("notion_database"),
},
properties: {
title: {
title: [
{
type: "text",
text: {
content: title,
},
},
],
},
task: {
relation: [{ id: page.id }],
},
[core.getInput("commit_url")]: {
url: commit.url,
},
[core.getInput("commit_id")]: {
rich_text: [
{
type: "text",
text: {
content: commit.id,
},
},
],
},
[core.getInput("commit_description")]: {
rich_text: [
{
type: "text",
text: {
content: description,
},
},
],
},
[core.getInput("commit_project")]: {
multi_select: [
{
name: github.context.repo.repo,
},
],
},
},
children: [
{
object: "block",
type: "paragraph",
paragraph: {
text: [
{
type: "text",
text: {
content: description,
},
},
],
},
},
filesBlock,
],
});
});
}
(async () => {
try {
const notion = new Client({ auth: core.getInput("notion_secret") });
createCommit(notion, github.context.payload.commits);
} catch (error) {
core.setFailed(error.message);
}
})();
async function getFiles() {
try {
const MyOctokit = Octokit.plugin(restEndpointMethods);
const octokit = new MyOctokit({
auth: core.getInput("token", { required: true }),
});
const format = core.getInput("files_format", { required: true });
if (format !== "text-list") {
core.setFailed("file output format not supported.");
}
core.debug(`Payload keys: ${Object.keys(github.context.payload)}`);
const eventName = github.context.eventName;
switch (eventName) {
case "pull_request":
base = github.context.payload.pull_request.base.sha;
head = github.context.payload.pull_request.head.sha;
break;
case "push":
base = github.context.payload.before;
head = github.context.payload.after;
break;
default:
core.setFailed(
`This action only supports pull requests and pushes, ${github.context.eventName} events are not supported. ` +
"Please submit an issue on this action's GitHub repo if you believe this in correct."
);
}
core.info(`Base commit: ${base}`);
core.info(`Head commit: ${head}`);
if (!base || !head) {
core.setFailed(
`The base and head commits are missing from the payload for this ${github.context.eventName} event. ` +
"Please submit an issue on this action's GitHub repo."
);
base = "";
head = "";
}
const response = await octokit.repos.compareCommits({
base,
head,
owner: github.context.repo.owner,
repo: github.context.repo.repo,
});
if (response.status !== 200) {
core.setFailed(
`The GitHub API for comparing the base and head commits for this ${github.context.eventName} event returned ${response.status}, expected 200. ` +
"Please submit an issue on this action's GitHub repo."
);
}
if (response.data.status !== "ahead") {
core.setFailed(
`The head commit for this ${github.context.eventName} event is not ahead of the base commit. ` +
"Please submit an issue on this action's GitHub repo."
);
}
const files = response.data.files;
const all = [],
added = [],
modified = [],
removed = [],
renamed = [],
addedModified = [];
for (const file of files) {
const filename = file.filename;
if (format === "text-list" && filename.includes(" ")) {
core.setFailed(
`One of your files includes a space. Consider using a different output format or removing spaces from your filenames. ` +
"Please submit an issue on this action's GitHub repo."
);
}
all.push(filename);
switch (file.status) {
case "added":
added.push(filename);
addedModified.push(filename);
break;
case "modified":
modified.push(filename);
addedModified.push(filename);
break;
case "removed":
removed.push(filename);
break;
case "renamed":
renamed.push(filename);
break;
default:
core.setFailed(
`One of your files includes an unsupported file status '${file.status}', expected 'added', 'modified', 'removed', or 'renamed'.`
);
}
}
let allFormatted,
addedFormatted,
modifiedFormatted,
removedFormatted,
renamedFormatted,
addedModifiedFormatted;
switch (format) {
case "text-list":
for (const file of all) {
if (file.includes(" "))
core.setFailed(
`One of your files includes a space. Consider using a different output format or removing spaces from your filenames.`
);
}
allFormatted = all.join(" ");
addedFormatted = added.join(" ");
modifiedFormatted = modified.join(" ");
removedFormatted = removed.join(" ");
renamedFormatted = renamed.join(" ");
addedModifiedFormatted = addedModified.join(" ");
break;
case "csv":
allFormatted = all.join(",");
addedFormatted = added.join(",");
modifiedFormatted = modified.join(",");
removedFormatted = removed.join(",");
renamedFormatted = renamed.join(",");
addedModifiedFormatted = addedModified.join(",");
break;
case "json":
allFormatted = JSON.stringify(all);
addedFormatted = JSON.stringify(added);
modifiedFormatted = JSON.stringify(modified);
removedFormatted = JSON.stringify(removed);
renamedFormatted = JSON.stringify(renamed);
addedModifiedFormatted = JSON.stringify(addedModified);
break;
}
// Log the output values.s
// core.info(`All: ${allFormatted}`);
// core.info(`Added: ${addedFormatted}`);
// core.info(`Modified: ${modifiedFormatted}`);
// core.info(`Removed: ${removedFormatted}`);
// core.info(`Renamed: ${renamedFormatted}`);
// core.info(`Added or modified: ${addedModifiedFormatted}`);
let outPutMessage =
(addedFormatted != "" ? "Added: \n" + addedFormatted : "") +
(modifiedFormatted != "" ? "Modified \n" + modifiedFormatted : "") +
(removedFormatted != "" ? "Removed: \n" + removedFormatted : "") +
(renamedFormatted != "" ? "Renamed: \n" + renamedFormatted : "");
return outPutMessage;
} catch (error) {
core.info("error " + error + " occurred");
}
}