From 939b9b261c52f64926f366dce2319147f881610c Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Wed, 18 Dec 2024 07:53:44 -0500 Subject: [PATCH] Set title/body from commit-message if given If `title`, `body`, and `body-path` are all not given, and `commit-message` is, parse the `title` and `body` from the 'commit-message'. A properly formatted message (title, blank line, then body) will be used as title and body. Otherwise, the first line is used as title and the remaining lines as body. Another valid approach could be to take all the lines as title, or all the lines until the first blank as title, but the former is unlikely to be the user's intention and the latter would introduce complexity. The parsing function was added in `utils` to facilitate testing. --- __test__/utils.unit.test.ts | 34 ++++++++++++++++++++++++++++++++++ src/main.ts | 12 ++++++++++++ src/utils.ts | 12 ++++++++++++ 3 files changed, 58 insertions(+) diff --git a/__test__/utils.unit.test.ts b/__test__/utils.unit.test.ts index b7805d29..4a34e529 100644 --- a/__test__/utils.unit.test.ts +++ b/__test__/utils.unit.test.ts @@ -37,6 +37,40 @@ describe('utils tests', () => { expect(array[2]).toEqual('team3') }) + test('getTitleBodyFromCommitMessage works for single-line', async () => { + const {title, body} = utils.getTitleBodyFromCommitMessage( + 'This is a commit message' + ) + expect(title).toEqual('This is a commit message') + expect(body).toEqual('') + }) + + test('getTitleBodyFromCommitMessage works for multi-line', async () => { + const {title, body} = utils.getTitleBodyFromCommitMessage( + 'This is a commit message\n' + + 'That is not properly formatted with blank line\n' + + 'between title and body' + ) + expect(title).toEqual('This is a commit message') + expect(body).toEqual( + 'That is not properly formatted with blank line\n' + + 'between title and body' + ) + }) + + test('getTitleBodyFromCommitMessage works for title with body', async () => { + const {title, body} = utils.getTitleBodyFromCommitMessage( + 'This is a commit message\n' + + '\n' + + 'That IS properly formatted with blank line\n' + + 'between title and body' + ) + expect(title).toEqual('This is a commit message') + expect(body).toEqual( + 'That IS properly formatted with blank line\n' + 'between title and body' + ) + }) + test('getRepoPath successfully returns the path to the repository', async () => { expect(utils.getRepoPath()).toEqual(process.env['GITHUB_WORKSPACE']) expect(utils.getRepoPath('foo')).toEqual( diff --git a/src/main.ts b/src/main.ts index dad26794..eb5eb499 100644 --- a/src/main.ts +++ b/src/main.ts @@ -44,6 +44,18 @@ async function run(): Promise { if (!inputs.token) { throw new Error(`Input 'token' not supplied. Unable to continue.`) } + if ( + inputs.commitMessage && + inputs.commitMessage !== '' && + inputs.body === '' && + inputs.bodyPath === '' + ) { + const {title, body} = utils.getTitleBodyFromCommitMessage( + inputs.commitMessage + ) + inputs.title = title + inputs.body = body + } if (!inputs.branchToken) { inputs.branchToken = inputs.token } diff --git a/src/utils.ts b/src/utils.ts index ced3cbdf..082b2677 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -26,6 +26,18 @@ export function stripOrgPrefixFromTeams(teams: string[]): string[] { }) } +export function getTitleBodyFromCommitMessage(commitMessage: string): { + title: string + body: string +} { + const lines = commitMessage.split('\n') + + return { + title: lines[0] ?? '', + body: lines.slice(1).join('\n').trim() + } +} + export function getRepoPath(relativePath?: string): string { let githubWorkspacePath = process.env['GITHUB_WORKSPACE'] if (!githubWorkspacePath) {