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) {