gitea-release-action/main.js

202 lines
6.0 KiB
JavaScript
Raw Normal View History

2023-12-01 16:14:35 +08:00
import fs from "fs";
import { Blob } from "buffer";
2023-12-02 12:59:46 +08:00
import * as glob from "glob";
2023-12-01 16:14:35 +08:00
import core from "@actions/core";
import gitea from "gitea-api";
import path from 'path';
import CryptoJS from 'crypto-js';
2024-05-06 23:15:48 +08:00
function getIsTrue(v) {
const trueValue = ['true', 'True', 'TRUE']
return trueValue.includes(v)
}
2023-12-01 16:14:35 +08:00
async function run() {
try {
const server_url = core.getInput("server_url")
const name = core.getInput("name")
2023-12-04 09:43:35 +08:00
const body = getReleaseBody(core.getInput("body"), core.getInput("body_path"))
2023-12-01 16:14:35 +08:00
const tag_name = core.getInput("tag_name")
2024-05-06 23:15:48 +08:00
const draft = getIsTrue(core.getInput("draft"))
const prerelease = getIsTrue(core.getInput("prerelease"))
2023-12-01 16:14:35 +08:00
const files = core.getInput("files")
const repository = core.getInput("repository")
const token = core.getInput("token")
const target_commitish = core.getInput("target_commitish")
2024-05-06 23:15:48 +08:00
const md5sum = getIsTrue(core.getInput("md5sum"))
const sha256sum = getIsTrue(core.getInput("sha256sum"))
2023-12-01 16:14:35 +08: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 16:14:35 +08: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, {
md5sum: md5sum,
sha256sum: sha256sum,
})
2023-12-01 16:14:35 +08:00
console.log(`🎉 Release ready at ${response.html_url}`);
} catch (error) {
2023-12-01 16:24:33 +08:00
console.log(error);
2023-12-01 16:14:35 +08: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 16:14:35 +08: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 16:14:35 +08: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 13:18:10 +08:00
glob.sync(pattern).filter((path) => fs.statSync(path).isFile())
2023-12-01 16:14:35 +08:00
);
}, []);
};
/**
*
* @param {gitea.GiteaApi} client
* @param {String} owner
* @param {String} repo
* @param {Number} release_id
* @param {Array<String>} all_files
2024-02-21 17:34:53 +08:00
* @param {Map<String, Any>} additional parameters
2023-12-01 16:14:35 +08:00
*/
async function uploadFiles(client, owner, repo, release_id, all_files, params) {
params = params || {};
2023-12-01 16:14:35 +08:00
const attachments = await client.repository.repoListReleaseAttachments({
owner: owner,
repo: repo,
id: release_id,
})
2023-12-02 19:16:47 +08:00
for (const filepath of all_files) {
for (const attachment of attachments) {
let will_deleted = [path.basename(filepath), `${path.basename(filepath)}.md5`, `${path.basename(filepath)}.sha256`]
if (will_deleted.includes(attachment.name)) {
2023-12-01 16:14:35 +08:00
await client.repository.repoDeleteReleaseAttachment({
owner: owner,
repo: repo,
2024-02-15 19:15:47 +08:00
id: release_id,
2023-12-01 16:14:35 +08:00
attachmentId: attachment.id,
})
console.log(`Successfully deleted old release attachment ${attachment.name}`)
}
}
2023-12-02 19:09:42 +08:00
const content = fs.readFileSync(filepath);
let blob = new Blob([content]);
2023-12-02 19:09:42 +08:00
await client.repository.repoCreateReleaseAttachment({
owner: owner,
repo: repo,
id: release_id,
attachment: blob,
name: path.basename(filepath),
})
if (params.md5sum) {
let wordArray = CryptoJS.lib.WordArray.create(content);
let hash = CryptoJS.MD5(wordArray).toString();
blob = new Blob([hash], { type : 'plain/text' });
await client.repository.repoCreateReleaseAttachment({
owner: owner,
repo: repo,
id: release_id,
attachment: blob,
name: `${path.basename(filepath)}.md5`,
})
}
if (params.sha256sum) {
let wordArray = CryptoJS.lib.WordArray.create(content);
let hash = CryptoJS.SHA256(wordArray).toString();
blob = new Blob([hash], { type : 'plain/text' });
await client.repository.repoCreateReleaseAttachment({
owner: owner,
repo: repo,
id: release_id,
attachment: blob,
name: `${path.basename(filepath)}.sha256`,
})
}
2023-12-02 19:09:42 +08:00
console.log(`Successfully uploaded release attachment ${filepath}`)
2023-12-01 16:14:35 +08:00
}
}
2023-12-04 09:43:35 +08:00
function getReleaseBody(body, body_path) {
return (
(body_path && fs.readFileSync(body_path).toString("utf8")) || body
)
}
run()