Pull the release asset uploading into a utility function
This commit is contained in:
parent
61e589112a
commit
97ca41c201
2 changed files with 30 additions and 25 deletions
|
|
@ -1,12 +1,22 @@
|
|||
import { createReadStream } from "fs";
|
||||
import axios from "axios";
|
||||
import { config } from "dotenv";
|
||||
import { assertEnvKey } from "./utils.mjs";
|
||||
import { assertEnvKey, addReleaseAsset } from "./utils.mjs";
|
||||
|
||||
config({ quiet: true });
|
||||
assertEnvKey("MANIFEST");
|
||||
assertEnvKey("TAG");
|
||||
assertEnvKey("FORGEJO_SERVER_URL");
|
||||
assertEnvKey("FORGEJO_API_URL");
|
||||
assertEnvKey("FORGEJO_REPOSITORY");
|
||||
assertEnvKey("FORGEJO_TOKEN");
|
||||
assertEnvKey("FORGEJO_REF_NAME");
|
||||
|
||||
const MANIFEST_FILE = process.env.MANIFEST;
|
||||
const MANIFEST_NAME = MANIFEST_FILE.split(`/`).at(-1);
|
||||
if (!MANIFEST_NAME) {
|
||||
console.error(`Failed to parse manifest name from: ${MANIFEST_FILE}`);
|
||||
process.exit(1);
|
||||
};
|
||||
|
||||
const {
|
||||
TAG,
|
||||
|
|
@ -17,21 +27,6 @@ const {
|
|||
FORGEJO_REF_NAME: TARGET_REF,
|
||||
} = process.env;
|
||||
|
||||
async function addReleaseAsset(releaseID, name, filepath) {
|
||||
const stream = createReadStream(filepath);
|
||||
return axios.post(
|
||||
`${API}/repos/${REPO}/releases/${releaseID}/assets`,
|
||||
{ attachment: stream, },
|
||||
{
|
||||
headers: {
|
||||
Authorization: `token ${TOKEN}`,
|
||||
"Content-Type": `multipart/form-data`,
|
||||
},
|
||||
params: { name },
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
// Initial Release Data
|
||||
const release = await axios.post(
|
||||
`${API}/repos/${REPO}/releases`,
|
||||
|
|
@ -48,15 +43,9 @@ const release = await axios.post(
|
|||
}
|
||||
);
|
||||
|
||||
const manifestName = MANIFEST_FILE.split(`/`).at(-1);
|
||||
if (!manifestName) {
|
||||
console.error(`Failed to parse manifest name from: ${MANIFEST_FILE}`);
|
||||
process.exit(1);
|
||||
};
|
||||
|
||||
try {
|
||||
await addReleaseAsset(release.data.id, `release.zip`, `release.zip`);
|
||||
await addReleaseAsset(release.data.id, manifestName, MANIFEST_FILE);
|
||||
await addReleaseAsset(release.data.upload_url, `release.zip`, `release.zip`);
|
||||
await addReleaseAsset(release.data.upload_url, MANIFEST_NAME, MANIFEST_FILE);
|
||||
} catch (e) {
|
||||
console.error(`Failed to add assets to the release`);
|
||||
process.exit(1);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { readFile } from "fs/promises";
|
||||
import { createReadStream } from "fs";
|
||||
|
||||
export async function getManifest(manifest) {
|
||||
let data = undefined;
|
||||
|
|
@ -18,3 +19,18 @@ export function assertEnvKey(key, { checkTruthiness = true } = {}) {
|
|||
process.exit(1);
|
||||
};
|
||||
};
|
||||
|
||||
export async function addReleaseAsset(url, name, filepath) {
|
||||
const stream = createReadStream(filepath);
|
||||
return axios.post(
|
||||
url,
|
||||
{ attachment: stream, },
|
||||
{
|
||||
headers: {
|
||||
Authorization: `token ${TOKEN}`,
|
||||
"Content-Type": `multipart/form-data`,
|
||||
},
|
||||
params: { name },
|
||||
}
|
||||
);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue