refactor to new octokit api, work around release asset upload api

This commit is contained in:
softprops 2021-08-04 00:46:55 -04:00
parent 4ea9fd8322
commit 11282f4ee9
5 changed files with 230 additions and 221 deletions

View file

@ -1,9 +1,12 @@
import { GitHub } from "@actions/github";
import fetch from "node-fetch";
import { GitHub } from "@actions/github/lib/utils";
import { Config, isTag, releaseBody } from "./util";
import { lstatSync, readFileSync } from "fs";
import { getType } from "mime";
import { basename } from "path";
type GitHub = InstanceType<typeof GitHub>;
export interface ReleaseAsset {
name: string;
mime: string;
@ -16,8 +19,8 @@ export interface Release {
upload_url: string;
html_url: string;
tag_name: string;
name: string;
body: string;
name: string | null;
body?: string | null | undefined;
target_commitish: string;
draft: boolean;
prerelease: boolean;
@ -70,7 +73,7 @@ export class GitHubReleaser implements Releaser {
repo: string;
tag: string;
}): Promise<{ data: Release }> {
return this.github.repos.getReleaseByTag(params);
return this.github.rest.repos.getReleaseByTag(params);
}
createRelease(params: {
@ -83,7 +86,7 @@ export class GitHubReleaser implements Releaser {
prerelease: boolean | undefined;
target_commitish: string | undefined;
}): Promise<{ data: Release }> {
return this.github.repos.createRelease(params);
return this.github.rest.repos.createRelease(params);
}
updateRelease(params: {
@ -97,7 +100,7 @@ export class GitHubReleaser implements Releaser {
draft: boolean | undefined;
prerelease: boolean | undefined;
}): Promise<{ data: Release }> {
return this.github.repos.updateRelease(params);
return this.github.rest.repos.updateRelease(params);
}
allReleases(params: {
@ -106,7 +109,7 @@ export class GitHubReleaser implements Releaser {
}): AsyncIterableIterator<{ data: Release[] }> {
const updatedParams = { per_page: 100, ...params };
return this.github.paginate.iterator(
this.github.repos.listReleases.endpoint.merge(updatedParams)
this.github.rest.repos.listReleases.endpoint.merge(updatedParams)
);
}
}
@ -129,17 +132,29 @@ export const upload = async (
url: string,
path: string
): Promise<any> => {
let { name, size, mime, data } = asset(path);
let { name, size, mime, data: body } = asset(path);
console.log(`⬆️ Uploading ${name}...`);
return await gh.repos.uploadReleaseAsset({
url,
const endpoint = new URL(url);
endpoint.searchParams.append("name", name);
const resp = await fetch(endpoint, {
headers: {
"content-length": size,
"content-length": `${size}`,
"content-type": mime
},
name,
data
method: "POST",
body
});
return resp.json();
// return await gh.rest.repos.uploadReleaseAsset({
// url,
// headers: {
// "content-length": size,
// "content-type": mime,
// },
// name,
// file,
// });
};
export const release = async (

View file

@ -1,7 +1,8 @@
import { paths, parseConfig, isTag, unmatchedPatterns } from "./util";
import { release, upload, GitHubReleaser } from "./github";
import { setFailed, setOutput } from "@actions/core";
import { GitHub } from "@actions/github";
import { GitHub, getOctokitOptions } from "@actions/github/lib/utils";
import { env } from "process";
async function run() {
@ -23,30 +24,33 @@ async function run() {
throw new Error(`⚠️ There were unmatched files`);
}
}
GitHub.plugin([
const oktokit = GitHub.plugin(
require("@octokit/plugin-throttling"),
require("@octokit/plugin-retry")
]);
const gh = new GitHub(config.github_token, {
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;
);
const gh = new oktokit(
getOctokitOptions(config.github_token, {
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}`
);
}
},
onAbuseLimit: (retryAfter, options) => {
// does not retry, only logs a warning
console.warn(
`Abuse detected for request ${options.method} ${options.url}`
);
}
}
});
})
);
let rel = await release(config, new GitHubReleaser(gh));
if (config.input_files) {
const files = paths(config.input_files);