diff --git a/src/validate.ts b/src/validate.ts index c7bb6e8..31737d5 100644 --- a/src/validate.ts +++ b/src/validate.ts @@ -126,3 +126,33 @@ export function checkTitle(fullTitle: string): true { return true; } + +export function formatPRTitle( + type: string, + module: string, + subject: string +): string { + if (!allowedTypes.includes(type)) { + throw new Error( + `Invalid type, expected one of ${allowedTypes.join(", ")}. Got ${type}.` + ); + } + + if (!allowedModules.includes(module)) { + throw new Error( + `Invalid module, expected one of ${allowedModules.join( + ", " + )}. Got ${module}.` + ); + } + + const prTitle = `[${type}:${module}] ${subject}`; + const maxLegnth = 50; + if (prTitle.length > maxLegnth) { + throw new Error( + `PR title exceeds the maximum allowed length of ${maxLegnth} characters.` + ); + } + + return prTitle; +} diff --git a/test/validate.spec.ts b/test/validate.spec.ts index c8f02f3..a199dd5 100644 --- a/test/validate.spec.ts +++ b/test/validate.spec.ts @@ -1,5 +1,10 @@ import { expect } from "chai"; -import { allowedModules, allowedTypes, checkTitle } from "../src/validate"; +import { + allowedModules, + allowedTypes, + checkTitle, + formatPRTitle, +} from "../src/validate"; describe("validate", () => { describe("checkTitle", () => { @@ -146,4 +151,16 @@ describe("validate", () => { }); }); }); + + describe("formatPRTitle", () => { + it("should format PR Title correctly", () => { + const type = "Feature"; + const module = "Submission"; + const subject = "Add support for file uploads"; + + const formattedTitle = formatPRTitle(type, module, subject); + const expectedTitle = `[${type}:${module}] ${subject}`; + expect(formattedTitle).to.equal(expectedTitle); + }); + }); });