Files
action-gh-release/src/main.ts
T

96 lines
2.7 KiB
TypeScript
Raw Normal View History

2021-08-08 00:28:01 -04:00
import {
paths,
parseConfig,
isTag,
unmatchedPatterns,
uploadUrl,
2021-08-08 00:28:01 -04:00
} from "./util";
2019-09-17 19:27:32 +09:00
import { release, upload, GitHubReleaser } from "./github";
2021-08-08 00:28:01 -04:00
import { getOctokit } from "@actions/github";
2019-10-17 11:08:28 -04:00
import { setFailed, setOutput } from "@actions/core";
2021-08-08 00:28:01 -04:00
import { GitHub, getOctokitOptions } from "@actions/github/lib/utils";
2019-09-09 21:20:59 +09:00
import { env } from "process";
2019-09-09 17:10:07 +09:00
async function run() {
try {
2019-09-09 20:16:58 +09:00
const config = parseConfig(env);
if (
!config.input_tag_name &&
!isTag(config.github_ref) &&
!config.input_draft
) {
2019-09-09 17:10:07 +09:00
throw new Error(`⚠️ GitHub Releases requires a tag`);
}
2020-06-24 23:11:41 -07:00
if (config.input_files) {
const patterns = unmatchedPatterns(config.input_files);
patterns.forEach((pattern) =>
2020-06-24 23:11:41 -07:00
console.warn(`🤔 Pattern '${pattern}' does not match any files.`)
);
if (patterns.length > 0 && config.input_fail_on_unmatched_files) {
throw new Error(`⚠️ There were unmatched files`);
}
}
2021-08-08 00:28:01 -04:00
// const oktokit = GitHub.plugin(
// require("@octokit/plugin-throttling"),
// require("@octokit/plugin-retry")
// );
const gh = getOctokit(config.github_token, {
//new oktokit(
throttle: {
onRateLimit: (retryAfter, options) => {
console.warn(
`Request quota exhausted for request ${options.method} ${options.url}`
);
if (options.request.retryCount === 0) {
// only retries once
console.log(`Retrying after ${retryAfter} seconds!`);
return true;
}
},
onAbuseLimit: (retryAfter, options) => {
// does not retry, only logs a warning
console.warn(
`Abuse detected for request ${options.method} ${options.url}`
);
},
},
2019-10-02 20:51:12 -04:00
});
2021-08-08 00:28:01 -04:00
//);
const rel = await release(config, new GitHubReleaser(gh));
2019-09-09 17:10:07 +09:00
if (config.input_files) {
const files = paths(config.input_files);
2019-12-29 06:07:38 +08:00
if (files.length == 0) {
console.warn(`🤔 ${config.input_files} not include valid file.`);
2019-12-29 06:07:38 +08:00
}
2021-11-26 00:02:50 +01:00
const currentAssets = rel.assets;
const assets = await Promise.all(
files.map(async (path) => {
2021-11-26 00:02:50 +01:00
const json = await upload(
2021-08-08 00:28:01 -04:00
config,
gh,
uploadUrl(rel.upload_url),
path,
2021-11-26 00:02:50 +01:00
currentAssets
2021-08-08 00:28:01 -04:00
);
2021-11-26 00:02:50 +01:00
delete json.uploader;
return json;
})
).catch((error) => {
throw error;
2019-09-09 17:10:07 +09:00
});
2021-11-26 00:02:50 +01:00
setOutput("assets", assets);
2019-09-09 17:10:07 +09:00
}
2019-09-09 21:20:59 +09:00
console.log(`🎉 Release ready at ${rel.html_url}`);
2019-10-20 18:04:32 -04:00
setOutput("url", rel.html_url);
2021-05-02 00:56:49 +02:00
setOutput("id", rel.id.toString());
2021-03-21 06:59:32 +01:00
setOutput("upload_url", rel.upload_url);
2019-09-09 17:10:07 +09:00
} catch (error) {
2019-09-09 18:14:17 +09:00
setFailed(error.message);
2019-09-09 17:10:07 +09:00
}
}
2019-09-09 21:20:59 +09:00
run();