Skip to content

Commit

Permalink
filter out non semver compatible tags
Browse files Browse the repository at this point in the history
  • Loading branch information
afujiwara-roblox committed Nov 2, 2023
1 parent a27b8a4 commit 990de42
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 13 deletions.
48 changes: 46 additions & 2 deletions __tests__/configFile.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import configFile from "../src/configFile";
import {parse} from "toml";

import foreman from "../src/foreman";
import type { GitHubRelease } from "../src/foreman";
import { parse } from "toml";
import semver from "semver";
test("get off my back, Jest", () => {
expect(5).toEqual(5);
});
Expand Down Expand Up @@ -30,3 +32,45 @@ 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: []
}
];

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);
});
8 changes: 5 additions & 3 deletions src/configFile.ts
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -58,7 +58,9 @@ async function checkSameOrgInConfig(org: string): Promise<void> {

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);
Expand Down
15 changes: 13 additions & 2 deletions src/foreman.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,20 @@ async function getReleases(octokit: GitHub): Promise<GitHubRelease[]> {
repo: "foreman"
});

const releases = response.data as GitHubRelease[];
let releases = response.data as GitHubRelease[];
releases = filterValidReleases(releases);
releases.sort((a, b) => -semver.compare(a.tag_name, b.tag_name));

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[]
Expand Down Expand Up @@ -97,5 +105,8 @@ export default {
chooseAsset,
authenticate,
addBinDirToPath,
installTools
installTools,
filterValidReleases
};

export type {GitHubRelease};
11 changes: 5 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { getInput, debug, addPath, setFailed } from "@actions/core";
import { downloadTool, extractZip } from "@actions/tool-cache";
import { GitHub } from "@actions/github";
import { resolve } from "path";
import { exec } from "@actions/exec";
import {getInput, debug, addPath, setFailed} from "@actions/core";
import {downloadTool, extractZip} from "@actions/tool-cache";
import {GitHub} from "@actions/github";
import {resolve} from "path";
import {exec} from "@actions/exec";
import configFile from "./configFile";
import foreman from "./foreman";

Expand All @@ -15,7 +15,6 @@ async function run(): Promise<void> {
"allow-external-github-orgs"
).toLowerCase();


const octokit = new GitHub(githubToken);
const releases = await foreman.getReleases(octokit);
debug("Choosing release from GitHub API");
Expand Down

0 comments on commit 990de42

Please sign in to comment.