-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.mjs
188 lines (161 loc) · 7.52 KB
/
index.mjs
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
import { readFile } from 'node:fs';
import { basename, extname, parse } from 'node:path';
import fetch from 'node-fetch';
import FormData from 'form-data';
import glob from '@actions/glob';
import core from '@actions/core';
import { context, getOctokit } from '@actions/github';
import Catbox from 'catbox.moe';
const defaultHost = 'https://litterbox.catbox.moe/resources/internals/api.php';
async function run() {
if (!context.payload.pull_request) {
return core.setFailed('Failed to run action, only ment for pull requests!');
}
3;
try {
const token = core.getInput('GITHUB_TOKEN', { required: true });
const pathGlob = core.getInput('path', { required: true });
const title = core.getInput('title');
const IMG_ENDPOINT = core.getInput('uploadHost') || defaultHost;
const annotationTag = core.getInput('annotationTag') || '[--]';
const annotationLevel = core.getInput('annotationLevel') || 'notice';
const octokit = getOctokit(token);
const globber = await glob.create(pathGlob, { followSymbolicLinks: false, matchDirectories: false });
const files = await globber.glob();
if (!files || files.length <= 0) return core.setFailed(`Failed to find files on path {${pathGlob}}`);
// UPLOAD FILES --------------------------
const litter = new Catbox.Litterbox();
const urlPromises = files.map(
(file) =>
new Promise(async (resolve, reject) => {
const name = basename(file);
console.log(`Uploading file '${name}'`);
if (IMG_ENDPOINT === defaultHost) {
litter
.upload(file, '24h')
.then((url) => {
console.log(`Uploaded to ${url}`);
resolve({
file: file,
url: url.trim(),
});
})
.catch((err) => {
return reject(`Failed to upload {${file}} : ${err}`);
});
return;
} else {
readFile(file, (err, buffer) => {
const form = new FormData();
form.append('file', buffer, {
name: name,
filename: name,
});
fetch(IMG_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': `image/${extname(file)}`,
},
body: form,
})
.then((res) => res.text())
.then((url) => {
if (!url.startsWith('http')) {
return reject(`Failed to upload {${file}} : ${url}`);
}
console.log(`Uploaded to ${url}`);
resolve({
file: file,
url: url.trim(),
});
})
.catch(() => reject(`Failed to upload {${file}}`));
});
}
}),
);
const urls = await Promise.all(urlPromises).catch((err) => {
core.setFailed(err);
});
if (!urls || urls.length <= 0) return core.setFailed(`Failed to upload files to the provider`);
// --------------------------
// GENERATE ANNOTATIONS --------------------------
const validateBase64 = function (encoded1) {
var decoded1 = Buffer.from(encoded1, 'base64').toString('utf8');
var encoded2 = Buffer.from(decoded1, 'binary').toString('base64');
return encoded1 == encoded2;
};
const promises = urls.map(
(urlData) =>
new Promise((resolve, reject) => {
const cleanFile = parse(urlData.file).name;
if (!validateBase64(cleanFile)) {
return resolve({ imageUrl: urlData.url });
}
const base64Decode = Buffer.from(cleanFile, 'base64').toString('ascii');
if (base64Decode.indexOf(annotationTag) === -1) return resolve({ imageUrl: urlData.url });
const fileData = base64Decode.split(annotationTag);
if (!fileData || fileData.length < 1)
return reject(`Invalid annotation file name, should be {filePath${annotationTag}line:col}`);
// Normalize the path from \\ to / and remove any "./" "/" at start
let filePath = fileData[0].replace(/\\/g, '/').replace('./', '');
if (filePath.startsWith('/')) filePath = filePath.substring(1);
const lineCol = fileData[1].split(':');
if (!lineCol || lineCol.length !== 2) return reject('Invalid annotation file name');
const line = lineCol[0];
const branch = context.payload.pull_request.head.ref;
const fileUrl = `${context.payload.repository.html_url}/blob/${branch}/${filePath}#L${line}`;
return resolve({
imageUrl: urlData.url,
data: {
path: filePath,
end_line: parseInt(line),
start_line: parseInt(line),
annotation_level: annotationLevel,
message: fileUrl,
},
});
}),
);
const annotationData = await Promise.all(promises).catch((err) => core.setFailed(err));
if (!annotationData || annotationData.length <= 0) return core.setFailed('Failed to generate comments / annotations');
const annotations = [];
const images = [];
annotationData.forEach((anno) => {
if (!anno) return;
if (anno.data) annotations.push(anno.data);
images.push({
alt: 'Image',
image_url: anno.imageUrl,
});
});
// --------------------------
// UPLOAD ANNOTATIONS --------------------------
octokit.rest.checks
.create({
head_sha: context.payload.pull_request.head.sha,
name: '@edunad/actions-image',
owner: context.repo.owner,
repo: context.repo.repo,
completed_at: new Date().toISOString(),
conclusion: 'success',
status: 'completed',
output: {
summary: '',
title: title,
annotations: annotations,
images: images,
},
})
.then(() => {
console.warn('Done creating annotations');
})
.catch((err) => {
core.setFailed(err.message);
});
// --------------------------
} catch (err) {
core.setFailed(err.message);
}
}
run();