feat: implement (optional) semver parsing of version (#213)

This commit is contained in:
Radek Simko 2020-06-15 17:05:51 +01:00 committed by GitHub
parent de0cc32957
commit 62b67a060c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 1572 additions and 9 deletions

View file

@ -1,4 +1,6 @@
import * as httpm from '@actions/http-client';
import * as core from '@actions/core';
import * as semver from 'semver';
export interface GitHubRelease {
id: number;
@ -6,7 +8,36 @@ export interface GitHubRelease {
}
export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
const url: string = `https://github.com/goreleaser/goreleaser/releases/${version}`;
const resolvedVersion: string = (await resolveVersion(version)) || version;
const url: string = `https://github.com/goreleaser/goreleaser/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();
if (!allTags) {
throw new Error(`Cannot find GoReleaser tags`);
}
core.debug(`Found ${allTags.length} tags in total`);
return semver.maxSatisfying(allTags, version);
};
interface GitHubTag {
tag_name: string;
}
const getAllTags = async (): Promise<Array<string>> => {
const http: httpm.HttpClient = new httpm.HttpClient('goreleaser-action');
const url: string = `https://goreleaser.com/static/releases.json`;
const getTags = http.getJson<Array<GitHubTag>>(url);
return getTags.then(response => {
if (response.result == null) {
return [];
}
return response.result.map(obj => obj.tag_name);
});
};