mirror of
https://github.com/daydreamer-json/ak-endfield-api-archive.git
synced 2026-04-06 23:52:21 +02:00
feat: add resource files archiving
This commit is contained in:
@@ -4,18 +4,14 @@ import appConfig from './config.js';
|
||||
import logger from './logger.js';
|
||||
|
||||
async function uploadAsset(
|
||||
client: Octokit | null,
|
||||
authCfg: {
|
||||
github: {
|
||||
relArchive: { token: string; owner: string; repo: string; tag: string };
|
||||
main: { token: string; owner: string; repo: string };
|
||||
};
|
||||
} | null,
|
||||
client: Octokit,
|
||||
owner: string,
|
||||
repo: string,
|
||||
tag: string,
|
||||
url: string,
|
||||
targetFileName: string | null,
|
||||
) {
|
||||
if (!client || !authCfg) return;
|
||||
const release = await getReleaseInfo(client, authCfg);
|
||||
const release = await getReleaseInfo(client, owner, repo, tag);
|
||||
if (!release) throw new Error('GH release not found');
|
||||
const releaseId = release.id;
|
||||
|
||||
@@ -25,53 +21,87 @@ async function uploadAsset(
|
||||
const binSize: number = bin.byteLength;
|
||||
logger.info(`Mirror archive: Uploading ${new URL(url).pathname.split('/').pop()} ...`);
|
||||
await client.rest.repos.uploadReleaseAsset({
|
||||
owner: authCfg.github.relArchive.owner,
|
||||
repo: authCfg.github.relArchive.repo,
|
||||
owner,
|
||||
repo,
|
||||
release_id: releaseId,
|
||||
name,
|
||||
data: bin as any,
|
||||
headers: { 'content-length': binSize },
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
async function getReleaseInfo(
|
||||
client: Octokit | null,
|
||||
authCfg: {
|
||||
github: {
|
||||
relArchive: { token: string; owner: string; repo: string; tag: string };
|
||||
main: { token: string; owner: string; repo: string };
|
||||
};
|
||||
} | null,
|
||||
async function uploadAssetWithBuffer(
|
||||
client: Octokit,
|
||||
owner: string,
|
||||
repo: string,
|
||||
tag: string,
|
||||
targetFileName: string,
|
||||
buffer: Uint8Array,
|
||||
) {
|
||||
if (!client || !authCfg) return;
|
||||
const { data: release } = await client.rest.repos.getReleaseByTag({
|
||||
owner: authCfg.github.relArchive.owner,
|
||||
repo: authCfg.github.relArchive.repo,
|
||||
tag: authCfg.github.relArchive.tag,
|
||||
const release = await getReleaseInfo(client, owner, repo, tag);
|
||||
if (!release) throw new Error('GH release not found');
|
||||
const releaseId = release.id;
|
||||
|
||||
logger.info(`Mirror archive: Uploading to ${tag}, ${targetFileName} ...`);
|
||||
await client.rest.repos.uploadReleaseAsset({
|
||||
owner,
|
||||
repo,
|
||||
release_id: releaseId,
|
||||
name: targetFileName,
|
||||
data: buffer as any,
|
||||
headers: { 'content-length': buffer.byteLength },
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
async function getReleaseInfo(client: Octokit, owner: string, repo: string, tag: string) {
|
||||
const { data: release } = await client.rest.repos.getReleaseByTag({ owner, repo, tag });
|
||||
return release;
|
||||
}
|
||||
|
||||
async function checkIsActionRunning(
|
||||
authCfg: {
|
||||
github: {
|
||||
relArchive: { token: string; owner: string; repo: string; tag: string };
|
||||
main: { token: string; owner: string; repo: string };
|
||||
};
|
||||
} | null,
|
||||
): Promise<boolean> {
|
||||
if (!authCfg) return false;
|
||||
async function checkIsActionRunning(client: Octokit, owner: string, repo: string): Promise<boolean> {
|
||||
logger.debug('Checking GitHub Actions running status ...');
|
||||
const client = new Octokit({ auth: authCfg.github.main.token });
|
||||
const data = await client.rest.actions.listWorkflowRunsForRepo({
|
||||
owner: authCfg.github.main.owner,
|
||||
repo: authCfg.github.main.repo,
|
||||
});
|
||||
const data = await client.rest.actions.listWorkflowRunsForRepo({ owner, repo });
|
||||
return data.data.workflow_runs.filter((e) => e.status === 'in_progress').length > 1;
|
||||
}
|
||||
|
||||
async function createNewRelease(
|
||||
client: Octokit,
|
||||
owner: string,
|
||||
repo: string,
|
||||
tag: string,
|
||||
title: string,
|
||||
note: string,
|
||||
preRelFlag: boolean,
|
||||
draftFlag: boolean = false,
|
||||
targetCommitish: string = 'main',
|
||||
) {
|
||||
const { data } = await client.rest.repos.createRelease({
|
||||
owner,
|
||||
repo,
|
||||
tag_name: tag,
|
||||
name: title,
|
||||
body: note,
|
||||
draft: draftFlag,
|
||||
prerelease: preRelFlag,
|
||||
target_commitish: targetCommitish,
|
||||
});
|
||||
return data;
|
||||
}
|
||||
|
||||
async function deleteReleaseTag(client: Octokit, owner: string, repo: string, tag: string) {
|
||||
const { data: release } = await client.rest.repos.getReleaseByTag({ owner, repo, tag });
|
||||
await client.rest.repos.deleteRelease({ owner, repo, release_id: release.id });
|
||||
const data = await client.rest.git.deleteRef({ owner, repo, ref: `tags/${tag}` });
|
||||
return data;
|
||||
}
|
||||
|
||||
export default {
|
||||
uploadAsset,
|
||||
uploadAssetWithBuffer,
|
||||
getReleaseInfo,
|
||||
checkIsActionRunning,
|
||||
createNewRelease,
|
||||
deleteReleaseTag,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user