fix: use @action/github (#390)

* fix: use @action/github

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>

* Update README.md

Co-authored-by: CrazyMax <github@crazymax.dev>

* Update action.yml

Co-authored-by: CrazyMax <github@crazymax.dev>

---------

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
Co-authored-by: CrazyMax <github@crazymax.dev>
This commit is contained in:
Carlos Alexandro Becker 2023-01-27 23:22:07 -03:00 committed by GitHub
parent b1a238106b
commit 9754a253a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 834 additions and 29 deletions

View file

@ -10,6 +10,7 @@ export interface Inputs {
args: string;
workdir: string;
installOnly: boolean;
githubToken: string;
}
export async function getInputs(): Promise<Inputs> {
@ -18,6 +19,7 @@ export async function getInputs(): Promise<Inputs> {
version: core.getInput('version'),
args: core.getInput('args'),
workdir: core.getInput('workdir') || '.',
githubToken: core.getInput('github-token'),
installOnly: core.getBooleanInput('install-only')
};
}

View file

@ -2,30 +2,67 @@ import * as goreleaser from './goreleaser';
import * as semver from 'semver';
import * as core from '@actions/core';
import * as httpm from '@actions/http-client';
import * as github from '@actions/github';
export interface GitHubRelease {
export interface Release {
id: number;
tag_name: string;
}
export const getRelease = async (distribution: string, version: string): Promise<GitHubRelease | null> => {
const resolvedVersion: string = (await resolveVersion(distribution, version)) || version;
const url = `https://github.com/goreleaser/${distribution}/releases/${resolvedVersion}`;
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
return (await http.getJson<GitHubRelease>(url)).result;
const owner = 'goreleaser';
export const getRelease = async (
distribution: string,
version: string,
githubToken: string
): Promise<Release | null> => {
if (version === 'latest') {
return getLatestRelease(distribution, githubToken);
}
const tag: string = (await resolveVersion(distribution, version)) || version;
return getReleaseTag(distribution, tag, githubToken);
};
export const getReleaseTag = async (repo: string, tag: string, githubToken: string): Promise<Release> => {
core.info(`Getting tag ${resolveVersion}...`);
return (
await github
.getOctokit(githubToken, {
baseUrl: 'https://api.github.com'
})
.rest.repos.getReleaseByTag({
owner,
repo,
tag
})
.catch(error => {
throw new Error(`Cannot get ${repo} release ${tag}: ${error}`);
})
).data as Release;
};
export const getLatestRelease = async (repo: string, githubToken: string): Promise<Release> => {
core.info(`Getting tag latest...`);
return (
await github
.getOctokit(githubToken)
.rest.repos.getLatestRelease({
owner,
repo
})
.catch(error => {
throw new Error(`Cannot get latest release: ${error}`);
})
).data as Release;
};
const resolveVersion = async (distribution: string, version: string): Promise<string | null> => {
const allTags: Array<string> | null = await getAllTags(distribution);
if (!allTags) {
throw new Error(`Cannot find GoReleaser tags`);
throw new Error(`Cannot download ${distribution} tags`);
}
core.debug(`Found ${allTags.length} tags in total`);
if (version === 'latest' || !goreleaser.isPro(distribution)) {
return semver.maxSatisfying(allTags, version);
}
const cleanTags: Array<string> = allTags.map(tag => cleanTag(tag));
const cleanVersion: string = cleanTag(version);
return semver.maxSatisfying(cleanTags, cleanVersion) + goreleaser.distribSuffix(distribution);
@ -39,6 +76,7 @@ const getAllTags = async (distribution: string): Promise<Array<string>> => {
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
const suffix: string = goreleaser.distribSuffix(distribution);
const url = `https://goreleaser.com/static/releases${suffix}.json`;
core.info(`Downloading ${url}`);
const getTags = http.getJson<Array<GitHubTag>>(url);
return getTags.then(response => {
if (response.result == null) {

View file

@ -7,8 +7,8 @@ import * as github from './github';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
export async function install(distribution: string, version: string): Promise<string> {
const release: github.GitHubRelease | null = await github.getRelease(distribution, version);
export async function install(distribution: string, version: string, githubToken: string): Promise<string> {
const release: github.Release | null = await github.getRelease(distribution, version, githubToken);
if (!release) {
throw new Error(`Cannot find GoReleaser ${version} release`);
}

View file

@ -9,7 +9,7 @@ import * as exec from '@actions/exec';
async function run(): Promise<void> {
try {
const inputs: context.Inputs = await context.getInputs();
const bin = await goreleaser.install(inputs.distribution, inputs.version);
const bin = await goreleaser.install(inputs.distribution, inputs.version, inputs.githubToken);
core.info(`GoReleaser ${inputs.version} installed successfully`);
if (inputs.installOnly) {