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

@ -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) {