-
Notifications
You must be signed in to change notification settings - Fork 7
/
curseforge-upload.js
223 lines (216 loc) · 6.83 KB
/
curseforge-upload.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
const core = require("@actions/core");
const fs = require("fs");
const req = require("request");
async function getGameVersions(token, endpoint) {
let gameVersionsString = core.getInput("game_versions", { required: false });
let stringList = gameVersionsString.split(",");
let gameVersions = [];
let gameVersionNames = {};
stringList.forEach((valStr) => {
if (valStr != null && valStr != "") {
if (isID(valStr)) {
gameVersions.push(parseInt(valStr));
} else {
if (valStr.includes(":")) {
let valStrSp = valStr.split(":");
if (valStrSp.length != 2) {
core.setFailed(
"Invalid game version type id pair: " +
valStr +
" - Valid Format: typeid:versionid"
);
}
gameVersionNames[valStrSp[1]] = valStrSp[0];
} else {
gameVersionNames[valStr] = "blank";
}
}
}
});
if (Object.keys(gameVersionNames).length > 0) {
const options = {
method: "GET",
url: "https://" + endpoint + ".curseforge.com/api/game/versions",
port: 443,
headers: {
"X-Api-Token": token,
},
};
core.debug("Converting game version names to IDs via CF API request");
const versionData = JSON.parse(await requestPromise(options));
const optionsTypes = {
method: "GET",
url: "https://" + endpoint + ".curseforge.com/api/game/version-types",
port: 443,
headers: {
"X-Api-Token": token,
},
};
const versionTypeData =
Object.values(gameVersionNames).filter((v) => v != "blank").length > 0
? JSON.parse(await requestPromise(optionsTypes))
: [];
const filteredVersions = versionData.filter((v) => {
let typeId = gameVersionNames.hasOwnProperty(v.name)
? gameVersionNames[v.name]
: gameVersionNames.hasOwnProperty(v.slug)
? gameVersionNames[v.slug]
: "";
if (typeId != null && typeId != "blank" && typeId != "") {
if (isID(typeId)) {
return typeId == v.gameVersionTypeID;
} else {
let result = versionTypeData.filter(
(v) => v.name == typeId || v.slug == typeId
);
if (result.length != 1) {
core.setFailed("Cannot evaluate version type id " + typeId);
}
core.debug(
"Converted version type " + typeId + " to " + result[0].id
);
return result[0].id == v.gameVersionTypeID;
}
}
return typeId == "blank";
});
filteredVersions.forEach((v) => {
gameVersions.push(v.id);
core.debug("Converted " + v.slug + " to " + v.id);
});
}
return gameVersions;
}
function requestPromise(options) {
return new Promise((resolve, reject) => {
req(options, (err, response, body) => {
if (!err) {
if (response.statusCode == 200) {
resolve(body);
} else {
reject(response.statusCode + ": " + response.statusMessage);
}
} else {
reject(error);
}
});
}).catch((error) => core.setFailed(error.toString()));
}
function isID(str) {
return !(isNaN(str) || str.includes("."));
}
async function run() {
try {
const token = core.getInput("token", { required: true });
const projectId = core.getInput("project_id", { required: true });
if (!isID(projectId)) {
core.setFailed("Invalid project ID! (Must be an integer)");
}
const endpoint = core.getInput("game_endpoint", { required: true });
const filePath = core.getInput("file_path", { required: true });
if (!fs.existsSync(filePath)) {
core.setFailed("Specified file at " + filePath + " does not exist!");
}
const changelog = core.getInput("changelog", { required: false });
const changelogType = core.getInput("changelog_type", { required: false });
const displayName = core.getInput("display_name", { required: false });
const parentFileIDStr = core.getInput("parent_file_id", {
required: false,
});
if (!isID(parentFileIDStr)) {
core.setFailed("Invalid parent file ID! (Must be an integer)");
}
const gameVersions = await getGameVersions(token, endpoint);
const releaseType = core.getInput("release_type", { required: false });
const relationsString = core.getInput("relations", { required: false });
const projects = [];
relationsString.split(",").forEach((v) => {
if (v != null && v != "") {
const projectSplit = v.split(":");
projects.push({
slug: projectSplit[0],
type: projectSplit[1],
});
}
});
let metadata = {
changelog: changelog || "",
};
if (gameVersions.length > 0) {
metadata.gameVersions = gameVersions;
}
if (
releaseType != "" &&
releaseType != "alpha" &&
releaseType != "beta" &&
releaseType != "release"
) {
core.setFailed(
`Invalid release type: ${releaseType} (valid values: "alpha", "beta", "release")`
);
} else {
metadata.releaseType = releaseType == "" ? "release" : releaseType;
}
if (
changelogType != "" &&
changelogType != "markdown" &&
changelogType != "html" &&
changelogType != "text"
) {
core.setFailed(
`Invalid changelog type: ${changelogType} (valid values: "markdown", "html", "text")`
);
} else {
metadata.changelogType = changelogType || "markdown";
}
if (displayName != "") {
metadata.displayName = displayName;
}
if (parentFileIDStr != "") {
metadata.parentFileID = parseInt(parentFileIDStr);
}
if (relationsString != "") {
metadata.relations = { projects: projects };
}
core.debug("Request meta: " + JSON.stringify(metadata));
const options = {
method: "POST",
url:
"https://" +
endpoint +
".curseforge.com/api/projects/" +
projectId +
"/upload-file",
port: 443,
headers: {
"Content-Type": "multipart/form-data",
"X-Api-Token": token,
},
formData: {
file: fs.createReadStream(filePath),
metadata: JSON.stringify(metadata),
},
};
req.post(options, (err, response, body) => {
if (!err) {
core.debug("Response code: " + response.statusCode);
if (response.statusCode == 200) {
core.debug(`Response body:\n${response.body}`);
core.setOutput("id", JSON.parse(body).id.toString());
} else {
core.setFailed(
`${response.statusCode}: ${response.statusMessage}\nResponse body:\n${response.body}\nRequest body:${body}`
);
}
} else {
core.setFailed(
`Request error:${err}\nResponse body:\n${response.body}\nRequest body:${body}`
);
}
});
} catch (error) {
core.setFailed(error.message);
throw error;
}
}
run();