Skip to content
This repository has been archived by the owner on Jun 6, 2019. It is now read-only.

Commit

Permalink
feat(config): add the ability to customize commit messages
Browse files Browse the repository at this point in the history
Add configuration options allowing Greenkeeper's commit message format
to be customized as the user desires. These are handled exactly like
other configuration options, as a `commitMessages` key in
`package.json`. Some messages support data coming in, the format for
this follows JavaScript's template literals.

This will allow Greenkeeper to fit in with any custom commit message
format that a project requires, instead of forcing the
conventional-commit style.

Fixes greenkeeperio#153.
  • Loading branch information
Arcanemagus committed Feb 7, 2018
1 parent b719d61 commit 6a4fae5
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 12 deletions.
7 changes: 4 additions & 3 deletions jobs/create-initial-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const env = require('../lib/env')
const getRangedVersion = require('../lib/get-ranged-version')
const dbs = require('../lib/dbs')
const getConfig = require('../lib/get-config')
const getMessage = require('../lib/get-message')
const createBranch = require('../lib/create-branch')
const statsd = require('../lib/statsd')
const { updateRepoDoc } = require('../lib/repository-docs')
Expand Down Expand Up @@ -153,7 +154,7 @@ module.exports = async function ({ repositoryId }) {
const transforms = [
{
path: 'package.json',
message: 'chore(package): update dependencies',
message: getMessage(config.commitMessages, 'initialDependencies'),
transform: oldPkg => {
const oldPkgParsed = JSON.parse(oldPkg)
const inplace = jsonInPlace(oldPkg)
Expand All @@ -168,13 +169,13 @@ module.exports = async function ({ repositoryId }) {
},
{
path: '.travis.yml',
message: 'chore(travis): whitelist greenkeeper branches',
message: getMessage(config.commitMessages, 'initialBranches'),
transform: raw => travisTransform(config, raw)
},
{
path: 'README.md',
create: true,
message: 'docs(readme): add Greenkeeper badge',
message: getMessage(config.commitMessages, 'initialBadge'),
transform: (readme, path) => {
// TODO: empty readme, no image support
const ext = extname(path).slice(1)
Expand Down
13 changes: 7 additions & 6 deletions jobs/create-version-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Log = require('gk-log')

const dbs = require('../lib/dbs')
const getConfig = require('../lib/get-config')
const getMessage = require('../lib/get-message')
const getInfos = require('../lib/get-infos')
const getRangedVersion = require('../lib/get-ranged-version')
const createBranch = require('../lib/create-branch')
Expand Down Expand Up @@ -139,17 +140,17 @@ module.exports = async function (
)
log.info('database: found open PR for this dependency', {openPR})

const commitMessageScope = !satisfies && type === 'dependencies'
? 'fix'
: 'chore'
let commitMessage = `${commitMessageScope}(package): update ${dependency} to version ${version}`

const commitMessageKey = !satisfies && type === 'dependencies'
? 'dependencyUpdate'
: 'devDependencyUpdate'
const commitMessageValues = { dependency, version }
let commitMessage = getMessage(config.commitMessages, commitMessageKey, commitMessageValues)
if (!satisfies && openPR) {
await upsert(repositories, openPR._id, {
comments: [...(openPR.comments || []), version]
})

commitMessage += `\n\nCloses #${openPR.number}`
commitMessage += getMessage(config.commitMessages, 'closes', {number: openPR.number})
}
log.info('commit message created', {commitMessage})

Expand Down
15 changes: 14 additions & 1 deletion lib/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,25 @@ module.exports = repository => {
['packages', 'package.json', 'greenkeeper'],
{}
)
/* eslint-disable no-template-curly-in-string */
return Object.assign(
{
label: 'greenkeeper',
branchPrefix: 'greenkeeper/',
ignore: []
ignore: [],
commitMessages: {
initialBadge: 'docs(readme): add Greenkeeper badge',
initialDependencies: 'chore(package): update dependencies',
initialBranches: 'chore(travis): whitelist greenkeeper branches',
dependencyUpdate: 'fix(package): update ${dependency} to version ${version}',
devDependencyUpdate: 'chore(package): update ${dependency} to version ${version}',
dependencyPin: 'fix: pin ${dependency} to ${oldVersion}',
devDependencyPin: 'chore: pin ${dependency} to ${oldVersion}',
// Conditionally appended to dependencyUpdate
closes: '\n\nCloses #${number}'
}
},
config
)
/* eslint-enable no-template-curly-in-string */
}
11 changes: 11 additions & 0 deletions lib/get-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const template = require('es6-template-strings')

module.exports = async function (commitMessages, key, values) {
if (!Object.prototype.hasOwnProperty.call(commitMessages, key)) {
throw new Error(`Unknown message key '${key}'`)
}

const templateValues = Object.assign({}, values)

return template(commitMessages[key], templateValues)
}
8 changes: 6 additions & 2 deletions lib/open-issue.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const dbs = require('./dbs')
const createBranch = require('./create-branch')
const updatedAt = require('./updated-at')
const getConfig = require('./get-config')
const getMessage = require('./get-message')
const statsd = require('./statsd')
const githubQueue = require('./github-queue')

Expand All @@ -31,7 +32,7 @@ module.exports = async function (
) {
const { repositories } = await dbs()
const repoDoc = await repositories.get(repositoryId)
const { branchPrefix, label } = getConfig(repoDoc)
const { branchPrefix, label, commitMessages } = getConfig(repoDoc)

const body = issueContent({
dependencyLink,
Expand Down Expand Up @@ -64,6 +65,9 @@ module.exports = async function (
return parsed.toString()
}

const messageValues = { dependency, oldVersion: oldVersionResolved }
const messageKey = dependencyType === 'devDependencies' ? 'dependencyPin' : 'devDependencyPin'

const sha = await createBranch({
installationId,
owner,
Expand All @@ -72,7 +76,7 @@ module.exports = async function (
newBranch,
path: 'package.json',
transform,
message: `${dependencyType === 'devDependencies' ? 'chore' : 'fix'}: pin ${dependency} to ${oldVersionResolved}`
message: getMessage(commitMessages, messageKey, messageValues)
})

await repositories.bulkDocs(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"catbox-memory": "^2.0.4",
"couchdb-bootstrap": "^1.14.0",
"envalid": "^4.0.1",
"es6-template-strings": "^2.0.1",
"escape-string-regexp": "^1.0.5",
"github-url-from-git": "^1.4.0",
"gk-log": "1.2.0",
Expand Down
53 changes: 53 additions & 0 deletions test/lib/get-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const { test } = require('tap')
const getMessage = require('../../lib/get-message')

/* eslint-disable no-template-curly-in-string */

test('get default commit messages', t => {
const commitMessages = {
initialBadge: 'docs(readme): add Greenkeeper badge',
initialDependencies: 'chore(package): update dependencies',
initialBranches: 'chore(travis): whitelist greenkeeper branches',
dependencyUpdate: 'fix(package): update ${dependency} to version ${version}',
devDependencyUpdate: 'chore(package): update ${dependency} to version ${version}',
dependencyPin: 'fix: pin ${dependency} to ${oldVersion}',
devDependencyPin: 'chore: pin ${dependency} to ${oldVersion}',
closes: '\n\nCloses #${number}'
}
const values = {
dependency: 'foo',
version: 'bar',
oldVersion: 'rab',
number: 42
}
const expected = {
initialBadge: 'docs(readme): add Greenkeeper badge',
initialDependencies: 'chore(package): update dependencies',
initialBranches: 'chore(travis): whitelist greenkeeper branches',
dependencyUpdate: 'fix(package): update foo to version bar',
devDependencyUpdate: 'chore(package): update foo to version bar',
dependencyPin: 'fix: pin foo to rab',
devDependencyPin: 'chore: pin foo to rab',
closes: '\n\nCloses #42'
}

t.plan(Object.keys(commitMessages).length)
t.is(getMessage(commitMessages, 'initialBadge'), expected.initialBadge)
t.is(getMessage(commitMessages, 'initialDependencies'), expected.initialDependencies)
t.is(getMessage(commitMessages, 'initialBranches'), expected.initialBranches)
t.is(getMessage(commitMessages, 'dependencyUpdate', values), expected.dependencyUpdate)
t.is(getMessage(commitMessages, 'devDependencyUpdate', values), expected.devDependencyUpdate)
t.is(getMessage(commitMessages, 'dependencyPin', values), expected.dependencyPin)
t.is(getMessage(commitMessages, 'devDependencyPin', values), expected.devDependencyPin)
t.is(getMessage(commitMessages, 'closes', values), expected.closes)
})

test("throws when it doesn't know a message", t => {
const commitMessages = {
foo: '42'
}
const message = "Unknown message key 'bar'"

t.plan(1)
t.throws(() => getMessage(commitMessages, 'bar'), message)
})

0 comments on commit 6a4fae5

Please sign in to comment.