This commit is contained in:
softprops
2019-09-09 21:20:59 +09:00
parent 624fcca9a1
commit ef96a2eb52
7 changed files with 158 additions and 102 deletions

View File

@ -1,19 +1,19 @@
import { GitHub } from '@actions/github';
import { Config } from './util';
import { lstatSync, readFileSync } from 'fs';
import { getType } from 'mime';
import { basename } from 'path';
import { GitHub } from "@actions/github";
import { Config } from "./util";
import { lstatSync, readFileSync } from "fs";
import { getType } from "mime";
import { basename } from "path";
export interface ReleaseAsset {
name: string,
mime: string,
size: number,
file: Buffer
name: string;
mime: string;
size: number;
file: Buffer;
}
export interface Release {
upload_url: string,
html_url: string
upload_url: string;
html_url: string;
}
export const asset = (path: string): ReleaseAsset => {
@ -23,39 +23,31 @@ export const asset = (path: string): ReleaseAsset => {
size: lstatSync(path).size,
file: readFileSync(path)
};
}
};
export const mimeOrDefault = (path: string): string => {
return getType(path) || "application/octet-stream";
}
};
export const upload = async (
gh: GitHub,
url: string,
path: string
): Promise<any> => {
let {
name,
size,
mime,
file
} = asset(path);
let { name, size, mime, file } = asset(path);
console.log(`⬆️ Uploading ${name}...`);
return await gh.repos.uploadReleaseAsset({
url,
headers: {
headers: {
"content-length": size,
"content-type": mime
},
name,
file
});
}
};
export const release = async (
config: Config,
gh: GitHub
): Promise<Release> => {
export const release = async (config: Config, gh: GitHub): Promise<Release> => {
const [owner, repo] = config.github_repository.split("/");
const tag = config.github_ref.replace("refs/tags/", "");
try {
@ -88,8 +80,10 @@ export const release = async (
return release(config, gh);
}
} else {
console.log(`Unexpected error fetching GitHub release for tag ${config.github_ref}: ${error}`);
console.log(
`Unexpected error fetching GitHub release for tag ${config.github_ref}: ${error}`
);
throw error;
}
}
}
};

View File

@ -1,9 +1,8 @@
import { paths, parseConfig, isTag } from './util';
import { release, upload } from './github';
import { setFailed } from '@actions/core';
import { GitHub } from '@actions/github';
import { env } from 'process';
import { paths, parseConfig, isTag } from "./util";
import { release, upload } from "./github";
import { setFailed } from "@actions/core";
import { GitHub } from "@actions/github";
import { env } from "process";
async function run() {
try {
@ -14,15 +13,15 @@ async function run() {
const gh = new GitHub(config.github_token);
let rel = await release(config, gh);
if (config.input_files) {
paths(config.input_files).forEach(async (path) => {
await upload(gh, rel.upload_url, path)
paths(config.input_files).forEach(async path => {
await upload(gh, rel.upload_url, path);
});
}
console.log(`🎉 Release ready at ${rel.html_url}`)
console.log(`🎉 Release ready at ${rel.html_url}`);
} catch (error) {
console.log(`Error: ${error}`);
setFailed(error.message);
}
}
run();
run();

View File

@ -1,16 +1,16 @@
import * as glob from 'glob';
import { lstatSync } from 'fs';
import * as glob from "glob";
import { lstatSync } from "fs";
export interface Config {
github_token: string,
github_ref: string,
github_repository: string,
// user provided
input_name?: string,
input_body?: string,
input_body_path?: string,
input_files?: string[],
input_draft?: boolean,
github_token: string;
github_ref: string;
github_repository: string;
// user provided
input_name?: string;
input_body?: string;
input_body_path?: string;
input_files?: string[];
input_draft?: boolean;
}
type Env = { [key: string]: string | undefined };
@ -18,22 +18,24 @@ type Env = { [key: string]: string | undefined };
export const parseConfig = (env: Env): Config => {
return {
github_token: env.GITHUB_TOKEN || "",
github_ref: (env.GITHUB_REF || ""),
github_ref: env.GITHUB_REF || "",
github_repository: env.GITHUB_REPOSITORY || "",
input_name: env.INPUT_NAME,
input_body: env.INPUT_BODY,
input_body_path: env.INPUT_BODY_PATH,
input_files: (env.INPUT_FILES || "").split(","),
input_draft: env.INPUT_DRAFT === 'true'
}
}
input_draft: env.INPUT_DRAFT === "true"
};
};
export const paths = (patterns: string[]): string[] => {
return patterns.reduce((acc: string[], pattern: string): string[] => {
return acc.concat(glob.sync(pattern).filter(path => lstatSync(path).isFile()));
}, [])
}
return acc.concat(
glob.sync(pattern).filter(path => lstatSync(path).isFile())
);
}, []);
};
export const isTag = (ref: string): boolean => {
return ref.startsWith("refs/tags/")
}
return ref.startsWith("refs/tags/");
};