-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
117 lines (94 loc) · 3.87 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
const core = require("@actions/core");
const github = require("@actions/github");
const { SkynetClient, genKeyPairFromSeed } = require("@skynetlabs/skynet-nodejs");
function outputAxiosErrorMessage(error) {
if (error.response) {
const { path, method } = error.request;
const { status, statusText } = error.response;
console.log(`${method.toUpperCase()} ${path} failed with status ${status}: ${statusText}`);
console.log(error.response.data || "Response contained no data");
}
}
function prepareClientOptions() {
const options = {};
if (core.getInput("skynet-jwt")) {
// transform skynet-jwt into a cookie accepted format
options.customCookie = `skynet-jwt=${core.getInput("skynet-jwt")}`;
}
if (core.getInput("skynet-api-key")) {
// set Skynet Api Key
options.skynetApiKey = core.getInput("skynet-api-key");
}
return options;
}
function prepareUploadOptions() {
const options = {};
if (core.getInput("try-files")) {
// transform try-files input which is space separated list
// of file paths into an array of those paths
options.tryFiles = core.getInput("try-files").split(/\s+/);
}
if (core.getInput("not-found-page")) {
// transform not-found-page input which is a single file path into
// an object with a 404 key and its value being the specified path
options.errorPages = { 404: core.getInput("not-found-page") };
}
return options;
}
(async () => {
try {
// parse options and upload output directory to skynet
const clientOptions = prepareClientOptions();
const skynetClient = new SkynetClient(core.getInput("portal-url"), clientOptions);
const skylink = await skynetClient.uploadDirectory(core.getInput("upload-dir"), clientOptions);
// generate base32 skylink url from base64 skylink
const skylinkUrl = await skynetClient.getSkylinkUrl(skylink, {
subdomain: true,
});
core.setOutput("skylink", skylink);
console.log(`Skylink: ${skylink}`);
core.setOutput("skylink-url", skylinkUrl);
console.log(`Deployed to: ${skylinkUrl}`);
// if registry is properly configured, update the skylink in the entry
if (core.getInput("registry-seed") && core.getInput("registry-datakey")) {
try {
const seed = core.getInput("registry-seed");
const dataKey = core.getInput("registry-datakey");
const { publicKey, privateKey } = genKeyPairFromSeed(seed);
const [entryUrl, resolverSkylink] = await Promise.all([
skynetClient.registry.getEntryUrl(publicKey, dataKey),
skynetClient.registry.getEntryLink(publicKey, dataKey),
skynetClient.db.setDataLink(privateKey, dataKey, skylink),
]);
const resolverUrl = await skynetClient.getSkylinkUrl(resolverSkylink, {
subdomain: true,
});
console.log(`Registry entry updated: ${entryUrl}`);
core.setOutput("resolver-skylink-url", resolverUrl);
console.log(`Resolver Skylink Url: ${resolverUrl}`);
core.setOutput("resolver-skylink", resolverSkylink);
console.log(`Resolver Skylink: ${resolverSkylink}`);
} catch (error) {
outputAxiosErrorMessage(error);
console.log(`Failed to update registry entry: ${error.message}`);
}
}
// put a skylink in a pull request comment if available
if (github.context.issue.number) {
const gitHubToken = core.getInput("github-token");
const octokit = github.getOctokit(gitHubToken);
try {
await octokit.rest.issues.createComment({
...github.context.repo,
issue_number: github.context.issue.number,
body: `Deployed to ${skylinkUrl}<br>Skylink: \`${skylink}\``,
});
} catch (error) {
console.log(`Failed to create comment: ${error.message}`);
}
}
} catch (error) {
outputAxiosErrorMessage(error);
core.setFailed(`Failed to deploy: ${error.message}`);
}
})();