mirror of
https://github.com/softprops/action-gh-release.git
synced 2026-02-14 18:55:43 +08:00
Previously, the releases were created and then artifacts (if any) were added to them. This broke when GitHub released "immutable" releases, which disallow changes after the release is published. Make it so that releases are always marked as "draft" when being worked on by the action, and unmarked as draft (if desired) once the action is completed. Fixes #653
99 lines
3.3 KiB
TypeScript
99 lines
3.3 KiB
TypeScript
import { setFailed, setOutput } from '@actions/core';
|
|
import { getOctokit } from '@actions/github';
|
|
import { GitHubReleaser, release, finalizeRelease, upload } from './github';
|
|
import { isTag, parseConfig, paths, unmatchedPatterns, uploadUrl } from './util';
|
|
|
|
import { env } from 'process';
|
|
|
|
async function run() {
|
|
try {
|
|
const config = parseConfig(env);
|
|
if (!config.input_tag_name && !isTag(config.github_ref) && !config.input_draft) {
|
|
throw new Error(`⚠️ GitHub Releases requires a tag`);
|
|
}
|
|
if (config.input_files) {
|
|
const patterns = unmatchedPatterns(config.input_files, config.input_working_directory);
|
|
patterns.forEach((pattern) => {
|
|
if (config.input_fail_on_unmatched_files) {
|
|
throw new Error(`⚠️ Pattern '${pattern}' does not match any files.`);
|
|
} else {
|
|
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`);
|
|
}
|
|
}
|
|
|
|
// 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}`);
|
|
},
|
|
},
|
|
});
|
|
//);
|
|
const releaser = new GitHubReleaser(gh);
|
|
let rel = await release(config, releaser);
|
|
if (config.input_files && config.input_files.length > 0) {
|
|
const files = paths(config.input_files, config.input_working_directory);
|
|
if (files.length == 0) {
|
|
if (config.input_fail_on_unmatched_files) {
|
|
throw new Error(`⚠️ ${config.input_files} does not include a valid file.`);
|
|
} else {
|
|
console.warn(`🤔 ${config.input_files} does not include a valid file.`);
|
|
}
|
|
}
|
|
const currentAssets = rel.assets;
|
|
|
|
const uploadFile = async (path) => {
|
|
const json = await upload(config, gh, uploadUrl(rel.upload_url), path, currentAssets);
|
|
if (json) {
|
|
delete json.uploader;
|
|
}
|
|
return json;
|
|
};
|
|
|
|
let results: (any | null)[];
|
|
if (!config.input_preserve_order) {
|
|
results = await Promise.all(files.map(uploadFile));
|
|
} else {
|
|
results = [];
|
|
for (const path of files) {
|
|
results.push(await uploadFile(path));
|
|
}
|
|
}
|
|
|
|
const assets = results.filter(Boolean);
|
|
setOutput('assets', assets);
|
|
}
|
|
|
|
console.log('Finalizing release...');
|
|
rel = await finalizeRelease(config, releaser, rel);
|
|
|
|
console.log(`🎉 Release ready at ${rel.html_url}`);
|
|
setOutput('url', rel.html_url);
|
|
setOutput('id', rel.id.toString());
|
|
setOutput('upload_url', rel.upload_url);
|
|
} catch (error) {
|
|
setFailed(error.message);
|
|
}
|
|
}
|
|
|
|
run();
|