Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter Release Tags #46

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: []
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for good measure, can we add one that starts with v but isn't valid? 😀

];

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);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume that semver.valid must handle the leading v just fine, since your tests appear to pass

}

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
Loading