Skip to content

Commit

Permalink
address asset upload warning (#134)
Browse files Browse the repository at this point in the history
* bump version

* address asset upload warning

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

* format upload url

* unminify to debug

* try alt constructor

* utilize formatted upload url

* authorize upload

* pass token explicitly

* address conflicting uploads

* remove debugging artifacts
  • Loading branch information
softprops authored Aug 8, 2021
1 parent 59c3b48 commit 003621c
Show file tree
Hide file tree
Showing 8 changed files with 273 additions and 218 deletions.
4 changes: 2 additions & 2 deletions __tests__/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ describe("github", () => {

describe("asset", () => {
it("derives asset info from a path", async () => {
const { name, mime, size, file } = asset("tests/data/foo/bar.txt");
const { name, mime, size, data } = asset("tests/data/foo/bar.txt");
assert.equal(name, "bar.txt");
assert.equal(mime, "text/plain");
assert.equal(size, 10);
assert.equal(file.toString(), "release me");
assert.equal(data.toString(), "release me");
});
});
});
13 changes: 12 additions & 1 deletion __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,22 @@ import {
paths,
parseConfig,
parseInputFiles,
unmatchedPatterns
unmatchedPatterns,
uploadUrl
} from "../src/util";
import * as assert from "assert";

describe("util", () => {
describe("uploadUrl", () => {
it("stripts template", () => {
assert.equal(
uploadUrl(
"https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets{?name,label}"
),
"https://uploads.github.com/repos/octocat/Hello-World/releases/1/assets"
);
});
});
describe("parseInputFiles", () => {
it("parses empty strings", () => {
assert.deepStrictEqual(parseInputFiles(""), []);
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

350 changes: 169 additions & 181 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "action-gh-release",
"version": "0.1.7",
"version": "0.1.8",
"private": true,
"description": "GitHub Action for creating GitHub Releases",
"main": "lib/main.js",
Expand All @@ -20,18 +20,20 @@
"author": "softprops",
"license": "MIT",
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/github": "^2.0.0",
"@octokit/plugin-retry": "^3.0.2",
"@octokit/plugin-throttling": "^3.2.1",
"@actions/core": "^1.4.0",
"@actions/github": "^5.0.0",
"@octokit/plugin-retry": "^3.0.9",
"@octokit/plugin-throttling": "^3.5.1",
"glob": "^7.1.6",
"mime": "^2.4.4"
"mime": "^2.4.4",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"@types/glob": "^7.1.1",
"@types/jest": "^24.0.25",
"@types/mime": "^2.0.1",
"@types/node": "^12.12.24",
"@types/node-fetch": "^2.5.12",
"@zeit/ncc": "^0.21.0",
"jest": "^24.9.0",
"jest-circus": "^24.9.0",
Expand Down
63 changes: 45 additions & 18 deletions src/github.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
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;
size: number;
file: Buffer;
data: Buffer;
}

export interface Release {
id: number;
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;
assets: Array<{ id: number; name: string }>;
}

export interface Releaser {
Expand Down Expand Up @@ -70,7 +74,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: {
Expand All @@ -83,7 +87,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: {
Expand All @@ -97,7 +101,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: {
Expand All @@ -106,7 +110,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)
);
}
}
Expand All @@ -116,7 +120,7 @@ export const asset = (path: string): ReleaseAsset => {
name: basename(path),
mime: mimeOrDefault(path),
size: lstatSync(path).size,
file: readFileSync(path)
data: readFileSync(path)
};
};

Expand All @@ -125,21 +129,44 @@ export const mimeOrDefault = (path: string): string => {
};

export const upload = async (
gh: GitHub,
config: Config,
github: GitHub,
url: string,
path: string
path: string,
currentAssets: Array<{ id: number; name: string }>
): Promise<any> => {
let { name, size, mime, file } = asset(path);
const [owner, repo] = config.github_repository.split("/");
const { name, size, mime, data: body } = asset(path);
const currentAsset = currentAssets.find(
({ name: currentName }) => currentName == name
);
if (currentAsset) {
console.log(`♻️ Deleting previously uploaded asset ${name}...`);
await github.rest.repos.deleteReleaseAsset({
asset_id: currentAsset.id || 1,
owner,
repo
});
}
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-type": mime
"content-length": `${size}`,
"content-type": mime,
authorization: `token ${config.github_token}`
},
name,
file
method: "POST",
body
});
const json = await resp.json();
if (resp.status !== 201) {
throw new Error(
"Failed to upload release asset ${name}. recieved status code ${resp.status}\n${json.message}\n${json.errors}"
);
}
return json;
};

export const release = async (
Expand Down
37 changes: 28 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { paths, parseConfig, isTag, unmatchedPatterns } from "./util";
import {
paths,
parseConfig,
isTag,
unmatchedPatterns,
uploadUrl
} from "./util";
import { release, upload, GitHubReleaser } from "./github";
import { getOctokit } from "@actions/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() {
Expand All @@ -23,11 +31,14 @@ async function run() {
throw new Error(`⚠️ There were unmatched files`);
}
}
GitHub.plugin([
require("@octokit/plugin-throttling"),
require("@octokit/plugin-retry")
]);
const gh = new GitHub(config.github_token, {

// 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(
Expand All @@ -47,15 +58,23 @@ async function run() {
}
}
});
let rel = await release(config, new GitHubReleaser(gh));
//);
const rel = await release(config, new GitHubReleaser(gh));
if (config.input_files) {
const files = paths(config.input_files);
if (files.length == 0) {
console.warn(`🤔 ${config.input_files} not include valid file.`);
}
const currentAsserts = rel.assets;
await Promise.all(
files.map(async path => {
await upload(gh, rel.upload_url, path);
await upload(
config,
gh,
uploadUrl(rel.upload_url),
path,
currentAsserts
);
})
).catch(error => {
throw error;
Expand Down
8 changes: 8 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ export interface Config {
input_target_commitish?: string;
}

export const uploadUrl = (url: string): string => {
const templateMarkerPos = url.indexOf("{");
if (templateMarkerPos > -1) {
return url.substring(0, templateMarkerPos);
}
return url;
};

export const releaseBody = (config: Config): string | undefined => {
return (
(config.input_body_path &&
Expand Down

0 comments on commit 003621c

Please sign in to comment.