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.
This commit is contained in:
patrick brisbin 2024-12-18 07:53:44 -05:00
parent fa9200e5b4
commit 939b9b261c
No known key found for this signature in database
GPG Key ID: 07BF97A312D7F34C
3 changed files with 58 additions and 0 deletions

View File

@ -37,6 +37,40 @@ describe('utils tests', () => {
expect(array[2]).toEqual('team3') 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 () => { test('getRepoPath successfully returns the path to the repository', async () => {
expect(utils.getRepoPath()).toEqual(process.env['GITHUB_WORKSPACE']) expect(utils.getRepoPath()).toEqual(process.env['GITHUB_WORKSPACE'])
expect(utils.getRepoPath('foo')).toEqual( expect(utils.getRepoPath('foo')).toEqual(

View File

@ -44,6 +44,18 @@ async function run(): Promise<void> {
if (!inputs.token) { if (!inputs.token) {
throw new Error(`Input 'token' not supplied. Unable to continue.`) 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) { if (!inputs.branchToken) {
inputs.branchToken = inputs.token inputs.branchToken = inputs.token
} }

View File

@ -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 { export function getRepoPath(relativePath?: string): string {
let githubWorkspacePath = process.env['GITHUB_WORKSPACE'] let githubWorkspacePath = process.env['GITHUB_WORKSPACE']
if (!githubWorkspacePath) { if (!githubWorkspacePath) {