Skip to content
Open
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
77 changes: 39 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"@actions/core": "^1.11.1",
"@actions/github": "^6.0.0",
"@octokit/core": "^5.2.1",
"date-fns": "^4.1.0"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { exec } from "child_process";

describe("CLI", () => {
test("should run without errors", (done) => {
exec("ts-node src/cli.ts", (error, stdout, stderr) => {
expect(error).toBeNull();
expect(stderr).toBe("");
done();
});
});

// Additional tests can be added to verify environment variable handling and CLI options
});
104 changes: 104 additions & 0 deletions src/commitComments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { TaggedCommitComments } from "./commitComments";
import { Octokit } from "@octokit/core";

jest.mock("@octokit/core");

describe("TaggedCommitComments", () => {
let octokit: Octokit;
let taggedCommitComments: TaggedCommitComments;

beforeEach(() => {
octokit = new Octokit();
taggedCommitComments = new TaggedCommitComments(
{ owner: "github", repo: "octocat" },
octokit,
{}
);
});

test("formatCommentMessage replaces tokens correctly", () => {
const message = TaggedCommitComments.formatCommentMessage(
"Hello {author}, branch {branchName} in repo {repoName}",
{
branchName: "feature-branch",
prefix: "refs/heads/",
commitId: "abc123",
author: { username: "user1", email: "[email protected]", belongsToOrganization: false },
date: Date.now(),
isProtected: false,
openPrs: false,
},
{ daysBeforeBranchStale: 10, daysBeforeBranchDelete: 5 },
{ owner: "github", repo: "octocat" },
"user1"
);
expect(message).toContain("Hello user1");
expect(message).toContain("branch feature-branch");
expect(message).toContain("repo octocat");
});

test("addCommitComments calls octokit request with correct parameters", async () => {
const mockRequest = jest.fn().mockResolvedValue({});
(octokit.request as jest.Mock) = mockRequest;

await taggedCommitComments.addCommitComments({
commentTag: "stale:feature-branch",
commentBody: "Test comment",
commitSHA: "abc123",
});

expect(mockRequest).toHaveBeenCalledWith(
"POST /repos/{owner}/{repo}/commits/{commit_sha}/comments",
expect.objectContaining({
body: "[stale:feature-branch]\r\n\r\nTest comment",
commit_sha: "abc123",
})
);
});

test("getCommitCommentsWithTag filters comments correctly", async () => {
const mockComments = [
{ body: "[stale:feature-branch] Comment 1", id: 1 },
{ body: "Other comment", id: 2 },
];
const mockRequest = jest.fn().mockResolvedValue({ data: mockComments });
(octokit.request as jest.Mock) = mockRequest;

const comments = await taggedCommitComments.getCommitCommentsWithTag({
commentTag: "stale:feature-branch",
commitSHA: "abc123",
});

expect(comments).toHaveLength(1);
expect(comments[0].id).toBe(1);
});

test("deleteCommitComments calls octokit request with correct parameters", async () => {
const mockRequest = jest.fn().mockResolvedValue({});
(octokit.request as jest.Mock) = mockRequest;

await taggedCommitComments.deleteCommitComments({ commentId: 1 });

expect(mockRequest).toHaveBeenCalledWith(
"DELETE /repos/{owner}/{repo}/comments/{comment_id}",
expect.objectContaining({ comment_id: 1 })
);
});

test("deleteBranch calls octokit request with correct parameters", async () => {
const mockRequest = jest.fn().mockResolvedValue({});
(octokit.request as jest.Mock) = mockRequest;

await taggedCommitComments.deleteBranch({
branchName: "feature-branch",
prefix: "refs/heads/",
} as any);

expect(mockRequest).toHaveBeenCalledWith(
"DELETE /repos/{owner}/{repo}/git/refs/{ref}",
expect.objectContaining({ ref: "heads/feature-branch" })
);
});

// Additional tests for error handling can be added here
});
Loading