Add helper function for checking the latest releases in the unit tests (#15).

This commit is contained in:
Robrecht Cannoodt 2023-06-28 11:03:05 +02:00
parent 7136a6f06e
commit e066eb124a
4 changed files with 51 additions and 8 deletions

View file

@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix release version check in unit test (#15). - Fix release version check in unit test (#15).
- Add helper function for checking the latest releases in the unit tests (#15).
## [1.3.0] - 2023-05-19 ## [1.3.0] - 2023-05-19
## Changed ## Changed

View file

@ -3,7 +3,7 @@ import { GitHub } from "@actions/github/lib/utils"
import anyTest, { TestFn } from "ava" // eslint-disable-line import/no-unresolved import anyTest, { TestFn } from "ava" // eslint-disable-line import/no-unresolved
import * as functions from "../src/functions" import * as functions from "../src/functions"
import { getToken } from "./utils" import { getReleaseTag, getToken } from "./utils"
const test = anyTest as TestFn<{ const test = anyTest as TestFn<{
token: string token: string
@ -29,7 +29,8 @@ test("lastest_stable_release_data", async t => {
t.context["octokit"] t.context["octokit"]
) )
t.is(typeof result, "object") t.is(typeof result, "object")
t.is(result["tag_name"], "v23.04.2") const expected = await getReleaseTag("nextflow-io/nextflow", false)
t.is(result["tag_name"], expected)
}) })
test.todo("nextflow_bin_url") test.todo("nextflow_bin_url")

View file

@ -3,7 +3,7 @@ import { GitHub } from "@actions/github/lib/utils"
import anyTest, { TestFn } from "ava" // eslint-disable-line import/no-unresolved import anyTest, { TestFn } from "ava" // eslint-disable-line import/no-unresolved
import { release_data } from "../src/functions" import { release_data } from "../src/functions"
import { getToken } from "./utils" import { getReleaseTag, getToken } from "./utils"
const test = anyTest as TestFn<{ const test = anyTest as TestFn<{
token: string token: string
@ -19,12 +19,22 @@ test.before(t => {
} }
}) })
const macro = test.macro(async (t, version: string, expected: string) => { const macro = test.macro(async (t, version: string) => {
let expected
if (version === "latest-stable") {
expected = await getReleaseTag("nextflow-io/nextflow", false)
} else if (version === "latest-edge") {
expected = await getReleaseTag("nextflow-io/nextflow", true)
} else if (version === "latest-everything") {
expected = await getReleaseTag("nextflow-io/nextflow", undefined)
} else {
expected = version
}
const result = await release_data(version, t.context["octokit"]) const result = await release_data(version, t.context["octokit"])
t.is(result["tag_name"], expected) t.is(result["tag_name"], expected)
}) })
test("hard version", macro, "v22.10.2", "v22.10.2") test("hard version", macro, "v22.10.2")
test("latest-stable", macro, "latest-stable", "v23.04.2") test("latest-stable", macro, "latest-stable")
test("latest-edge", macro, "latest-edge", "v23.06.0-edge") test("latest-edge", macro, "latest-edge")
test("latest-everything", macro, "latest-everything", "v23.06.0-edge") test("latest-everything", macro, "latest-everything")

View file

@ -1,3 +1,5 @@
import fetch from "node-fetch"
export function getToken(first: boolean): string { export function getToken(first: boolean): string {
const token = process.env["GITHUB_TOKEN"] || "" const token = process.env["GITHUB_TOKEN"] || ""
if (!token && first) { if (!token && first) {
@ -10,3 +12,31 @@ export function getToken(first: boolean): string {
return token return token
} }
/**
* Retrieves the release from a GitHub repository. This function allows to fetch
* either the latest release or the latest pre-release ("edge" release).
*
* @param {string} repo - The GitHub repository to fetch the release from,
* in the format 'owner/repo'.
* @param {boolean} [prerelease] - If true, fetches the latest pre-release.
* If false or undefined, fetches the latest release regardless of whether
* it's a pre-release or not.
*
* @returns {Promise<string>} A Promise that resolves to a string representing the tag name
* of the found release. If no release is found, the Promise resolves to 'Release not found'.
*/
export async function getReleaseTag(
repo: string,
prerelease?: boolean
): Promise<string> {
const response = await fetch(`https://api.github.com/repos/${repo}/releases`)
const releases = await response.json()
const release = releases.find(
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
(rel: any) => prerelease === undefined || rel.prerelease === prerelease
)
return release ? release.tag_name : "No release found"
}