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
6 changes: 6 additions & 0 deletions validate-pull-request-title/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ A comma-separated list of prefixes that are allowed in pull request titles.

A boolean indicating whether the pull request title is valid.

### `error-message`

A detailed message indicating why the pull request title is invalid. This may
include information such as exceeding maximum length, not meeting minimum
length, or lacking required prefixes.

## License

This action is licensed under the [MIT License](LICENSE).
Expand Down
21 changes: 19 additions & 2 deletions validate-pull-request-title/__tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ describe("run function", () => {

it("should set is-valid to false if validateTitleLength fails", async () => {
// arrange
const errorMessage = "Title length not valid";
const mockInputs = {
pullRequestTitle: "Test PR",
caseSensitivePrefix: true,
maxLengthTitle: 100,
minLengthTitle: 10,
allowedPrefixes: ["Test"]
};
const mockLengthValidation = { isValid: false, message: undefined }; // Invalid result
const mockLengthValidation = {
isValid: false,
message: errorMessage
}; // Invalid result
const validateInputsSpy = jest
.spyOn(Validators, "validateInputs")
.mockResolvedValue(mockInputs);
Expand All @@ -56,10 +60,15 @@ describe("run function", () => {
mockInputs.maxLengthTitle
);
expect(coreSetOutputSpy).toHaveBeenCalledWith("is-valid", false);
expect(coreSetOutputSpy).toHaveBeenCalledWith(
"error-message",
errorMessage
);
});

it("should set is-valid to false if validateTitlePrefixes fails", async () => {
// arrange
const errorMessage = "Title prefix not valid";
const mockInputs = {
pullRequestTitle: "Test PR",
caseSensitivePrefix: true,
Expand All @@ -70,7 +79,7 @@ describe("run function", () => {
const mockLengthValidation = { isValid: true }; // Valid result
const mockPrefixesValidation = {
isValid: false,
message: undefined
message: errorMessage
}; // Invalid result
const validateInputsSpy = jest
.spyOn(Validators, "validateInputs")
Expand Down Expand Up @@ -99,6 +108,10 @@ describe("run function", () => {
mockInputs.caseSensitivePrefix
);
expect(coreSetOutputSpy).toHaveBeenCalledWith("is-valid", false);
expect(coreSetOutputSpy).toHaveBeenCalledWith(
"error-message",
errorMessage
);
});

it("should call core.info if all validations pass", async () => {
Expand All @@ -122,6 +135,7 @@ describe("run function", () => {
.spyOn(Validators, "validateTitlePrefixes")
.mockReturnValue(mockPrefixesValidation);
const coreInfoSpy = jest.spyOn(core, "info");
const coreSetOutputSpy = jest.spyOn(core, "setOutput");

// act
await run();
Expand All @@ -133,6 +147,7 @@ describe("run function", () => {
expect(coreInfoSpy).toHaveBeenCalledWith(
"Pull Request title validation passed."
);
expect(coreSetOutputSpy).toHaveBeenCalledWith("is-valid", true);
});

it("should throw an error with specific message if error is not an instance of Error", async () => {
Expand All @@ -142,6 +157,7 @@ describe("run function", () => {
.spyOn(Validators, "validateInputs")
.mockRejectedValue(error);
const coreSetFailedSpy = jest.spyOn(core, "setFailed");
const coreSetOutputSpy = jest.spyOn(core, "setOutput");

// act
await run();
Expand All @@ -151,5 +167,6 @@ describe("run function", () => {
expect(coreSetFailedSpy).toHaveBeenCalledWith(
"Failed to validate pull request title: Unknown error"
);
expect(coreSetOutputSpy).not.toHaveBeenCalled();
});
});
4 changes: 4 additions & 0 deletions validate-pull-request-title/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ inputs:
outputs:
is-valid:
description: Indicates if the title is valid or not
error-message:
ddescription: |
A detailed message indicating why the pull request title is invalid.
This may include information such as exceeding maximum length, not meeting minimum length, or lacking required prefixes.

runs:
using: node20
Expand Down
2 changes: 2 additions & 0 deletions validate-pull-request-title/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24954,11 +24954,13 @@ async function run() {
const lengthValidation = (0, validators_1.validateTitleLength)(pullRequestTitle, minLengthTitle, maxLengthTitle);
if (!lengthValidation.isValid) {
core.setOutput("is-valid", false);
core.setOutput("error-message", lengthValidation.message);
return;
}
const prefixesValidation = (0, validators_1.validateTitlePrefixes)(pullRequestTitle, allowedPrefixes, caseSensitivePrefix);
if (!prefixesValidation.isValid) {
core.setOutput("is-valid", false);
core.setOutput("error-message", prefixesValidation.message);
return;
}
core.info("Pull Request title validation passed.");
Expand Down
2 changes: 2 additions & 0 deletions validate-pull-request-title/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export async function run(): Promise<void> {
);
if (!lengthValidation.isValid) {
core.setOutput("is-valid", false);
core.setOutput("error-message", lengthValidation.message);
return;
}

Expand All @@ -32,6 +33,7 @@ export async function run(): Promise<void> {
);
if (!prefixesValidation.isValid) {
core.setOutput("is-valid", false);
core.setOutput("error-message", prefixesValidation.message);
return;
}

Expand Down