Skip to content

Commit

Permalink
feature: refactor and improve --commit-url
Browse files Browse the repository at this point in the history
  • Loading branch information
rvagg committed Nov 9, 2018
1 parent 64a8fde commit 9c700d2
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ $ npm i changelog-maker -g

* `--simple`: print a simple form, without additional Markdown cruft
* `--group`: reorder commits so that they are listed in groups where the `xyz:` prefix of the commit message defines the group. Commits are listed in original order _within_ group.
* `--commit-url`: pass in a url template which will be used to generate commit URLs for a repository not hosted in Github. `{ref}` is the placeholder that will be replaced with the commit, i.e. `--commit-url=https://gitlab.com/myUser/myRepo/commit/{ref}`
* `--commit-url`: pass in a url template which will be used to generate commit URLs for a repository not hosted in Github. `{ref}` is the placeholder that will be replaced with the commit, i.e. `--commit-url=https://gitlab.com/myUser/myRepo/commit/{ref}`
* `--start-ref=<ref>`: use the given git `<ref>` as a starting point rather than the _last tag_. The `<ref>` can be anything commit-ish including a commit sha, tag, branch name. If you specify a `--start-ref` argument the commit log will not be pruned so that version commits and `working on <version>` commits are left in the list.
* `--end-ref=<ref>`: use the given git `<ref>` as a end-point rather than the _now_. The `<ref>` can be anything commit-ish including a commit sha, tag, branch name.
* `--filter-release`: exclude Node-style release commits from the list. e.g. "Working on v1.0.0" or "2015-10-21 Version 2.0.0" and also "npm version X" style commits containing _only_ an `x.y.z` semver designator.
* `--quiet` or `-q`: do not print to `process.stdout`
* `--all` or `-a`: process all commits since beginning, instead of last tag.
* `--help` or `-h`: show usage and help.
* `--quiet` or `-q`: do not print to `process.stdout`
* `--all` or `-a`: process all commits since beginning, instead of last tag.
* `--help` or `-h`: show usage and help.

## License

Expand Down
6 changes: 3 additions & 3 deletions changelog-maker.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const argv = require('minimist')(process.argv.slice(2))
const quiet = argv.quiet || argv.q
const simple = argv.simple || argv.s
const help = argv.h || argv.help
const commitUrl = argv['commit-url']
const commitUrl = argv['commit-url'] || 'https://github.com/{ghUser}/{ghRepo}/commit/{ref}'
const pkgFile = path.join(process.cwd(), 'package.json')
const pkgData = fs.existsSync(pkgFile) ? require(pkgFile) : {}
const pkgId = pkgtoId(pkgData)

const ghId = {
user: argv._[0] || pkgId.user || 'nodejs',
name: argv._[1] || (pkgId.name && stripScope(pkgId.name)) || 'node'
repo: argv._[1] || (pkgId.name && stripScope(pkgId.name)) || 'node'
}
const gitcmd = 'git log --pretty=full --since="{{sincecmd}}" --until="{{untilcmd}}"'
const commitdatecmd = '$(git show -s --format=%cd `{{refcmd}}`)'
Expand Down Expand Up @@ -139,5 +139,5 @@ debug('%s', _gitcmd)

gitexec.exec(process.cwd(), _gitcmd)
.pipe(split2())
.pipe(commitStream(ghId.user, ghId.name))
.pipe(commitStream(ghId.user, ghId.repo))
.pipe(list.obj(onCommitList))
6 changes: 3 additions & 3 deletions commit-to-output.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function toStringSimple (data) {
s += data.revert ? '" ' : ' '
s += data.author ? '(' + data.author + ') ' : ''
s += data.pr ? data.prUrl : ''
s = s.trim()

return (data.semver && data.semver.length)
? chalk.green(chalk.bold(s))
Expand Down Expand Up @@ -48,17 +49,16 @@ function commitToOutput (commit, simple, ghId, commitUrl) {
let data = {}
let prUrlMatch = commit.prUrl && commit.prUrl.match(/^https?:\/\/.+\/([^/]+\/[^/]+)\/\w+\/\d+$/i)
let urlHash = '#' + commit.ghIssue || commit.prUrl
let ghUrl = ghId.user + '/' + ghId.name
let ref = commit.sha.substr(0, 10)

data.sha = commit.sha
data.shaUrl = commitUrl ? commitUrl.replace('{ref}', ref) : 'https://github.com/' + ghUrl + '/commit/' + ref
data.shaUrl = commitUrl.replace(/\{ghUser\}/g, ghId.user).replace(/\{ghRepo\}/g, ghId.repo).replace(/\{ref\}/g, ref)
data.semver = commit.labels && commit.labels.filter((l) => l.indexOf('semver') > -1)
data.revert = reverts.isRevert(commit.summary)
data.group = groups.toGroups(commit.summary)
data.summary = groups.cleanSummary(reverts.cleanSummary(commit.summary))
data.author = (commit.author && commit.author.name) || ''
data.pr = prUrlMatch && ((prUrlMatch[1] !== ghUrl ? prUrlMatch[1] : '') + urlHash)
data.pr = prUrlMatch && ((prUrlMatch[1] !== `${ghId.user}/${ghId.repo}` ? prUrlMatch[1] : '') + urlHash)
data.prUrl = prUrlMatch && commit.prUrl

return (simple ? toStringSimple : toStringMarkdown)(data)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/rvagg/changelog-maker.git"
"url": "https://github.com/nodejs/changelog-maker.git"
},
"preferGlobal": true,
"scripts": {
Expand Down
83 changes: 78 additions & 5 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// NOTE: to run this you will probably need to be authorized with GitHub.
// Run `./changelog-maker.js` by itself to set this up.

const test = require('tape')
const execSync = require('child_process').execSync

Expand All @@ -9,11 +12,81 @@ function exec (args) {

test('test basic commit block', function (t) {
t.equal(exec('--start-ref=v1.3.9 --end-ref=v1.3.10'),
`* [[\`e28b3f2813\`](https://github.com/rvagg/changelog-maker/commit/e28b3f2813)] - 1.3.10 (Rod Vagg)
* [[\`ace3af943e\`](https://github.com/rvagg/changelog-maker/commit/ace3af943e)] - Merge pull request #13 from jamsyoung/private-repo-support (Rod Vagg)
* [[\`25ec5428bc\`](https://github.com/rvagg/changelog-maker/commit/25ec5428bc)] - default to repo scope always - revert previous changes (James Young)
* [[\`424d6c22c1\`](https://github.com/rvagg/changelog-maker/commit/424d6c22c1)] - add --private arg to set repo scope, update readme (James Young)
* [[\`7c8c5e6215\`](https://github.com/rvagg/changelog-maker/commit/7c8c5e6215)] - 1.3.9 (Rod Vagg)
`* [[\`e28b3f2813\`](https://github.com/nodejs/changelog-maker/commit/e28b3f2813)] - 1.3.10 (Rod Vagg)
* [[\`ace3af943e\`](https://github.com/nodejs/changelog-maker/commit/ace3af943e)] - Merge pull request #13 from jamsyoung/private-repo-support (Rod Vagg)
* [[\`25ec5428bc\`](https://github.com/nodejs/changelog-maker/commit/25ec5428bc)] - default to repo scope always - revert previous changes (James Young)
* [[\`424d6c22c1\`](https://github.com/nodejs/changelog-maker/commit/424d6c22c1)] - add --private arg to set repo scope, update readme (James Young)
* [[\`7c8c5e6215\`](https://github.com/nodejs/changelog-maker/commit/7c8c5e6215)] - 1.3.9 (Rod Vagg)
`)
t.end()
})

test('test filter-release', function (t) {
t.equal(exec('--start-ref=v1.3.9 --end-ref=v1.3.10 --filter-release'),
`* [[\`ace3af943e\`](https://github.com/nodejs/changelog-maker/commit/ace3af943e)] - Merge pull request #13 from jamsyoung/private-repo-support (Rod Vagg)
* [[\`25ec5428bc\`](https://github.com/nodejs/changelog-maker/commit/25ec5428bc)] - default to repo scope always - revert previous changes (James Young)
* [[\`424d6c22c1\`](https://github.com/nodejs/changelog-maker/commit/424d6c22c1)] - add --private arg to set repo scope, update readme (James Young)
`)
t.end()
})

test('test simple', function (t) {
t.equal(exec('--start-ref=v1.3.9 --end-ref=v1.3.10 --simple'),
`* [e28b3f2813] - 1.3.10 (Rod Vagg)
* [ace3af943e] - Merge pull request #13 from jamsyoung/private-repo-support (Rod Vagg)
* [25ec5428bc] - default to repo scope always - revert previous changes (James Young)
* [424d6c22c1] - add --private arg to set repo scope, update readme (James Young)
* [7c8c5e6215] - 1.3.9 (Rod Vagg)
`)
t.end()
})

// TODO: replace HEAD with last commit for TODAY
test('test group, semver labels, PR-URL', function (t) {
t.equal(exec('--start-ref=v2.2.7 --end-ref=HEAD --group --filter-release --simple'),
`* [cc442b6534] - (SEMVER-MINOR) minor nit (Rod Vagg) https://github.com/nodejs/node/pull/23715
* [4f2b7f8136] - deps: use strip-ansi instead of chalk.stripColor (Rod Vagg)
* [6898501e18] - deps: update deps, introduce test & lint deps (Rod Vagg)
* [3e367bd67c] - feature: refactor and improve --commit-url (Rod Vagg)
* [5094524655] - feature: make the commit url configurable via an additional argument (Jim Nielsen) https://github.com/nodejs/changelog-maker/pull/55
* [42f248cf89] - src: use \`standard\` for linting (Rod Vagg)
* [64a8fdef3c] - test: basic test infrastructure (Rod Vagg)
`)
t.end()
})

// TODO: replace HEAD with last commit for TODAY
test('test simple group, semver labels, PR-URL', function (t) {
t.equal(exec('--start-ref=v2.2.7 --end-ref=HEAD --group --filter-release'),
`* [[\`cc442b6534\`](https://github.com/nodejs/changelog-maker/commit/cc442b6534)] - **(SEMVER-MINOR)** minor nit (Rod Vagg) [nodejs/node#23715](https://github.com/nodejs/node/pull/23715)
* [[\`4f2b7f8136\`](https://github.com/nodejs/changelog-maker/commit/4f2b7f8136)] - **deps**: use strip-ansi instead of chalk.stripColor (Rod Vagg)
* [[\`6898501e18\`](https://github.com/nodejs/changelog-maker/commit/6898501e18)] - **deps**: update deps, introduce test & lint deps (Rod Vagg)
* [[\`3e367bd67c\`](https://github.com/nodejs/changelog-maker/commit/3e367bd67c)] - **feature**: refactor and improve --commit-url (Rod Vagg)
* [[\`5094524655\`](https://github.com/nodejs/changelog-maker/commit/5094524655)] - **feature**: make the commit url configurable via an additional argument (Jim Nielsen) [#55](https://github.com/nodejs/changelog-maker/pull/55)
* [[\`42f248cf89\`](https://github.com/nodejs/changelog-maker/commit/42f248cf89)] - **src**: use \`standard\` for linting (Rod Vagg)
* [[\`64a8fdef3c\`](https://github.com/nodejs/changelog-maker/commit/64a8fdef3c)] - **test**: basic test infrastructure (Rod Vagg)
`)
t.end()
})

test('test blank commit-url', function (t) {
let actual = exec('--start-ref=v2.2.7 --end-ref=HEAD --filter-release --commit-url=http://foo.bar/').split('\n')
actual.splice(0, actual.length - 3)
actual = actual.join('\n')
t.equal(actual,
`* [[\`cc442b6534\`](http://foo.bar/)] - **(SEMVER-MINOR)** minor nit (Rod Vagg) [nodejs/node#23715](https://github.com/nodejs/node/pull/23715)
* [[\`5094524655\`](http://foo.bar/)] - **feature**: make the commit url configurable via an additional argument (Jim Nielsen) [#55](https://github.com/nodejs/changelog-maker/pull/55)
`)
t.end()
})

test('test blank commit-url', function (t) {
let actual = exec('--start-ref=v2.2.7 --end-ref=HEAD --filter-release --commit-url=https://yeehaw.com/{ref}/{ref}/{ghUser}/{ghRepo}/').split('\n')
actual.splice(0, actual.length - 3)
actual = actual.join('\n')
t.equal(actual,
`* [[\`cc442b6534\`](https://yeehaw.com/cc442b6534/cc442b6534/nodejs/changelog-maker/)] - **(SEMVER-MINOR)** minor nit (Rod Vagg) [nodejs/node#23715](https://github.com/nodejs/node/pull/23715)
* [[\`5094524655\`](https://yeehaw.com/5094524655/5094524655/nodejs/changelog-maker/)] - **feature**: make the commit url configurable via an additional argument (Jim Nielsen) [#55](https://github.com/nodejs/changelog-maker/pull/55)
`)
t.end()
})

0 comments on commit 9c700d2

Please sign in to comment.