feat: Use native GitHub Action tools to download assets and use GitHub API (#187)

* Use native GitHub Action tools to download assets and use GitHub API
* Fix unexpected output when tag not found
* Use GitHub Action exec
* Add screenshot

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-05-07 02:15:24 +02:00 committed by GitHub
parent 88f1da8c86
commit 6c7b10c265
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 269 additions and 23170 deletions

BIN
.github/goreleaser-action.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@ -20,12 +20,12 @@ jobs:
# https://github.com/actions/checkout
name: Checkout
uses: actions/checkout@v2
-
name: Unshallow
run: git fetch --prune --unshallow
-
name: Install
run: npm install
-
name: Build
run: npm run build
-
name: Test
run: npm run test

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2019 CrazyMax
Copyright (c) 2019-2020 CrazyMax
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -12,6 +12,8 @@
---
![GoRelease Action](.github/goreleaser-action.png)
## Usage
```yaml

17
__tests__/git.test.ts Normal file
View File

@ -0,0 +1,17 @@
import * as git from '../src/git';
describe('git', () => {
it('returns git tag', async () => {
const tag: string = await git.getTag();
console.log(tag);
expect(tag).not.toEqual('');
});
it('checks if tag is dirty', async () => {
expect(await git.isTagDirty('v1.3.1')).toBe(true);
});
it('returns short commit', async () => {
const commit: string = await git.getShortCommit();
console.log(commit);
expect(commit).not.toEqual('');
});
});

16
__tests__/github.test.ts Normal file
View File

@ -0,0 +1,16 @@
import * as github from '../src/github';
describe('github', () => {
it('returns latest GoReleaser GitHub release', async () => {
const release = await github.getRelease('latest');
console.log(release);
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
});
it('returns v0.117.0 GoReleaser GitHub release', async () => {
const release = await github.getRelease('v0.117.0');
console.log(release);
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.117.0');
});
});

View File

@ -4,11 +4,13 @@ import * as installer from '../src/installer';
describe('installer', () => {
it('acquires v0.117.0 version of GoReleaser', async () => {
const goreleaser = await installer.getGoReleaser('v0.117.0');
console.log(goreleaser);
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);
it('acquires latest version of GoReleaser', async () => {
const goreleaser = await installer.getGoReleaser('latest');
console.log(goreleaser);
expect(fs.existsSync(goreleaser)).toBe(true);
}, 100000);
});

22380
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

View File

@ -1,11 +1,12 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
setupFiles: ["dotenv/config"],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: true
verbose: false
}

834
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -24,15 +24,14 @@
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/exec": "^1.0.4",
"@actions/tool-cache": "^1.3.4",
"download": "^8.0.0",
"typed-rest-client": "^1.7.3"
"@actions/http-client": "^1.0.8",
"@actions/tool-cache": "^1.3.4"
},
"devDependencies": {
"@types/download": "^6.2.4",
"@types/jest": "^25.2.1",
"@types/node": "^13.13.5",
"@zeit/ncc": "^0.22.1",
"dotenv": "^8.2.0",
"jest": "^25.5.4",
"jest-circus": "^25.5.4",
"jest-runtime": "^25.5.4",

34
src/exec.ts Normal file
View File

@ -0,0 +1,34 @@
import * as actionsExec from '@actions/exec';
import {ExecOptions} from '@actions/exec';
export interface ExecResult {
success: boolean;
stdout: string;
stderr: string;
}
export const exec = async (command: string, args: string[] = [], silent: boolean): Promise<ExecResult> => {
let stdout: string = '';
let stderr: string = '';
const options: ExecOptions = {
silent: silent,
ignoreReturnCode: true
};
options.listeners = {
stdout: (data: Buffer) => {
stdout += data.toString();
},
stderr: (data: Buffer) => {
stderr += data.toString();
}
};
const returnCode: number = await actionsExec.exec(command, args, options);
return {
success: returnCode === 0,
stdout: stdout.trim(),
stderr: stderr.trim()
};
};

View File

@ -1,12 +1,22 @@
import * as child_process from 'child_process';
import * as exec from './exec';
const git = async (args: string[] = []) => {
const stdout = child_process.execSync(`git ${args.join(' ')}`, {
encoding: 'utf8'
const git = async (args: string[] = []): Promise<string> => {
return await exec.exec(`git`, args, true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(res.stderr);
}
return res.stdout.trim();
});
return stdout.trim();
};
export async function getTag(): Promise<string> {
try {
return await git(['describe', '--tags', '--abbrev=0']);
} catch (err) {
return '';
}
}
export async function isTagDirty(currentTag: string): Promise<boolean> {
try {
await git(['describe', '--exact-match', '--tags', '--match', currentTag]);
@ -16,14 +26,6 @@ export async function isTagDirty(currentTag: string): Promise<boolean> {
return false;
}
export async function getTag(): Promise<string> {
try {
return await git(['describe', '--tags', '--abbrev=0']);
} catch (err) {
return '';
}
}
export async function getShortCommit(): Promise<string> {
return await git(['show', "--format='%h'", 'HEAD', '--quiet']);
}

12
src/github.ts Normal file
View File

@ -0,0 +1,12 @@
import * as httpm from '@actions/http-client';
export interface GitHubRelease {
id: number;
tag_name: string;
}
export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
const url: string = `https://github.com/goreleaser/goreleaser/releases/${version}`;
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
return (await http.getJson<GitHubRelease>(url)).result;
};

View File

@ -1,68 +1,52 @@
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
import * as download from 'download';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as util from 'util';
import * as restm from 'typed-rest-client/RestClient';
import * as github from './github';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
let osPlat: string = os.platform();
let osArch: string = os.arch();
const osPlat: string = os.platform();
const osArch: string = os.arch();
export async function getGoReleaser(version: string): Promise<string> {
const selected = await determineVersion(version);
if (selected) {
version = selected;
const release: github.GitHubRelease | null = await github.getRelease(version);
if (!release) {
throw new Error(`Cannot find GoReleaser ${version} release`);
}
core.info(`✅ GoReleaser version found: ${version}`);
const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'goreleaser-'));
const fileName = getFileName();
core.info(`✅ GoReleaser version found: ${release.tag_name}`);
const filename = getFilename();
const downloadUrl = util.format(
'https://github.com/goreleaser/goreleaser/releases/download/%s/%s',
version,
fileName
release.tag_name,
filename
);
core.info(`⬇️ Downloading ${downloadUrl}...`);
await download.default(downloadUrl, tmpdir, {filename: fileName});
const downloadPath: string = await tc.downloadTool(downloadUrl);
core.debug(`Downloaded to ${downloadPath}`);
core.info('📦 Extracting GoReleaser...');
let extPath: string = tmpdir;
let extPath: string;
if (osPlat == 'win32') {
extPath = await tc.extractZip(`${tmpdir}/${fileName}`);
extPath = await tc.extractZip(downloadPath);
} else {
extPath = await tc.extractTar(`${tmpdir}/${fileName}`);
extPath = await tc.extractTar(downloadPath);
}
core.debug(`Extracted to ${extPath}`);
const cachePath: string = await tc.cacheDir(extPath, 'goreleaser-action', release.tag_name.replace(/^v/, ''));
core.debug(`Cached to ${cachePath}`);
const exePath: string = path.join(cachePath, osPlat == 'win32' ? 'goreleaser.exe' : 'goreleaser');
core.debug(`Exe path is ${exePath}`);
return exePath;
}
return path.join(extPath, osPlat == 'win32' ? 'goreleaser.exe' : 'goreleaser');
}
function getFileName(): string {
const getFilename = (): string => {
const platform: string = osPlat == 'win32' ? 'Windows' : osPlat == 'darwin' ? 'Darwin' : 'Linux';
const arch: string = osArch == 'x64' ? 'x86_64' : 'i386';
const ext: string = osPlat == 'win32' ? 'zip' : 'tar.gz';
return util.format('goreleaser_%s_%s.%s', platform, arch, ext);
}
interface GitHubRelease {
tag_name: string;
}
async function determineVersion(version: string): Promise<string> {
let rest: restm.RestClient = new restm.RestClient('goreleaser-action', 'https://github.com', undefined, {
headers: {
Accept: 'application/json'
}
});
let res: restm.IRestResponse<GitHubRelease> = await rest.get<GitHubRelease>(
`/goreleaser/goreleaser/releases/${version}`
);
if (res.statusCode != 200 || res.result === null) {
throw new Error(`Cannot find GoReleaser ${version} release (http ${res.statusCode})`);
}
return res.result.tag_name;
}
};

View File

@ -4,7 +4,7 @@ import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as fs from 'fs';
export async function run(silent?: boolean) {
async function run(): Promise<void> {
try {
const version = core.getInput('version') || 'latest';
const args = core.getInput('args');
@ -37,15 +37,11 @@ export async function run(silent?: boolean) {
core.info('🔑 Importing signing key...');
let path = `${process.env.HOME}/key.asc`;
fs.writeFileSync(path, key, {mode: 0o600});
await exec.exec('gpg', ['--import', path], {
silent: silent
});
await exec.exec('gpg', ['--import', path]);
}
core.info('🏃 Running GoReleaser...');
await exec.exec(`${goreleaser} ${args}${snapshot}`, undefined, {
silent: silent
});
await exec.exec(`${goreleaser} ${args}${snapshot}`);
} catch (error) {
core.setFailed(error.message);
}