From f0d209c27a35804118065c979592830fd1ab86ed Mon Sep 17 00:00:00 2001 From: Corey Butler Date: Fri, 3 Apr 2020 00:22:43 -0500 Subject: [PATCH] Added simplistic commit message template --- README.md | 20 ++++++++++++++++++++ action.yml | 3 +++ app/lib/tag.js | 26 ++++++++++++++++++-------- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3394062..bef8938 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,26 @@ This is the annotated commit message associated with the tag. By default, a chan tag_message: "Custom message goes here." ``` +#### commit_message_template + +By default, a changelog is generated, containing the commit messages since the last release. The message is generated by applying a commit message template to each commit's data attributes. + +```yaml +- uses: butlerlogic/action-autotag@1.0.0 + with: + GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" + commit_message_template: "({{sha}} by {{author}}) {{message}}" +``` + +Optional data points: + +1. `number` The commit number (relevant to the overall list) +1. `message` The commit message. +1. `author` The author of the commit. +1. `sha` The SHA value representing the commit. + +The default is `{{number}}) {{message}} ({{author}})\nSHA: {{sha}}\n`. + #### version Explicitly set the version instead of using automatic detection. diff --git a/action.yml b/action.yml index 8a912d5..01e0c90 100644 --- a/action.yml +++ b/action.yml @@ -25,6 +25,9 @@ inputs: tag_message: description: This is the annotated commit message associated with the tag. By default, a changelog will be generated from the commits between the latest tag and the new tag (HEAD). This will override that with a hard-coded message. required: false + commit_message_template: + description: "The commit message template (per commit). Default is `{{number}}) {{message}} ({{author}})\nSHA: {{sha}}\n`" + required: false version: description: Explicitly set the version here instead of automatically detecting from `package.json`. Useful for non-JavaScript projects where version may be output by a previous action. required: false diff --git a/app/lib/tag.js b/app/lib/tag.js index c4d69f3..857c45b 100644 --- a/app/lib/tag.js +++ b/app/lib/tag.js @@ -55,18 +55,28 @@ export default class Tag { } const changelog = await github.repos.compareCommits({ owner, repo, base: tags.shift().name, head: 'master' }) + const tpl = (core.getInput('commit_message_template', { required: false }) || '').trim() return changelog.data.commits .map( - (commit, i) => - `${i === 0 ? '\n' : ''}${i + 1}) ${commit.commit.message}${ - commit.hasOwnProperty('author') - ? commit.author.hasOwnProperty('login') - ? ' (' + commit.author.login + ')' + (commit, i) => { + if (tpl.length > 0) { + return tpl + .replace(/\{\{\s?(number)\s?\}\}/gi, i + 1) + .replace(/\{\{\s?(message)\s?\}\}/gi, commit.commit.message) + .replace(/\{\{\s?(author)\s?\}\}/gi, commit.hasOwnProperty('author') ? (commit.author.hasOwnProperty('login') ? commit.author.login : '') : '') + .replace(/\{\{\s?(sha)\s?\}\}/gi, commit.sha) + .trim() + '\n' + } else { + return `${i === 0 ? '\n' : ''}${i + 1}) ${commit.commit.message}${ + commit.hasOwnProperty('author') + ? commit.author.hasOwnProperty('login') + ? ' (' + commit.author.login + ')' + : '' : '' - : '' - }\n(SHA: ${commit.sha})\n` - ) + }\n(SHA: ${commit.sha})\n` + } + }) .join('\n') } catch (e) { core.warning('Failed to generate changelog from commits: ' + e.message + os.EOL)