feat: support downloading goreleaser pro (#284)

This commit is contained in:
Carlos Alexandro Becker 2021-05-27 00:25:31 -03:00 committed by GitHub
parent a3b2f49e7c
commit 70eb4e573c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 140 additions and 47 deletions

View file

@ -1,36 +1,44 @@
import * as httpm from '@actions/http-client';
import * as core from '@actions/core';
import * as semver from 'semver';
import * as pro from './pro';
export interface GitHubRelease {
id: number;
tag_name: string;
}
export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
const resolvedVersion: string = (await resolveVersion(version)) || version;
const url: string = `https://github.com/goreleaser/goreleaser/releases/${resolvedVersion}`;
export const getRelease = async (distribution: string, version: string): Promise<GitHubRelease | null> => {
const resolvedVersion: string = (await resolveVersion(distribution, version)) || version;
const url: string = `https://github.com/goreleaser/${distribution}/releases/${resolvedVersion}`;
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
return (await http.getJson<GitHubRelease>(url)).result;
};
const resolveVersion = async (version: string): Promise<string | null> => {
const allTags: Array<string> | null = await getAllTags();
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`);
}
core.debug(`Found ${allTags.length} tags in total`);
return semver.maxSatisfying(allTags, version);
if (version === 'latest' || !pro.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) + pro.suffix(distribution);
};
interface GitHubTag {
tag_name: string;
}
const getAllTags = async (): Promise<Array<string>> => {
const getAllTags = async (distribution: string): Promise<Array<string>> => {
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
const url: string = `https://goreleaser.com/static/releases.json`;
const suffix: string = pro.suffix(distribution);
const url: string = `https://goreleaser.com/static/releases${suffix}.json`;
const getTags = http.getJson<Array<GitHubTag>>(url);
return getTags.then(response => {
@ -41,3 +49,7 @@ const getAllTags = async (): Promise<Array<string>> => {
return response.result.map(obj => obj.tag_name);
});
};
const cleanTag = (tag: string): string => {
return tag.replace(/-pro$/, '');
};