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";
|
2024-02-21 10:10:17 +01:00
|
|
|
import path from 'path';
|
2024-02-21 10:31:04 +01:00
|
|
|
import CryptoJS from 'crypto-js';
|
|
|
|
|
2024-05-06 17:15:48 +02:00
|
|
|
function getIsTrue(v) {
|
|
|
|
const trueValue = ['true', 'True', 'TRUE']
|
|
|
|
return trueValue.includes(v)
|
|
|
|
}
|
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")
|
2024-05-06 17:15:48 +02:00
|
|
|
const draft = getIsTrue(core.getInput("draft"))
|
|
|
|
const prerelease = getIsTrue(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")
|
2024-02-21 09:30:15 +01:00
|
|
|
const target_commitish = core.getInput("target_commitish")
|
2024-05-06 17:15:48 +02:00
|
|
|
const md5sum = getIsTrue(core.getInput("md5sum"))
|
|
|
|
const sha256sum = getIsTrue(core.getInput("sha256sum"))
|
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,
|
2024-02-21 09:30:15 +01:00
|
|
|
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.`);
|
|
|
|
}
|
2024-02-21 10:31:04 +01:00
|
|
|
await uploadFiles(gitea_client, owner, repo, response.id, all_files, {
|
|
|
|
md5sum: md5sum,
|
|
|
|
sha256sum: sha256sum,
|
|
|
|
})
|
2023-12-01 09:14:35 +01:00
|
|
|
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,
|
|
|
|
})
|
2024-02-21 09:30:15 +01:00
|
|
|
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,
|
2024-02-21 10:10:17 +01:00
|
|
|
target_commitish: target_commitish,
|
2024-02-21 09:30:15 +01:00
|
|
|
}
|
|
|
|
})
|
2023-12-01 09:14:35 +01:00
|
|
|
return release
|
|
|
|
} catch (error) {
|
|
|
|
if (!(error instanceof gitea.ApiError) || error.status !== 404) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 09:30:15 +01:00
|
|
|
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
|
2024-02-21 10:34:53 +01:00
|
|
|
* @param {Map<String, Any>} additional parameters
|
2023-12-01 09:14:35 +01:00
|
|
|
*/
|
2024-02-21 10:31:04 +01:00
|
|
|
async function uploadFiles(client, owner, repo, release_id, all_files, params) {
|
|
|
|
params = params || {};
|
2023-12-01 09:14:35 +01:00
|
|
|
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) {
|
2024-02-21 10:31:04 +01:00
|
|
|
let will_deleted = [path.basename(filepath), `${path.basename(filepath)}.md5`, `${path.basename(filepath)}.sha256`]
|
|
|
|
if (will_deleted.includes(attachment.name)) {
|
2023-12-01 09:14:35 +01:00
|
|
|
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);
|
2024-02-21 10:31:04 +01:00
|
|
|
let blob = new Blob([content]);
|
2023-12-02 12:09:42 +01:00
|
|
|
await client.repository.repoCreateReleaseAttachment({
|
|
|
|
owner: owner,
|
|
|
|
repo: repo,
|
|
|
|
id: release_id,
|
|
|
|
attachment: blob,
|
|
|
|
name: path.basename(filepath),
|
|
|
|
})
|
2024-02-21 10:31:04 +01:00
|
|
|
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 12:09:42 +01:00
|
|
|
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()
|