From 4f4ced4e512e3b55c959c8eccd6987274193d078 Mon Sep 17 00:00:00 2001 From: hasundue Date: Fri, 2 Aug 2024 17:15:47 +0900 Subject: [PATCH] fix(integration): do not error on non-existing tag --- integration/commits.ts | 48 ++++++++++++++++++++++++------------- integration/commits_test.ts | 42 ++++++++++++++++++++++++++++++++ integration/github.ts | 10 ++++++++ integration/github_test.ts | 16 +++++++++++-- 4 files changed, 97 insertions(+), 19 deletions(-) diff --git a/integration/commits.ts b/integration/commits.ts index 265f3b32..9ef70f8d 100644 --- a/integration/commits.ts +++ b/integration/commits.ts @@ -1,45 +1,59 @@ import type { Repository } from "./repository.ts"; import * as github from "./github.ts"; +import { equals, parse } from "@std/semver"; /** * Get the commits between two Git references in the repository. + * If the tags for the specified versions are not found, an empty array is returned. + * + * @param repo The repository to fetch the commits from. + * @param from The version of the dependency to compare from. + * @param to The version of the dependency to compare to. + * + * @returns The commit messages between the two versions. */ export async function compareCommits( repo: Repository, - base: string, - head: string, + from: string, + to: string, ): Promise { // // Return the stored commits if available // using kv = await Deno.openKv(); - const stored = await kv.get([ - "commits", - repo.owner, - repo.name, - base, - head, - ]); + const stored = await kv.get( + ["commits", repo.owner, repo.name, from, to], + ); if (stored.value) { return stored.value; } // // Otherwise, fetch the commits from the Git hosting platform // - const commits = await _compareCommits(repo, base, head); + const tags = await _getTags(repo); + // Tags and versions not necessarily include the "v" prefix consistently + const base = tags.find((it) => equals(parse(it), parse(from))); + const head = tags.find((it) => equals(parse(it), parse(to))); + if (!base || !head) { + return []; + } + const commits = await _compareCommits(repo, from, to); await kv.set( - [ - "commits", - repo.owner, - repo.name, - base, - head, - ], + ["commits", repo.owner, repo.name, from, to], commits, ); return commits; } +async function _getTags(repo: Repository): Promise { + switch (repo.host) { + case "github": + return await github.getTags(repo); + default: + throw new Error(`Unsupported Git hosting platform: ${repo.host}`); + } +} + async function _compareCommits( repo: Repository, base: string, diff --git a/integration/commits_test.ts b/integration/commits_test.ts index e69de29b..31656cab 100644 --- a/integration/commits_test.ts +++ b/integration/commits_test.ts @@ -0,0 +1,42 @@ +import { assertEquals } from "@std/assert"; +import { compareCommits } from "./commits.ts"; + +Deno.test("compareCommits", async () => { + assertEquals( + await compareCommits( + { + host: "github", + owner: "hasundue", + name: "molt", + }, + "0.17.0", + "0.17.2", + ), + [ + "fix: accept a lock file without `dependencies`", + "test(cli): update snapshot", + "chore(task/update): enable `--unstable-lock`", + "chore(cli): `--version` returns `dev` if undefined", + "test(lib/file): check EOL at the end of file", + "fix: add EOL at the end of updated lock file", + "test(cli): stub latest version of `jsr:@std/`", + "test(cli): update snapshot", + "build(deps): bump deno.land/std from 0.219.1 to 0.220.1", + ], + ); +}); + +Deno.test("compareCommits - non existing tags", async () => { + assertEquals( + await compareCommits( + { + host: "github", + owner: "hasundue", + name: "molt", + }, + "0.0.1", + "0.0.2", + ), + [], + ); +}); diff --git a/integration/github.ts b/integration/github.ts index 1a6dae8b..8a531c9f 100644 --- a/integration/github.ts +++ b/integration/github.ts @@ -27,6 +27,16 @@ export async function compareCommits( return data.commits.map((it) => it.commit.message); } +export async function getTags( + repo: Repository<"github">, +): Promise { + const { data } = await octokit.repos.listTags({ + owner: repo.owner, + repo: repo.name, + }); + return data.map((it) => it.name); +} + type GitHubContent = { path?: string; type?: "blob" | "tree"; diff --git a/integration/github_test.ts b/integration/github_test.ts index 4a0177d3..460140cf 100644 --- a/integration/github_test.ts +++ b/integration/github_test.ts @@ -1,6 +1,6 @@ -import { assertEquals } from "@std/assert"; +import { assertArrayIncludes, assertEquals } from "@std/assert"; import "@std/dotenv/load"; -import { compareCommits, resolvePackageRoot } from "./github.ts"; +import { compareCommits, getTags, resolvePackageRoot } from "./github.ts"; import { parse } from "./packages.ts"; import type { Repository } from "./repository.ts"; @@ -26,6 +26,18 @@ Deno.test("compareCommits", async () => { ); }); +Deno.test("getTags", async () => { + const repo = { + host: "github", + owner: "hasundue", + name: "molt", + } satisfies Repository; + assertArrayIncludes(await getTags(repo), [ + "0.18.5", + "0.19.0", + ]); +}); + Deno.test("resolvePackageRoot", async () => { // // An example of a single package repository