Skip to content

Commit

Permalink
fix(integration): do not error on non-existing tag
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Aug 2, 2024
1 parent b99557d commit 4f4ced4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 19 deletions.
48 changes: 31 additions & 17 deletions integration/commits.ts
Original file line number Diff line number Diff line change
@@ -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<string[]> {
//
// Return the stored commits if available
//
using kv = await Deno.openKv();
const stored = await kv.get<string[]>([
"commits",
repo.owner,
repo.name,
base,
head,
]);
const stored = await kv.get<string[]>(
["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<string[]> {
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,
Expand Down
42 changes: 42 additions & 0 deletions integration/commits_test.ts
Original file line number Diff line number Diff line change
@@ -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",
),
[],
);
});
10 changes: 10 additions & 0 deletions integration/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export async function compareCommits(
return data.commits.map((it) => it.commit.message);
}

export async function getTags(
repo: Repository<"github">,
): Promise<string[]> {
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";
Expand Down
16 changes: 14 additions & 2 deletions integration/github_test.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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
Expand Down

0 comments on commit 4f4ced4

Please sign in to comment.