Skip to content
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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ jobs:
- name: Build
run: pnpm build

- name: Unit tests

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We could push this to another job so it would run on forks. at the moment, we rely on merge queue rejecting broken integration tests because those require secrets.

It would complicate the setup though so perhaps it's not worth it

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah that makes sense. I'd like to push more tests as unit tests so forks can run it. I think I can follow up later

run: pnpm test:unit

- name: Integration tests
run: pnpm test:integration
env:
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"format": "oxfmt --check",
"format:fix": "oxfmt",
"lint": "oxlint",
"test:unit": "vitest",
"test:integration": "vitest -c vitest.integration.config.ts",
"version-packages": "changeset version && pnpm format:fix",
"release": "pnpm build && changeset publish"
Expand Down
12 changes: 2 additions & 10 deletions src/core.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { GetRepositoryMetadataQuery } from "./github/graphql/generated/operations.js";
import type { CommitMessage } from "./github/graphql/generated/types.ts";
import {
createCommitOnBranchQuery,
getRepositoryMetadata,
Expand All @@ -9,6 +8,7 @@ import type {
CommitFilesResult,
GitBase,
} from "./interface.ts";
import { normalizeCommitMessage } from "./utils.ts";

const getBaseRef = (base: GitBase): string => {
if ("branch" in base) {
Expand Down Expand Up @@ -62,22 +62,14 @@ const createCommit = async ({
refId: string;
baseOid: string;
}) => {
const normalizedMessage: CommitMessage =
typeof message === "string"
? {
headline: message.split("\n")[0]?.trim() ?? "",
body: message.split("\n").slice(1).join("\n").trim(),
}
: message;

// we have to stick to GraphQL here as with REST, each file change would become a separate API call
return createCommitOnBranchQuery(octokit, {
input: {
branch: {
id: refId,
},
expectedHeadOid: baseOid,
message: normalizedMessage,
message: normalizeCommitMessage(message),
fileChanges,
},
});
Expand Down
22 changes: 22 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { CommitMessage } from "./github/graphql/generated/types.ts";

export function normalizeCommitMessage(
message: string | CommitMessage,
): CommitMessage {
if (typeof message === "object") {
return {
headline: message.headline.trim(),
body: message.body?.trim(),
};
}

if (!message.includes("\n")) {
return { headline: message.trim() };
}

const [headline, ...bodyLines] = message.split("\n");
return {
headline: headline.trim(),
body: bodyLines.join("\n").trim(),
};
}
92 changes: 0 additions & 92 deletions tests/integration/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { promises as fs } from "fs";
import { getOctokit } from "@actions/github";
import git from "isomorphic-git";
import { afterAll, beforeAll, describe, expect, it, vi } from "vitest";
import type { CommitMessage } from "../../src/github/graphql/generated/types.ts";
import {
createRefMutation,
getRefTreeQuery,
Expand Down Expand Up @@ -497,97 +496,6 @@ describe("node", () => {
});
});
});

describe("commit message is correctly handled", () => {
const testCommitMessage = async ({
branch,
input,
expected,
}: {
branch: string;
input: string | CommitMessage;
expected: string;
}) => {
await commitFilesFromBuffers({
octokit,
...REPO,
branch,
base: {
commit: testTargetCommit,
},
...BASIC_FILE_CONTENTS,
message: input,
});

await waitForGitHubToBeReady();

const ref = (
await getRefTreeQuery(octokit, {
...REPO,
ref: `refs/heads/${branch}`,
path: "package.json",
})
).repository?.ref?.target;

if (!ref || !("message" in ref)) {
throw new Error("Unexpected result");
}

expect(ref.message).toEqual(expected);
};

it("single string", async () => {
const branch = `${TEST_BRANCH_PREFIX}-commit-message-single`;
branches.push(branch);

await testCommitMessage({
branch,
input: "This is a basic commit message",
expected: "This is a basic commit message",
});
});

it("multi-line string", async () => {
const branch = `${TEST_BRANCH_PREFIX}-commit-message-multi`;
branches.push(branch);

await testCommitMessage({
branch,
input:
"This is a basic commit message\nwith a second line\n\nand some more lines!",
expected:
"This is a basic commit message\n\nwith a second line\n\nand some more lines!",
});
});

it("headline only", async () => {
const branch = `${TEST_BRANCH_PREFIX}-commit-message-h-only`;
branches.push(branch);

await testCommitMessage({
branch,
input: {
headline: "This is a basic commit message!!",
},
expected: "This is a basic commit message!!",
});
});

it("headline & body", async () => {
const branch = `${TEST_BRANCH_PREFIX}-commit-message-h-and-b`;
branches.push(branch);

await testCommitMessage({
branch,
input: {
headline: "This is a basic commit message!!",
body: "This is the body of the commit message\nit has more text",
},
expected:
"This is a basic commit message!!\n\nThis is the body of the commit message\nit has more text",
});
});
});
});

afterAll(async () => {
Expand Down
41 changes: 41 additions & 0 deletions tests/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe, expect, it } from "vitest";
import { normalizeCommitMessage } from "../src/utils.ts";

describe("normalizeCommitMessage", () => {
it("handles single line messages", () => {
const message = "This is a commit message";
const result = normalizeCommitMessage(message);
expect(result).toEqual({ headline: "This is a commit message" });
});

it("handles multi line messages", () => {
const message =
"This is a commit message\nwith a second line\nand a third line";
const result = normalizeCommitMessage(message);
expect(result).toEqual({
headline: "This is a commit message",
body: "with a second line\nand a third line",
});
});

it("trims whitespace from headline and body", () => {
const message = " This is a commit message \n with a second line ";
const result = normalizeCommitMessage(message);
expect(result).toEqual({
headline: "This is a commit message",
body: "with a second line",
});
});

it("handles object messages", () => {
const message = {
headline: " This is a commit message ",
body: " with a second line ",
};
const result = normalizeCommitMessage(message);
expect(result).toEqual({
headline: "This is a commit message",
body: "with a second line",
});
});
});
9 changes: 9 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
test: {
experimental: { preParse: true },
clearMocks: true,
include: ["tests/*.test.ts"],
},
});
3 changes: 2 additions & 1 deletion vitest.integration.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ process.env.ROOT_TEMP_DIRECTORY ??= path.join(

export default defineConfig({
test: {
Comment thread
bluwy marked this conversation as resolved.
environment: "node",
experimental: { preParse: true },
clearMocks: true,
globalSetup: ["./tests/integration/globalSetup.ts"],
include: ["tests/integration/**/*.test.ts"],
},
Expand Down