release/main.js

164 lines
4.7 KiB
JavaScript
Raw Permalink Normal View History

2023-12-01 09:14:35 +01:00
import fs from "fs";
import { Blob } from "buffer";
2023-12-02 05:59:46 +01:00
import * as glob from "glob";
2023-12-01 09:14:35 +01:00
import core from "@actions/core";
import gitea from "gitea-api";
import path from 'path';
2023-12-01 09:14:35 +01:00
async function run() {
try {
const server_url = core.getInput("server_url")
const name = core.getInput("name")
2023-12-04 02:43:35 +01:00
const body = getReleaseBody(core.getInput("body"), core.getInput("body_path"))
2023-12-01 09:14:35 +01:00
const tag_name = core.getInput("tag_name")
2023-12-01 10:46:40 +01:00
const draft = Boolean(core.getInput("draft"))
const prerelease = Boolean(core.getInput("prerelease"))
2023-12-01 09:14:35 +01:00
const files = core.getInput("files")
const repository = core.getInput("repository")
const token = core.getInput("token")
const target_commitish = core.getInput("target_commitish")
2023-12-01 09:14:35 +01:00
const [owner, repo] = (repository).split("/")
const gitea_client = new gitea.GiteaApi({
BASE: `${server_url}/api/v1`,
WITH_CREDENTIALS: true,
TOKEN: token,
});
const response = await createOrGetRelease(gitea_client, owner, repo, {
body: body,
draft: draft,
name: name,
prerelease: prerelease,
tag_name: tag_name,
target_commitish: target_commitish,
2023-12-01 09:14:35 +01:00
})
const file_patterns = files.split('\n')
const all_files = paths(file_patterns);
if (all_files.length == 0) {
console.warn(`${file_patterns} not include valid file.`);
}
await uploadFiles(gitea_client, owner, repo, response.id, all_files)
console.log(`🎉 Release ready at ${response.html_url}`);
} catch (error) {
2023-12-01 09:24:33 +01:00
console.log(error);
2023-12-01 09:14:35 +01:00
core.setFailed(error.message);
}
}
/**
*
* @param {gitea.GiteaApi} client
* @param {String} owner
* @param {String} repo
* @param {gitea.CreateReleaseOption} body
* @returns {Promise<gitea.Release>}
*/
async function createOrGetRelease(client, owner, repo, body) {
try {
let release = await client.repository.repoGetReleaseByTag({
owner: owner,
repo: repo,
tag: body.tag_name,
})
const release_id = release.id;
let target_commitish = release.target_commitish;
if (body.target_commitish && body.target_commitish !== release.target_commitish) {
console.log(`Updating commit from "${release.target_commitish}" to "${body.target_commitish}"`);
}
target_commitish = body.target_commitish;
release = client.repository.repoEditRelease({
owner: owner,
repo: repo,
id: release_id,
body: {
body: body.body || release.body,
draft: body.draft !== undefined ? body.draft : release.draft,
name: body.name || release.name,
prerelease: body.prerelease !== undefined ? body.prerelease : release.prerelease,
tag_name: body.tag_name || release.tag_name,
target_commitish: target_commitish,
}
})
2023-12-01 09:14:35 +01:00
return release
} catch (error) {
if (!(error instanceof gitea.ApiError) || error.status !== 404) {
throw error
}
}
let commit_message = "";
if (body.target_commitish) {
commit_message = ` using commit "${body.target_commitish}"`;
}
console.log(`👩‍🏭 Creating new GitHub release for tag ${body.tag_name}${commit_message}...`);
2023-12-01 09:14:35 +01:00
let release = await client.repository.repoCreateRelease({
owner: owner,
repo: repo,
body: body,
})
return release
}
/**
*
* @param {Array<String>} patterns
* @returns {Array<String>}
*/
function paths(patterns) {
return patterns.reduce((acc, pattern) => {
return acc.concat(
2023-12-02 06:18:10 +01:00
glob.sync(pattern).filter((path) => fs.statSync(path).isFile())
2023-12-01 09:14:35 +01:00
);
}, []);
};
/**
*
* @param {gitea.GiteaApi} client
* @param {String} owner
* @param {String} repo
* @param {Number} release_id
* @param {Array<String>} all_files
*/
async function uploadFiles(client, owner, repo, release_id, all_files) {
const attachments = await client.repository.repoListReleaseAttachments({
owner: owner,
repo: repo,
id: release_id,
})
2023-12-02 12:16:47 +01:00
for (const filepath of all_files) {
for (const attachment of attachments) {
2023-12-01 09:14:35 +01:00
if (attachment.name === path.basename(filepath)) {
await client.repository.repoDeleteReleaseAttachment({
owner: owner,
repo: repo,
2024-02-15 12:15:47 +01:00
id: release_id,
2023-12-01 09:14:35 +01:00
attachmentId: attachment.id,
})
console.log(`Successfully deleted old release attachment ${attachment.name}`)
}
}
2023-12-02 12:09:42 +01:00
const content = fs.readFileSync(filepath);
const blob = new Blob([content]);
await client.repository.repoCreateReleaseAttachment({
owner: owner,
repo: repo,
id: release_id,
attachment: blob,
name: path.basename(filepath),
})
console.log(`Successfully uploaded release attachment ${filepath}`)
2023-12-01 09:14:35 +01:00
}
}
2023-12-04 02:43:35 +01:00
function getReleaseBody(body, body_path) {
return (
(body_path && fs.readFileSync(body_path).toString("utf8")) || body
)
}
run()