-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (92 loc) · 2.89 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
import { getInput, setFailed } from '@actions/core';
import { readFile, stat } from 'fs/promises';
import axios from 'axios';
const getInputs = () => {
const inputs = {
pluginId: getInput('plugin_id'),
internalName: getInput('internal_name'),
versionNumber: getInput('version_number'),
path: getInput('path'),
type: getInput('type') || 'testing',
gameVersion: getInput('game_version') || 'any',
dalamudVersion: getInput('dalamud_version') || '9',
changelog: getInput('changelog') || '',
};
if (!inputs.pluginId) throw new Error('Missing plugin ID');
if (!inputs.internalName) throw new Error('Missing internal name');
if (!inputs.versionNumber) throw new Error('Missing version number');
if (!inputs.path) throw new Error('Missing path');
if (!process.env.PUBLISHER_KEY) throw new Error('Missing publisher key');
return inputs;
};
const tryFetch = async (method, url, body) => {
try {
const resp = await fetch(url, { method, body });
const text = await resp.text();
if (resp.status >= 400) {
throw new Error(text);
}
return text;
} catch (e) {
throw e;
}
};
const tryUploadFile = async (url, file, size) =>
new Promise((resolve, reject) => {
axios
.put(url, file, {
onUploadProgress: (e) => {
console.log(
'Upload progress:',
Math.round((e.loaded * 100) / (e.total ?? size)),
);
},
})
.then(() => {
resolve(true);
})
.catch((e) => {
reject(e);
});
});
const tryReadFileInfo = async (path) => {
try {
return await stat(path);
} catch (e) {
throw new Error('Failed to read file info:', e.message);
}
};
const tryReadFileData = async (path) => {
try {
return await readFile(path);
} catch (e) {
throw new Error('Failed to read file data:', e.message);
}
};
const run = async () => {
const inputs = getInputs();
const apiUrl = `https://puni.sh/api/plugins/download/${inputs.pluginId}/${inputs.internalName}/versions/${inputs.type}?versionNum=${inputs.versionNumber}&publisherKey=${process.env.PUBLISHER_KEY}`;
console.log('Trying to fetch presigned URL');
const presignedUrl = await tryFetch('PUT', apiUrl);
console.log('Got presigned URL');
const fileInfo = await tryReadFileInfo(inputs.path);
const fileData = await tryReadFileData(inputs.path);
console.log('Trying to upload file to presigned URL');
await tryUploadFile(presignedUrl, fileData, fileInfo.size);
console.log('Uploaded file');
console.log('Trying to publish new version');
const versionId = await tryFetch(
'POST',
apiUrl,
JSON.stringify({
gameVersion: inputs.gameVersion,
dalamudVersion: inputs.dalamudVersion,
changelog: inputs.changelog,
}),
);
console.log('Published new version with ID ', versionId);
};
run().catch((err) => {
console.error(err);
setFailed(err.message);
});