-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
189 lines (158 loc) · 5.97 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
const core = require('@actions/core');
const github = require('@actions/github');
const Fs = require("fs/promises");
const fetch = require("node-fetch");
async function apiCall(api, body) {
const url = `https://library.jmonkeyengine.org/${api}`;
// console.info("Fetch ", url, "with payload", body);
const data = await fetch(url, {
method: "POST",
body: JSON.stringify(body),
headers: {
"Content-Type": "application/json"
}
}).then(res => res.json());
// console.info("Response", data);
if (data.error) throw data.error;
return data;
}
async function main() {
try {
let importMedia = true;
if (typeof core.getInput('importMedia') != "undefined") {
importMedia = Boolean(core.getInput('importMedia'));
}
let funding = undefined;
if (typeof core.getInput('funding') != "undefined") {
funding = Boolean(core.getInput('funding'));
}
let platforms = undefined;
if (typeof core.getInput('platforms') != "undefined") {
platforms = core.getInput('platforms').split(",").map(p => p.trim());
}
const userId = core.getInput('userId');
const token = core.getInput('token');
const authKey = core.getInput('authKey');
const authId = core.getInput('authId');
const data = {};
if (core.getInput('data')) {
const inputData = JSON.parse(await Fs.readFile(core.getInput('data')));
for (const [k, v] of Object.entries(inputData)) {
if (v.value) data[k] = v.value;
else data[k] = v;
}
}
let repo = core.getInput('fetch-repo') ? (core.getInput('fetch-repo') == "current" ? github.context.repo : core.getInput('fetch-repo')) : undefined;
if (repo == "nil" || repo == "null" || repo == "undefined") repo = undefined;
let ref = undefined;
if (core.getInput('ref')) ref = core.getInput('ref');
else {
if (github.context.ref) {
const refpath = github.context.ref;
ref = refpath.substring(refpath.lastIndexOf("/") + 1);
}
if (!ref) ref = github.context.branch;
}
console.info("Publish ref", ref);
if (repo) {
// import entry data
const importedEntry = await apiCall("ext-import/github/entry", {
repo: `${repo.owner}/${repo.repo}`,
userId: userId,
token: token,
ref: ref
});
for (const [key, value] of Object.entries(importedEntry)) {
if (key == "platforms") continue; // hot fix, don't replace specified platforms
if (!data[key]) data[key] = value;
}
}
let entryId = data.entryId;
if (core.getInput('entryId')) {
entryId = core.getInput('entryId');
}
{
// Fetch old data
try {
let oldData = await apiCall("entry/get", {
userId: userId,
entryId: entryId,
authId: authId,
authKey: authKey
});
for (const [key, value] of Object.entries(oldData)) {
if (!data[key]) {
data[key] = value;
}
}
} catch (e) {
console.error(e);
}
// Update with new data
console.info("Set entry", data.entryId);
data.authId = authId;
data.authKey = authKey;
data.entryId = entryId;
if (typeof funding != "undefined") data.funding = funding;
if (typeof platforms != "undefined") data.platforms = platforms;
data.suspended = "Updating..."; // suspend during update
await apiCall("entry/set", data);
}
const media = [];
let lastMediaId = 0;
if (core.getInput('media-data-array')) {
const inputMediaR = JSON.parse(await Fs.readFile(core.getInput('media-data-array')));
for (const inputMedia of inputMediaR) {
const newMedia = {};
for (const [k, v] of Object.entries(inputMedia)) {
if (v.value) newMedia[k] = v.value;
else newMedia[k] = v;
}
newMedia.mediaId = lastMediaId;
lastMediaId++;
media.push(newMedia);
}
}
if (importMedia && repo) {
const importedMedia = [];
for (let mediaId = 0; ; mediaId++) {
try {
const mediaData = await apiCall("ext-import/github/media", {
repo: `${repo.owner}/${repo.repo}`,
userId: userId,
token: token,
mediaId: mediaId,
ref: ref
});
mediaData.mediaId = lastMediaId;
lastMediaId++;
importedMedia.push(mediaData);
} catch (e) {
break;
}
}
importedMedia.forEach(m => { if (m) media.push(m) });
}
// update media
for (const mediaData of media) {
console.info("Set media", mediaData.mediaId, "for entry", data.entryId);
try {
mediaData.entryId = entryId;
mediaData.authId = authId;
mediaData.authKey = authKey;
await apiCall("media/set", mediaData);
} catch (e) {
console.error(e);
}
}
// publish entry
{
console.info("Publish entry", data.entryId);
data.suspended = undefined;
await apiCall("entry/set", data);
}
} catch (error) {
core.setFailed(error.message);
}
}
main();