refactor: improve changelog logic (#197)

* refactor: improve changelog logic

Signed-off-by: Ardalan Amini <[email protected]>

* chore: update license comment formatting across all files

Replaced `/*` with `/**` for correct JSDoc-style license comments in all files.
This ensures consistency and compatibility with documentation tools that rely on JSDoc conventions.

Signed-off-by: Ardalan Amini <[email protected]>

* test: add unit tests for inputs

Signed-off-by: Ardalan Amini <[email protected]>

* test: add unit tests for utils

Signed-off-by: Ardalan Amini <[email protected]>

* test: add unit tests for outputs

Signed-off-by: Ardalan Amini <[email protected]>

* test: add unit tests for nodes

Signed-off-by: Ardalan Amini <[email protected]>

* test: add missing unit tests

Signed-off-by: Ardalan Amini <[email protected]>

* chore: address PR comments

Signed-off-by: Ardalan Amini <[email protected]>

* chore: address PR comments

Signed-off-by: Ardalan Amini <[email protected]>

---------

Signed-off-by: Ardalan Amini <[email protected]>
This commit is contained in:
Ardalan Amini authored and GitHub committed 2025-06-07 15:37:31 +03:30
1 parent 3c1177539c
commit 9bd6b8698c
95 files changed
+4381 -302

No files matched your search

+119
View File
@@ -0,0 +1,119 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getBooleanInput, getInput } from "@actions/core";
import { context } from "@actions/github";
import YAML from "yaml";
import { ChangelogNode, CommitNode, ScopeNode, TypeNode } from "../../src/nodes";
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
const url = `${ context.serverUrl }/${ repo.owner }/${ repo.repo }`;
beforeEach(() => {
jest.spyOn(context, "repo", "get").mockReturnValueOnce(repo);
});
it("should create an instance with common values", () => {
const shouldUseGithubAutolink = true;
const defaultCommitType = "Other Changes";
const commitTypes = "feat: Features\nbug: Bug Fixes";
jest.mocked(getBooleanInput).mockReturnValueOnce(shouldUseGithubAutolink);
jest.mocked(getInput).mockImplementation((input) => {
switch (input) {
case "default-commit-type":
return defaultCommitType;
case "commit-types":
return commitTypes;
default:
return "";
}
});
const changelogNode = (new ChangelogNode);
expect(changelogNode.shouldUseGithubAutolink).toBe(shouldUseGithubAutolink);
expect(changelogNode.defaultType).toBe(defaultCommitType);
expect(changelogNode.typeMap).toEqual(YAML.parse(commitTypes));
expect(changelogNode.repo).toEqual({
...repo,
url,
});
expect(changelogNode.print()).toBeNull();
});
it("should not print anything", () => {
jest.mocked(getBooleanInput).mockReturnValueOnce(true);
jest.mocked(getInput).mockImplementation((input) => {
switch (input) {
case "default-commit-type":
return "Other Changes";
case "commit-types":
return "feat: Features\nbug: Bug Fixes";
default:
return "";
}
});
const changelogNode = (new ChangelogNode);
const typeNode = changelogNode.addType("feat");
const scopeNode = typeNode.addScope();
expect(typeNode).toBeInstanceOf(TypeNode);
expect(scopeNode).toBeInstanceOf(ScopeNode);
expect(changelogNode.print()).toBeNull();
});
it("should print commit changelog", () => {
jest.mocked(getBooleanInput).mockReturnValueOnce(true);
jest.mocked(getInput).mockImplementation((input) => {
switch (input) {
case "default-commit-type":
return "Other Changes";
case "commit-types":
return "feat: Features\nbug: Bug Fixes";
default:
return "";
}
});
const changelogNode = (new ChangelogNode);
const commitNode1 = changelogNode.addType("feat").addScope()
.addCommit("implement awesome feature");
const commitNode2 = changelogNode.addType("bug").addScope()
.addCommit("fix some issue");
expect(commitNode1).toBeInstanceOf(CommitNode);
expect(commitNode2).toBeInstanceOf(CommitNode);
expect(changelogNode.print()).toBe("## Features\n* implement awesome feature\n\n## Bug Fixes\n* fix some issue");
});
+288
View File
@@ -0,0 +1,288 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getBooleanInput } from "@actions/core";
import { context } from "@actions/github";
import { CommitAuthorNode, CommitHashNode, Node, PullRequestNode } from "../../src/nodes";
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
const serverUrl = context.serverUrl;
const url = `${ serverUrl }/${ repo.owner }/${ repo.repo }`;
beforeEach(() => {
jest.spyOn(context, "repo", "get").mockReturnValueOnce(repo);
});
it("should print the username only to be autolinked", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const username = "ardalanamini";
const commitAuthorNode = new CommitAuthorNode(username);
expect(commitAuthorNode).toBeInstanceOf(Node);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(true);
expect(commitAuthorNode.repo).toEqual({
...repo,
url,
});
expect(commitAuthorNode.print()).toBe(`@${ username }`);
});
it("should not print anything", () => {
jest.mocked(getBooleanInput).mockImplementation(name => name !== "mention-authors");
const username = "ardalanamini";
const commitAuthorNode = new CommitAuthorNode(username);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(false);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(true);
expect(commitAuthorNode.print()).toBeNull();
});
it("should print the username with the link (not using autolink)", () => {
jest.mocked(getBooleanInput).mockImplementation(name => name !== "use-github-autolink");
const username = "ardalanamini";
const commitAuthorNode = new CommitAuthorNode(username);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(false);
expect(commitAuthorNode.print()).toBe(`[@${ username }](${ serverUrl }/${ username })`);
});
describe("should print the username & commit sha", () => {
it("with autolink", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const username = "ardalanamini";
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const commitAuthorNode = new CommitAuthorNode(username);
const reference = commitAuthorNode.addReference(sha);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(true);
expect(reference).toBeInstanceOf(CommitHashNode);
expect(commitAuthorNode.print()).toBe(`${ reference.print() } by @${ username }`);
});
it("without autolink", () => {
jest.mocked(getBooleanInput).mockImplementation(name => name !== "use-github-autolink");
const username = "ardalanamini";
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const commitAuthorNode = new CommitAuthorNode(username);
const reference = commitAuthorNode.addReference(sha);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(false);
expect(reference).toBeInstanceOf(CommitHashNode);
expect(commitAuthorNode.print()).toBe(`${ reference.print() } by [@${ username }](${ serverUrl }/${ username })`);
});
});
describe("should print the username & pr (prioritized over commit sha)", () => {
it("with autolink", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const username = "ardalanamini";
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const pr = "197";
const commitAuthorNode = new CommitAuthorNode(username);
const reference = commitAuthorNode.addReference(sha, pr);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(true);
expect(reference).toBeInstanceOf(PullRequestNode);
expect(commitAuthorNode.print()).toBe(`${ reference.print() } by @${ username }`);
});
it("without autolink", () => {
jest.mocked(getBooleanInput).mockImplementation(name => name !== "use-github-autolink");
const username = "ardalanamini";
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const pr = "197";
const commitAuthorNode = new CommitAuthorNode(username);
const reference = commitAuthorNode.addReference(sha, pr);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(false);
expect(reference).toBeInstanceOf(PullRequestNode);
expect(commitAuthorNode.print()).toBe(`${ reference.print() } by [@${ username }](${ serverUrl }/${ username })`);
});
});
describe("should print the username & multiple references", () => {
it("with autolink", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const username = "ardalanamini";
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const pr = "197";
const sha2 = "7a94b0db6150850cb1ae7243b13a4c972e9db261";
const commitAuthorNode = new CommitAuthorNode(username);
const reference = commitAuthorNode.addReference(sha, pr);
const reference2 = commitAuthorNode.addReference(sha2);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(true);
expect(reference).toBeInstanceOf(PullRequestNode);
expect(reference2).toBeInstanceOf(CommitHashNode);
expect(commitAuthorNode.print()).toBe(`${ reference.print() } & ${ reference2.print() } by @${ username }`);
});
it("without autolink", () => {
jest.mocked(getBooleanInput).mockImplementation(name => name !== "use-github-autolink");
const username = "ardalanamini";
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const pr = "197";
const sha2 = "7a94b0db6150850cb1ae7243b13a4c972e9db261";
const commitAuthorNode = new CommitAuthorNode(username);
const reference = commitAuthorNode.addReference(sha, pr);
const reference2 = commitAuthorNode.addReference(sha2);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(false);
expect(reference).toBeInstanceOf(PullRequestNode);
expect(reference2).toBeInstanceOf(CommitHashNode);
expect(commitAuthorNode.print()).toBe(`${ reference.print() } & ${ reference2.print() } by [@${ username }](${ serverUrl }/${ username })`);
});
});
describe("should reuse reference", () => {
it("commit sha", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const username = "ardalanamini";
const commitAuthorNode = new CommitAuthorNode(username);
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const reference1 = commitAuthorNode.addReference(sha);
const reference2 = commitAuthorNode.addReference(sha);
expect(commitAuthorNode).toBeInstanceOf(Node);
expect(reference1).toEqual(reference2);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(true);
expect(commitAuthorNode.repo).toEqual({
...repo,
url,
});
expect(commitAuthorNode.print()).toBe(`${ sha } by @${ username }`);
});
it("pr number", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const username = "ardalanamini";
const commitAuthorNode = new CommitAuthorNode(username);
const pr = "1";
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const reference1 = commitAuthorNode.addReference(sha, pr);
const reference2 = commitAuthorNode.addReference(sha, pr);
expect(commitAuthorNode).toBeInstanceOf(Node);
expect(reference1).toEqual(reference2);
expect(commitAuthorNode.username).toBe(username);
expect(commitAuthorNode.shouldMentionAuthor).toBe(true);
expect(commitAuthorNode.shouldUseGithubAutolink).toBe(true);
expect(commitAuthorNode.repo).toEqual({
...repo,
url,
});
expect(commitAuthorNode.print()).toBe(`#${ pr } by @${ username }`);
});
});
+88
View File
@@ -0,0 +1,88 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getBooleanInput } from "@actions/core";
import { context } from "@actions/github";
import { CommitHashNode, Node } from "../../src/nodes";
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
beforeEach(() => {
jest.spyOn(context, "repo", "get").mockReturnValueOnce(repo);
});
it("should print the sha only to be autolinked", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const commitHashNode = new CommitHashNode(sha);
expect(commitHashNode).toBeInstanceOf(Node);
expect(commitHashNode.sha).toBe(sha);
expect(commitHashNode.shouldNotPrint).toBe(false);
expect(commitHashNode.shouldUseGithubAutolink).toBe(true);
expect(commitHashNode.repo).toEqual({
...repo,
url: `${ context.serverUrl }/${ repo.owner }/${ repo.repo }`,
});
expect(commitHashNode.print()).toBe(sha);
});
it("should not print anything", () => {
jest.mocked(getBooleanInput).mockReturnValue(false);
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const commitHashNode = new CommitHashNode(sha);
expect(commitHashNode.sha).toBe(sha);
expect(commitHashNode.shouldNotPrint).toBe(true);
expect(commitHashNode.shouldUseGithubAutolink).toBe(false);
expect(commitHashNode.print()).toBeNull();
});
it("should print the sha with the link (not using autolink)", () => {
jest.mocked(getBooleanInput).mockImplementation(name => name !== "use-github-autolink");
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const commitHashNode = new CommitHashNode(sha);
expect(commitHashNode.sha).toBe(sha);
expect(commitHashNode.shouldNotPrint).toBe(false);
expect(commitHashNode.shouldUseGithubAutolink).toBe(false);
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
expect(commitHashNode.print()).toBe(`\`[${ sha.slice(0, 7) }](${ context.serverUrl }/${ repo.owner }/${ repo.repo }/commit/${ sha })\``);
});
+172
View File
@@ -0,0 +1,172 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getBooleanInput } from "@actions/core";
import { context } from "@actions/github";
import { CommitAuthorNode, CommitHashNode, CommitNode, Node } from "../../src/nodes";
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
const url = `${ context.serverUrl }/${ repo.owner }/${ repo.repo }`;
beforeEach(() => {
jest.spyOn(context, "repo", "get").mockReturnValueOnce(repo);
});
it("should print a commit description only", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const description = "ci: update test workflow";
const commitNode = new CommitNode(description);
expect(commitNode).toBeInstanceOf(Node);
expect(commitNode.description).toBe(description);
expect(commitNode.breaking).toBe(false);
expect(commitNode.shouldUseGithubAutolink).toBe(true);
expect(commitNode.repo).toEqual({
...repo,
url,
});
expect(commitNode.print()).toBe(`* ${ description }`);
});
it("should print a commit description with prefix", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const description = "ci: update test workflow";
const commitNode = new CommitNode(description);
expect(commitNode.description).toBe(description);
expect(commitNode.breaking).toBe(false);
expect(commitNode.shouldUseGithubAutolink).toBe(true);
const prefix = " ";
expect(commitNode.print(prefix)).toBe(`${ prefix }* ${ description }`);
});
it("should print a breaking commit description only", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const description = "ci: update test workflow";
const commitNode = new CommitNode(description, true);
expect(commitNode.description).toBe(description);
expect(commitNode.breaking).toBe(true);
expect(commitNode.shouldUseGithubAutolink).toBe(true);
expect(commitNode.print()).toBe(`* ***BREAKING:*** ${ description }`);
});
it("should print the commit description and its author", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const description = "ci: update test workflow";
const username = "ardalanamini";
const commitNode = new CommitNode(description);
const author = commitNode.addAuthor(username);
expect(commitNode.description).toBe(description);
expect(commitNode.breaking).toBe(false);
expect(commitNode.shouldUseGithubAutolink).toBe(true);
expect(author).toBeInstanceOf(CommitAuthorNode);
expect(commitNode.print()).toBe(`* ${ description } (${ author.print() })`);
});
it("should print the commit description and its multiple authors", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const description = "ci: update test workflow";
const username = "ardalanamini";
const username2 = "not-ardalanamini";
const commitNode = new CommitNode(description);
const author = commitNode.addAuthor(username);
const author2 = commitNode.addAuthor(username2);
expect(commitNode.description).toBe(description);
expect(commitNode.breaking).toBe(false);
expect(commitNode.shouldUseGithubAutolink).toBe(true);
expect(author).toBeInstanceOf(CommitAuthorNode);
expect(author2).toBeInstanceOf(CommitAuthorNode);
expect(commitNode.print()).toBe(`* ${ description } (${ author.print() }, ${ author2.print() })`);
});
it("it should reuse the same author node for same authors", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const description = "ci: update test workflow";
const username = "ardalanamini";
const sha = "3c1177539c1a216084f922ea52e56dd719a25945";
const sha2 = "7a94b0db6150850cb1ae7243b13a4c972e9db261";
const commitNode = new CommitNode(description);
const author = commitNode.addAuthor(username);
const reference = author.addReference(sha);
const author2 = commitNode.addAuthor(username);
const reference2 = author.addReference(sha2);
expect(commitNode.description).toBe(description);
expect(commitNode.breaking).toBe(false);
expect(commitNode.shouldUseGithubAutolink).toBe(true);
expect(author).toBeInstanceOf(CommitAuthorNode);
expect(reference).toBeInstanceOf(CommitHashNode);
expect(author2).toBeInstanceOf(CommitAuthorNode);
expect(reference2).toBeInstanceOf(CommitHashNode);
expect(author).toBe(author2);
const result = commitNode.print();
expect(result).toBe(`* ${ description } (${ author.print() })`);
expect(result).toBe(`* ${ description } (${ author2.print() })`);
});
+56
View File
@@ -0,0 +1,56 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getBooleanInput } from "@actions/core";
import { context } from "@actions/github";
import { Node } from "../../src/nodes";
it("should create an instance with common values", () => {
const shouldUseGithubAutolink = true;
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
jest.mocked(getBooleanInput).mockReturnValueOnce(shouldUseGithubAutolink);
jest.spyOn(context, "repo", "get").mockReturnValueOnce(repo);
class TestNode extends Node {
public print(): string {
return this.repo.repo;
}
}
const node = (new TestNode);
expect(node.shouldUseGithubAutolink).toBe(shouldUseGithubAutolink);
expect(node.serverUrl).toBe(context.serverUrl);
expect(node.repo).toEqual({
...repo,
url: `${ context.serverUrl }/${ repo.owner }/${ repo.repo }`,
});
});
+88
View File
@@ -0,0 +1,88 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getBooleanInput } from "@actions/core";
import { context } from "@actions/github";
import { Node, PullRequestNode } from "../../src/nodes";
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
beforeEach(() => {
jest.spyOn(context, "repo", "get").mockReturnValueOnce(repo);
});
it("should print the pr only to be autolinked", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const pr = "197";
const pullRequestNode = new PullRequestNode(pr);
expect(pullRequestNode).toBeInstanceOf(Node);
expect(pullRequestNode.pr).toBe(pr);
expect(pullRequestNode.shouldNotPrint).toBe(false);
expect(pullRequestNode.shouldUseGithubAutolink).toBe(true);
expect(pullRequestNode.repo).toEqual({
...repo,
url: `${ context.serverUrl }/${ repo.owner }/${ repo.repo }`,
});
expect(pullRequestNode.print()).toBe(`#${ pr }`);
});
it("should not print anything", () => {
jest.mocked(getBooleanInput).mockReturnValue(false);
const pr = "197";
const pullRequestNode = new PullRequestNode(pr);
expect(pullRequestNode.pr).toBe(pr);
expect(pullRequestNode.shouldNotPrint).toBe(true);
expect(pullRequestNode.shouldUseGithubAutolink).toBe(false);
expect(pullRequestNode.print()).toBeNull();
});
it("should print the pr with the link (not using autolink)", () => {
// Enable PR links but disable GitHub autolink to test manual link generation
jest.mocked(getBooleanInput).mockImplementation(name => name !== "use-github-autolink");
const pr = "197";
const pullRequestNode = new PullRequestNode(pr);
expect(pullRequestNode.pr).toBe(pr);
expect(pullRequestNode.shouldNotPrint).toBe(false);
expect(pullRequestNode.shouldUseGithubAutolink).toBe(false);
expect(pullRequestNode.print()).toBe(`[#${ pr }](${ context.serverUrl }/${ repo.owner }/${ repo.repo }/issues/${ pr })`);
});
+188
View File
@@ -0,0 +1,188 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getBooleanInput } from "@actions/core";
import { context } from "@actions/github";
import { CommitNode, Node, ScopeNode } from "../../src/nodes";
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
const url = `${ context.serverUrl }/${ repo.owner }/${ repo.repo }`;
beforeEach(() => {
jest.spyOn(context, "repo", "get").mockReturnValueOnce(repo);
});
it("should not print anything", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const scope = "commit-scope";
const scopeNode = new ScopeNode(scope);
expect(scopeNode).toBeInstanceOf(Node);
expect(scopeNode.scope).toBe(scope);
expect(scopeNode.shouldUseGithubAutolink).toBe(true);
expect(scopeNode.repo).toEqual({
...repo,
url,
});
expect(scopeNode.print()).toBeNull();
});
it("should print a scope & its commit", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const scope = "commit-scope";
const description = "ci: update test workflow";
const scopeNode = new ScopeNode(scope);
const commitNode = scopeNode.addCommit(description);
expect(scopeNode.scope).toBe(scope);
expect(scopeNode.shouldUseGithubAutolink).toBe(true);
expect(commitNode).toBeInstanceOf(CommitNode);
expect(scopeNode.print()).toBe(`* **${ scope }:**\n${ commitNode.print(" ") }`);
});
it("should reuse the same commit message (in the case the breaking matches as well)", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const scope = "commit-scope";
const description = "ci: update test workflow";
const scopeNode = new ScopeNode(scope);
const commitNode = scopeNode.addCommit(description);
const commitNode2 = scopeNode.addCommit(description);
expect(scopeNode.scope).toBe(scope);
expect(scopeNode.shouldUseGithubAutolink).toBe(true);
expect(commitNode).toBeInstanceOf(CommitNode);
expect(commitNode2).toBeInstanceOf(CommitNode);
expect(commitNode).toBe(commitNode2);
const result = scopeNode.print();
expect(result).toBe(`* **${ scope }:**\n${ commitNode.print(" ") }`);
expect(result).toBe(`* **${ scope }:**\n${ commitNode2.print(" ") }`);
});
it("should print a scope & its commit with prefix", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const scope = "commit-scope";
const description = "ci: update test workflow";
const scopeNode = new ScopeNode(scope);
const commitNode = scopeNode.addCommit(description);
expect(scopeNode.scope).toBe(scope);
expect(scopeNode.shouldUseGithubAutolink).toBe(true);
expect(commitNode).toBeInstanceOf(CommitNode);
const prefix = " ";
expect(scopeNode.print(prefix)).toBe(`${ prefix }* **${ scope }:**\n${ commitNode.print(`${ prefix } `) }`);
});
it("should print a scope & its multiple commits", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const scope = "commit-scope";
const description = "ci: update test workflow";
const description2 = "chore: update copyright year";
const scopeNode = new ScopeNode(scope);
const commitNode = scopeNode.addCommit(description);
const commitNode2 = scopeNode.addCommit(description2);
expect(scopeNode.scope).toBe(scope);
expect(scopeNode.shouldUseGithubAutolink).toBe(true);
expect(commitNode).toBeInstanceOf(CommitNode);
expect(commitNode2).toBeInstanceOf(CommitNode);
expect(scopeNode.print()).toBe(`* **${ scope }:**\n${ commitNode.print(" ") }\n${ commitNode2.print(" ") }`);
});
it("should print a commit without scope", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const description = "ci: update test workflow";
const scopeNode = (new ScopeNode);
const commitNode = scopeNode.addCommit(description);
expect(scopeNode.scope).toBe("");
expect(scopeNode.shouldUseGithubAutolink).toBe(true);
expect(commitNode).toBeInstanceOf(CommitNode);
expect(scopeNode.print()).toBe(`${ commitNode.print() }`);
});
it("should print multiple commits without scope", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const description = "ci: update test workflow";
const description2 = "chore: update copyright year";
const scopeNode = (new ScopeNode);
const commitNode = scopeNode.addCommit(description);
const commitNode2 = scopeNode.addCommit(description2);
expect(scopeNode.scope).toBe("");
expect(scopeNode.shouldUseGithubAutolink).toBe(true);
expect(commitNode).toBeInstanceOf(CommitNode);
expect(commitNode2).toBeInstanceOf(CommitNode);
expect(scopeNode.print()).toBe(`${ commitNode.print() }\n${ commitNode2.print() }`);
});
+198
View File
@@ -0,0 +1,198 @@
/**
* MIT License
*
* Copyright (c) 2025 Ardalan Amini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { getBooleanInput } from "@actions/core";
import { context } from "@actions/github";
import { CommitNode, Node, ScopeNode, TypeNode } from "../../src/nodes";
const repo = {
owner: "ardalanamini",
repo : "auto-changelog",
};
const url = `${ context.serverUrl }/${ repo.owner }/${ repo.repo }`;
beforeEach(() => {
jest.spyOn(context, "repo", "get").mockReturnValueOnce(repo);
});
it("should not print anything when no scopes/commits are added", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const commitType = "commit-type";
const typeNode = new TypeNode(commitType);
expect(typeNode).toBeInstanceOf(Node);
expect(typeNode.type).toBe(commitType);
expect(typeNode.shouldUseGithubAutolink).toBe(true);
expect(typeNode.repo).toEqual({
...repo,
url,
});
expect(typeNode.print()).toBeNull();
});
it("should not print anything when no commits are added", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const commitType = "commit-type";
const scope = "commit-scope";
const typeNode = new TypeNode(commitType);
const scopeNode = typeNode.addScope(scope);
expect(typeNode.type).toBe(commitType);
expect(typeNode.shouldUseGithubAutolink).toBe(true);
expect(scopeNode).toBeInstanceOf(ScopeNode);
expect(typeNode.print()).toBeNull();
});
it("should print a type & its scope/commit", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const commitType = "commit-type";
const scope = "commit-scope";
const description = "ci: update test workflow";
const typeNode = new TypeNode(commitType);
const scopeNode = typeNode.addScope(scope);
const commitNode = scopeNode.addCommit(description);
expect(typeNode.type).toBe(commitType);
expect(typeNode.shouldUseGithubAutolink).toBe(true);
expect(scopeNode).toBeInstanceOf(ScopeNode);
expect(commitNode).toBeInstanceOf(CommitNode);
expect(typeNode.print()).toBe(`## ${ commitType }\n${ scopeNode.print() }`);
});
it("should reuse the same scope (in the case the breaking matches as well)", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const commitType = "commit-type";
const scope = "commit-scope";
const description = "ci: update test workflow";
const description2 = "chore: update copyright year";
const typeNode = new TypeNode(commitType);
const scopeNode = typeNode.addScope(scope);
const scopeNode2 = typeNode.addScope(scope);
const commitNode = scopeNode.addCommit(description);
const commitNode2 = scopeNode2.addCommit(description2);
expect(typeNode.type).toBe(commitType);
expect(typeNode.shouldUseGithubAutolink).toBe(true);
expect(scopeNode).toBeInstanceOf(ScopeNode);
expect(scopeNode2).toBeInstanceOf(ScopeNode);
expect(scopeNode).toBe(scopeNode2);
expect(commitNode).toBeInstanceOf(CommitNode);
expect(commitNode2).toBeInstanceOf(CommitNode);
const result = typeNode.print();
expect(result).toBe(`## ${ commitType }\n${ scopeNode.print() }`);
expect(result).toBe(`## ${ commitType }\n${ scopeNode2.print() }`);
});
it("should print a type & its scope/commit with prefix", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const commitType = "commit-type";
const scope = "commit-scope";
const description = "ci: update test workflow";
const typeNode = new TypeNode(commitType);
const scopeNode = typeNode.addScope(scope);
const commitNode = scopeNode.addCommit(description);
expect(typeNode.type).toBe(commitType);
expect(typeNode.shouldUseGithubAutolink).toBe(true);
expect(scopeNode).toBeInstanceOf(ScopeNode);
expect(commitNode).toBeInstanceOf(CommitNode);
const prefix = " ";
expect(typeNode.print(prefix)).toBe(`${ prefix }## ${ commitType }\n${ scopeNode.print(prefix) }`);
});
it("should print a type & its multiple scope/commits", () => {
jest.mocked(getBooleanInput).mockReturnValue(true);
const commitType = "commit-type";
const scope = "commit-scope";
const description = "ci: update test workflow";
const description2 = "chore: update copyright year";
const typeNode = new TypeNode(commitType);
const scopeNode = typeNode.addScope(scope);
const commitNode = scopeNode.addCommit(description);
const scopeNode2 = typeNode.addScope();
const commitNode2 = scopeNode2.addCommit(description2);
expect(typeNode.type).toBe(commitType);
expect(typeNode.shouldUseGithubAutolink).toBe(true);
expect(scopeNode).toBeInstanceOf(ScopeNode);
expect(scopeNode2).toBeInstanceOf(ScopeNode);
expect(commitNode).toBeInstanceOf(CommitNode);
expect(commitNode2).toBeInstanceOf(CommitNode);
expect(typeNode.print()).toBe(`## ${ commitType }\n${ scopeNode2.print() }\n${ scopeNode.print() }`);
});