diff --git a/__tests__/configFile.test.ts b/__tests__/configFile.test.ts index 3b772eb..52c6210 100644 --- a/__tests__/configFile.test.ts +++ b/__tests__/configFile.test.ts @@ -1,6 +1,7 @@ import configFile from "../src/configFile"; -import {parse} from "toml"; - +import foreman from "../src/foreman"; +import type { GitHubRelease } from "../src/foreman"; +import { parse } from "toml"; test("get off my back, Jest", () => { expect(5).toEqual(5); }); @@ -30,3 +31,49 @@ test("checkSameOrgToolSpec different org", () => { false ); }); + +test("filter valid releases", () => { + const releases: GitHubRelease[] = [ + { + tag_name: "v1.0.0", + assets: [] + }, + { + tag_name: "v2.1.0", + assets: [] + }, + { + tag_name: "v3.0.0-rc.1", + assets: [] + }, + { + tag_name: "notvalidsemver", + assets: [] + }, + { + tag_name: "4.3.0", + assets: [] + }, + { + tag_name: "verybadtag", + assets: [] + } + ]; + + const expectedFilteredReleases: GitHubRelease[] = [ + { + tag_name: "v1.0.0", + assets: [] + }, + { + tag_name: "v2.1.0", + assets: [] + }, + { + tag_name: "v3.0.0-rc.1", + assets: [] + } + ]; + const filteredReleases = foreman.filterValidReleases(releases); + expect(filteredReleases).toEqual(expectedFilteredReleases); +}); diff --git a/src/configFile.ts b/src/configFile.ts index 6b7b13d..3da8c6f 100644 --- a/src/configFile.ts +++ b/src/configFile.ts @@ -1,5 +1,5 @@ -import { parse } from "toml"; -import { readFile } from "fs"; +import {parse} from "toml"; +import {readFile} from "fs"; import findUp from "find-up"; interface foremanConfig { tools: { @@ -58,7 +58,9 @@ async function checkSameOrgInConfig(org: string): Promise { await readFile(manifestPath, "utf8", (err, data) => { if (err) { - throw new Error(`setup-foreman Could not read Foreman config file. err: ${err}`); + throw new Error( + `setup-foreman Could not read Foreman config file. err: ${err}` + ); } const manifestContent = parse(data); const sameGithubOrgSource = checkSameOrgToolSpecs(manifestContent, org); diff --git a/src/foreman.ts b/src/foreman.ts index 718a5c9..b1604d5 100644 --- a/src/foreman.ts +++ b/src/foreman.ts @@ -1,6 +1,6 @@ -import {addPath} from "@actions/core"; -import {exec} from "@actions/exec"; -import {GitHub} from "@actions/github"; +import { addPath } from "@actions/core"; +import { exec } from "@actions/exec"; +import { GitHub } from "@actions/github"; import semver from "semver"; import os from "os"; @@ -26,6 +26,13 @@ async function getReleases(octokit: GitHub): Promise { return releases; } +function filterValidReleases(releases: GitHubRelease[]): GitHubRelease[] { + return releases.filter(release => { + const tag = release.tag_name; + return tag.startsWith("v") && semver.valid(tag); + }); +} + function chooseRelease( versionReq: string, releases: GitHubRelease[] @@ -97,5 +104,8 @@ export default { chooseAsset, authenticate, addBinDirToPath, - installTools + installTools, + filterValidReleases }; + +export type { GitHubRelease }; diff --git a/src/main.ts b/src/main.ts index 453abc9..d4de7a7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,12 +15,12 @@ async function run(): Promise { "allow-external-github-orgs" ).toLowerCase(); - const octokit = new GitHub(githubToken); const releases = await foreman.getReleases(octokit); + const validReleases = foreman.filterValidReleases(releases) debug("Choosing release from GitHub API"); - const release = foreman.chooseRelease(versionReq, releases); + const release = foreman.chooseRelease(versionReq, validReleases); if (release == null) { throw new Error( `Could not find Foreman release for version ${versionReq}`