Skip to content

Commit

Permalink
Filter Release Tags (#46)
Browse files Browse the repository at this point in the history
* filter out non semver compatible tags

* remove unused import

* code structure

* lint

* added invalid tag starting with v test
  • Loading branch information
afujiwara-roblox authored Nov 2, 2023
1 parent a27b8a4 commit fca1151
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 11 deletions.
51 changes: 49 additions & 2 deletions __tests__/configFile.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
Expand Down Expand Up @@ -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);
});
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
18 changes: 14 additions & 4 deletions src/foreman.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -26,6 +26,13 @@ async function getReleases(octokit: GitHub): Promise<GitHubRelease[]> {
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 +104,8 @@ export default {
chooseAsset,
authenticate,
addBinDirToPath,
installTools
installTools,
filterValidReleases
};

export type { GitHubRelease };
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ async function run(): Promise<void> {
"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}`
Expand Down

0 comments on commit fca1151

Please sign in to comment.