120 lines
3.7 KiB
JavaScript
120 lines
3.7 KiB
JavaScript
import fs from "fs";
|
|
import fetch from "node-fetch";
|
|
import FormData from "form-data";
|
|
import path from "path";
|
|
|
|
// node publish.js https://jusax.de/git/api/v1/ $${token} $${CI_REPO} $${CI_COMMIT_BRANCH} $${CI_COMMIT_TAG} build $${CI_COMMIT_MESSAGE}
|
|
|
|
let apiLink;
|
|
let token;
|
|
let repo;
|
|
let branch;
|
|
let tag;
|
|
let uploadDir;
|
|
let message;
|
|
|
|
try {
|
|
apiLink = process.argv[2];
|
|
if (typeof apiLink != "string" || apiLink.length < 5) throw new Error("Invalid apiLink!");
|
|
token = process.argv[3];
|
|
if (typeof token != "string" || token.length < 5) throw new Error("Invalid Token!");
|
|
repo = process.argv[4];
|
|
if (typeof repo != "string" || repo.length < 3) throw new Error("Invalid repo!");
|
|
branch = process.argv[5];
|
|
if (typeof branch != "string" || branch.length < 1) throw new Error("Invalid branch!");
|
|
tag = process.argv[6];
|
|
if (typeof tag != "string") throw new Error("Invalid tag!");
|
|
uploadDir = process.argv[7];
|
|
if (typeof tag != "string") throw new Error("Invalid uplaodDir!");
|
|
message = process.argv[8] || "";
|
|
} catch (error) {
|
|
console.error("Invalid args", error);
|
|
console.log("call with this Arguments: [apiLink] [token] [repo] [branch] [tag] [uploadDir] [message]");
|
|
process.exit(1);
|
|
}
|
|
if (tag.length < 1) {
|
|
console.log("No publish");
|
|
process.exit(0);
|
|
}
|
|
|
|
uploadDir = jpath(process.cwd(), uploadDir);
|
|
|
|
let uploadContent = fs.readdirSync(uploadDir, { withFileTypes: true });
|
|
|
|
let releaseID;
|
|
try {
|
|
releaseID = await createRelease(apiLink, repo, token, message, tag, branch);
|
|
} catch (error) {
|
|
console.error("Error while creating release: ", error);
|
|
process.exit(1);
|
|
}
|
|
try {
|
|
for (let i in uploadContent) {
|
|
let cont = uploadContent[i];
|
|
if (!cont.isFile()) continue;
|
|
console.log("try uploading: ",cont.name);
|
|
await uploadFile(apiLink, repo, token, releaseID, jpath(uploadDir, cont.name), cont.name);
|
|
console.log("done");
|
|
}
|
|
} catch (error) {
|
|
console.error("Error while uploading Files:", error);
|
|
process.exit(1);
|
|
}
|
|
|
|
function createRelease(apiLink, repo, token, message, tag, branch) {
|
|
return new Promise((res, rej) => {
|
|
fetch(apiLink + "repos/" + repo + "/releases?access_token=" + token, {
|
|
method: "post",
|
|
headers: {
|
|
accept: "application/json",
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify({
|
|
body: message,
|
|
draft: false,
|
|
name: tag,
|
|
tag_name: tag,
|
|
prerelease: true,
|
|
target_commitish: branch
|
|
})
|
|
})
|
|
.then(d => d.json())
|
|
.then(json => {
|
|
let releaseID = json.id;
|
|
if (isNaN(releaseID)) {
|
|
return void rej("Invalid Id!");
|
|
}
|
|
res(releaseID);
|
|
})
|
|
.catch((e) => {
|
|
rej(e);
|
|
})
|
|
});
|
|
}
|
|
|
|
function uploadFile(apiLink, repo, token, releaseID, path, name) {
|
|
return new Promise((res, rej) => {
|
|
const file = fs.createReadStream(path);
|
|
let data = new FormData();
|
|
data.append("attachment", file);
|
|
fetch(apiLink + "repos/" + repo + "/releases/" + releaseID + "/assets?name=" + name + "&access_token=" + token, {
|
|
method: "post",
|
|
headers: Object.assign({ accept: "application/json" }, data.getHeaders()),
|
|
body: data
|
|
})
|
|
.then(d => d.text())
|
|
.then(json => {
|
|
res();
|
|
})
|
|
.catch((e) => {
|
|
rej(e);
|
|
});
|
|
});
|
|
}
|
|
|
|
function jpath(a, b) {
|
|
if (b.startsWith("/")) {
|
|
return b;
|
|
}
|
|
return path.join(a, b);
|
|
}
|