Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Support commit's URL and issues autolink in changelog #16

Merged
merged 6 commits into from
Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 33 additions & 27 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,50 @@

### Added

- :sparkles: Add group to commit object (fafbf3635b75ee3f1d0e580976569017025f05b6)
- :sparkles: Use handlebars templating to generate markdown (#10) (141d1601fd1fa3b278db05acbc8f47e2bb239bbf)
- :sparkles: Generate changelog for the next release (#8) (2d783e44e7d4b84a993237e27b95a5d27532426b)
- :sparkles: Implement format option (#5) (9b7287d167637b7a02387cea135d1d7d44a90695)
- :sparkles: Convert changelog to markdown (b165f695f4c1a49ff16a5f03918545bfb36cf367)
- :sparkles: Generate markdown first draft function (10c6d3e20b82f5a0f6ce5cd372899e6519bc2412)
- :tada: Intialize markdown project (a4a12bb4f7133e7e5e40436da4c884f135abf03d)
- :sparkles: Generate changelog JSON file (86ca3eaefb18fd9c9b6bb4256ed2f6fa711aef59)
- :sparkles: Handle when no repository found (c912efd73b9b286711d468ccb73bf1a03bd6f848)
- :tada: intialize cli project (0b4f39408e675f368c9562bd7645ac74abcd2293)
- :sparkles: implements changelog generation to json (44e401db08cc828fcc8c19b027350ac9d310773e)
- :sparkles: add edge cases for commit parsing (cb35644a640cfe3cb2c246345fe25202d814c436)
- :sparkles: Implements parse commit function (195b59431dde83a4cff26be5c3ab362d97a9604e)
- :white_check_mark: add gitlab and bitbucket tests ([0203483](https://github.com/frinyvonnick/gitmoji-changelog/commit/0203483f17111a30be879797c398bb87df86e9ab))
- :sparkles: Add autolink to issues and PRs in commit subject and body ([#17](https://github.com/frinyvonnick/gitmoji-changelog/issues/17)) ([43449fc](https://github.com/frinyvonnick/gitmoji-changelog/commit/43449fc628facf06fe453c8f940b7a0887372820))
- :sparkles: support commit url in changelog ([02c72b0](https://github.com/frinyvonnick/gitmoji-changelog/commit/02c72b0214a8b3219bc69969321de6ba0b663b26))
- :sparkles: Group commits by changelog types ([#11](https://github.com/frinyvonnick/gitmoji-changelog/issues/11)) ([7b8b49b](https://github.com/frinyvonnick/gitmoji-changelog/commit/7b8b49b366d3d51f0cc75f4ffb67efcd633cca16))
- :sparkles: Use handlebars templating to generate markdown ([#10](https://github.com/frinyvonnick/gitmoji-changelog/issues/10)) ([141d160](https://github.com/frinyvonnick/gitmoji-changelog/commit/141d1601fd1fa3b278db05acbc8f47e2bb239bbf))
- :sparkles: Generate changelog for the next release ([#8](https://github.com/frinyvonnick/gitmoji-changelog/issues/8)) ([2d783e4](https://github.com/frinyvonnick/gitmoji-changelog/commit/2d783e44e7d4b84a993237e27b95a5d27532426b))
- :sparkles: Implement format option ([#5](https://github.com/frinyvonnick/gitmoji-changelog/issues/5)) ([9b7287d](https://github.com/frinyvonnick/gitmoji-changelog/commit/9b7287d167637b7a02387cea135d1d7d44a90695))
- :sparkles: Convert changelog to markdown ([b165f69](https://github.com/frinyvonnick/gitmoji-changelog/commit/b165f695f4c1a49ff16a5f03918545bfb36cf367))
- :sparkles: Generate markdown first draft function ([10c6d3e](https://github.com/frinyvonnick/gitmoji-changelog/commit/10c6d3e20b82f5a0f6ce5cd372899e6519bc2412))
- :tada: Intialize markdown project ([a4a12bb](https://github.com/frinyvonnick/gitmoji-changelog/commit/a4a12bb4f7133e7e5e40436da4c884f135abf03d))
- :sparkles: Generate changelog JSON file ([86ca3ea](https://github.com/frinyvonnick/gitmoji-changelog/commit/86ca3eaefb18fd9c9b6bb4256ed2f6fa711aef59))
- :sparkles: Handle when no repository found ([c912efd](https://github.com/frinyvonnick/gitmoji-changelog/commit/c912efd73b9b286711d468ccb73bf1a03bd6f848))
- :tada: intialize cli project ([0b4f394](https://github.com/frinyvonnick/gitmoji-changelog/commit/0b4f39408e675f368c9562bd7645ac74abcd2293))
- :sparkles: implements changelog generation to json ([44e401d](https://github.com/frinyvonnick/gitmoji-changelog/commit/44e401db08cc828fcc8c19b027350ac9d310773e))
- :sparkles: add edge cases for commit parsing ([cb35644](https://github.com/frinyvonnick/gitmoji-changelog/commit/cb35644a640cfe3cb2c246345fe25202d814c436))
- :sparkles: Implements parse commit function ([195b594](https://github.com/frinyvonnick/gitmoji-changelog/commit/195b59431dde83a4cff26be5c3ab362d97a9604e))

### Changed

- :recycle: remove babel to use native node 10 (#3) (6edd0c48591e935f3bcd7e73d48733e623f779d9)
- :truck: move parse functions into parser.js (e29f239dee8dc393caee9d320371e54a37eb90ae)
- :wrench: Fix main script in core module (7f091a3900605ee9bc44e793ddbb10a7272112fa)
- :wrench: Add build script (efbc04d902ac201b128a8e02692b778eff109b12)
- :wrench: Add lint (9faf008d457d777afe0fa3443c34af510c1098fa)
- :wrench: Set up tests (656c8bbb506fc8f9064df1fc7aa64e2f1869751e)
- :wrench: Add package.json (34c8f53a58487a6368016de09989465cd2a96786)
- :recycle: remove babel to use native node 10 ([#3](https://github.com/frinyvonnick/gitmoji-changelog/issues/3)) ([6edd0c4](https://github.com/frinyvonnick/gitmoji-changelog/commit/6edd0c48591e935f3bcd7e73d48733e623f779d9))
- :truck: move parse functions into parser.js ([e29f239](https://github.com/frinyvonnick/gitmoji-changelog/commit/e29f239dee8dc393caee9d320371e54a37eb90ae))
- :wrench: Fix main script in core module ([7f091a3](https://github.com/frinyvonnick/gitmoji-changelog/commit/7f091a3900605ee9bc44e793ddbb10a7272112fa))
- :wrench: Add build script ([efbc04d](https://github.com/frinyvonnick/gitmoji-changelog/commit/efbc04d902ac201b128a8e02692b778eff109b12))
- :wrench: Add lint ([9faf008](https://github.com/frinyvonnick/gitmoji-changelog/commit/9faf008d457d777afe0fa3443c34af510c1098fa))
- :wrench: Set up tests ([656c8bb](https://github.com/frinyvonnick/gitmoji-changelog/commit/656c8bbb506fc8f9064df1fc7aa64e2f1869751e))
- :wrench: Add package.json ([34c8f53](https://github.com/frinyvonnick/gitmoji-changelog/commit/34c8f53a58487a6368016de09989465cd2a96786))

### Removed

- :fire: remove parsing multiple commit (73207b70d85ad2371def0ec9045e5a6ea46fee8c)
- :fire: remove parsing multiple commit ([73207b7](https://github.com/frinyvonnick/gitmoji-changelog/commit/73207b70d85ad2371def0ec9045e5a6ea46fee8c))

### Fixed

- :green_heart: Ignore node_modules in linter (802c0f7dc29508876c6b4d178bbcc8274ee308b5)
- :green_heart: Set up travis configuration (dda9b287989c1ca2e9513c4a5a4a3d1b6749e816)
- :green_heart: Ignore node_modules in linter ([802c0f7](https://github.com/frinyvonnick/gitmoji-changelog/commit/802c0f7dc29508876c6b4d178bbcc8274ee308b5))
- :green_heart: Set up travis configuration ([dda9b28](https://github.com/frinyvonnick/gitmoji-changelog/commit/dda9b287989c1ca2e9513c4a5a4a3d1b6749e816))

### Miscellaneous

- :memo: Add README.md (c177349386dfd87ab41c58f8a317b0962a511207)
- :package: Fix build (a1526b38e164b8471954e3dd0c658e10595ee966)
- :package: Build dist file (acba6f776196d8312c470ccefce8be81bafd8d52)
- Initial commit (a60d98def5bcbbef74a19db9e8f43af7a6ff6865)
- :refactor: rename meta.repoInfo to meta.repository ([8f97208](https://github.com/frinyvonnick/gitmoji-changelog/commit/8f9720891e7a970a3dca1954ed4f0a570daaee05))
- :refactor: clean some code ([7af3086](https://github.com/frinyvonnick/gitmoji-changelog/commit/7af3086dfbc5d514434d990855d191ca81988bf1))
- :memo: Update roadmap ([02cec25](https://github.com/frinyvonnick/gitmoji-changelog/commit/02cec25ce329d68a02d1669913495108d93e2285))
- :memo: Add README.md ([c177349](https://github.com/frinyvonnick/gitmoji-changelog/commit/c177349386dfd87ab41c58f8a317b0962a511207))
- :package: Fix build ([a1526b3](https://github.com/frinyvonnick/gitmoji-changelog/commit/a1526b38e164b8471954e3dd0c658e10595ee966))
- :package: Build dist file ([acba6f7](https://github.com/frinyvonnick/gitmoji-changelog/commit/acba6f776196d8312c470ccefce8be81bafd8d52))
- Initial commit ([a60d98d](https://github.com/frinyvonnick/gitmoji-changelog/commit/a60d98def5bcbbef74a19db9e8f43af7a6ff6865))


6 changes: 3 additions & 3 deletions packages/gitmoji-changelog-cli/src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const fs = require('fs')

async function main({ format } = {}) {
try {
const changes = await generateChangelog()
const changelog = await generateChangelog()

switch (format) {
case 'json':
fs.writeFileSync('./CHANGELOG.json', JSON.stringify(changes))
fs.writeFileSync('./CHANGELOG.json', JSON.stringify(changelog))
break
case 'markdown':
default: {
fs.writeFileSync('./CHANGELOG.md', convert(changes))
fs.writeFileSync('./CHANGELOG.md', convert(changelog))
}
}
} catch (e) {
Expand Down
5 changes: 5 additions & 0 deletions packages/gitmoji-changelog-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@
"homepage": "https://github.com/frinyvonnick/gitmoji-changelog#readme",
"dependencies": {
"concat-stream": "^1.6.2",
"get-pkg-repo": "^2.0.0",
"git-raw-commits": "^2.0.0",
"git-remote-origin-url": "^2.0.0",
"git-semver-tags": "^2.0.0",
"lodash": "^4.17.11",
"normalize-package-data": "^2.4.0",
"read-pkg-up": "^4.0.0",
"split-lines": "^2.0.0",
"through2": "^2.0.3"
}
Expand Down
14 changes: 11 additions & 3 deletions packages/gitmoji-changelog-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const concat = require('concat-stream')
const { promisify } = require('util')

const { parseCommit } = require('./parser')
const { getRepoInfo } = require('./repoInfo')
const mapping = require('./mapping')

const gitSemverTagsAsync = promisify(gitSemverTags)
Expand Down Expand Up @@ -36,10 +37,14 @@ function makeGroups(commits) {
}

async function generateChangelog() {
const meta = {
repository: await getRepoInfo(),
}

let previousTag = ''
const tags = await gitSemverTagsAsync()

const result = await Promise.all(tags.map(async tag => {
const changes = await Promise.all(tags.map(async tag => {
const commits = await getCommits(previousTag, tag)

previousTag = tag
Expand All @@ -50,12 +55,15 @@ async function generateChangelog() {
}))

const commits = await getCommits(previousTag)
result.push({
changes.push({
version: 'next',
groups: makeGroups(commits),
})

return result
return {
meta,
changes,
}
}

module.exports = {
Expand Down
8 changes: 4 additions & 4 deletions packages/gitmoji-changelog-core/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ describe('changelog', () => {
it('should generate changelog in json format for next release', async () => {
gitSemverTags.mockImplementation((cb) => cb(null, []))

const result = await generateChangelog()
const { changes } = await generateChangelog()

expect(result).toEqual([
expect(changes).toEqual([
{
version: 'next',
groups: [
Expand All @@ -47,9 +47,9 @@ describe('changelog', () => {
it('should generate changelog in json format for all tags', async () => {
gitSemverTags.mockImplementation((cb) => cb(null, ['v1.0.0']))

const result = await generateChangelog()
const { changes } = await generateChangelog()

expect(result).toEqual([
expect(changes).toEqual([
{
version: 'v1.0.0',
groups: [
Expand Down
49 changes: 49 additions & 0 deletions packages/gitmoji-changelog-core/src/repoInfo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const readPkgUp = require('read-pkg-up')
const getPkgRepo = require('get-pkg-repo')
const gitRemoteOriginUrl = require('git-remote-origin-url')
const normalizePackageData = require('normalize-package-data')
const { isEmpty } = require('lodash')

async function getRepoInfo() {
// get the closest package.json file
let packageJson = {}
try {
const packageInfo = await readPkgUp()
packageJson = packageInfo.pkg || {}
} catch (e) {
// error reading package.json
}

// if pkg not found or empty, get git remote origin
if (!packageJson.repository || !packageJson.repository.url) {
try {
const url = await gitRemoteOriginUrl()
packageJson.repository = { url }
normalizePackageData(packageJson)
} catch (e) {
return null
}
}

// extract repo info from package.json info
try {
const repo = getPkgRepo(packageJson)

if (isEmpty(repo)) return null

return {
type: repo.type,
domain: repo.domain,
user: repo.user,
project: repo.project,
url: repo.browse(),
bugsUrl: repo.bugs(),
}
} catch (e) {
return null
}
}

module.exports = {
getRepoInfo,
}
101 changes: 101 additions & 0 deletions packages/gitmoji-changelog-core/src/repoInfo.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/* eslint-disable global-require */
const readPkgUp = require('read-pkg-up')
const gitRemoteOriginUrl = require('git-remote-origin-url')

const { getRepoInfo } = require('./repoInfo')

describe('getRepoInfo', () => {
it('should extract github repo info from package.json', async () => {
readPkgUp.mockImplementationOnce(() => Promise.resolve({
pkg: {
repository: {
type: 'git',
url: 'git+https://github.com/frinyvonnick/gitmoji-changelog.git',
},
},
}))

const result = await getRepoInfo()

expect(result).toEqual({
type: 'github',
domain: 'github.com',
user: 'frinyvonnick',
project: 'gitmoji-changelog',
url: 'https://github.com/frinyvonnick/gitmoji-changelog',
bugsUrl: 'https://github.com/frinyvonnick/gitmoji-changelog/issues',
})
})

it('should extract repo info from .git info if nothing in package.json', async () => {
readPkgUp.mockImplementationOnce(() => Promise.resolve({}))
gitRemoteOriginUrl.mockImplementationOnce(() => Promise.resolve('git+https://github.com/frinyvonnick/gitmoji-changelog.git'))

const result = await getRepoInfo()

expect(result).toEqual({
type: 'github',
domain: 'github.com',
user: 'frinyvonnick',
project: 'gitmoji-changelog',
url: 'https://github.com/frinyvonnick/gitmoji-changelog',
bugsUrl: 'https://github.com/frinyvonnick/gitmoji-changelog/issues',
})
})

it('should return null if no git info found', async () => {
readPkgUp.mockImplementationOnce(() => Promise.resolve({}))
gitRemoteOriginUrl.mockImplementationOnce(() => Promise.resolve(null))

const result = await getRepoInfo()

expect(result).toBeNull()
})

it('should extract gitlab repo info from package.json', async () => {
readPkgUp.mockImplementationOnce(() => Promise.resolve({
pkg: {
repository: {
type: 'git',
url: 'git+https://gitlab.com/gitlab-user/gitlab-project.git',
},
},
}))

const result = await getRepoInfo()

expect(result).toEqual({
type: 'gitlab',
domain: 'gitlab.com',
user: 'gitlab-user',
project: 'gitlab-project',
url: 'https://gitlab.com/gitlab-user/gitlab-project',
bugsUrl: 'https://gitlab.com/gitlab-user/gitlab-project/issues',
})
})

it('should extract bitbucket repo info from package.json', async () => {
readPkgUp.mockImplementationOnce(() => Promise.resolve({
pkg: {
repository: {
type: 'git',
url: 'https://[email protected]/bitbucket-account/bitbucket-project.git',
},
},
}))

const result = await getRepoInfo()

expect(result).toEqual({
type: 'bitbucket',
domain: 'bitbucket.org',
user: 'bitbucket-account',
project: 'bitbucket-project',
url: 'https://bitbucket.org/bitbucket-account/bitbucket-project',
bugsUrl: undefined,
})
})
})

jest.mock('read-pkg-up')
jest.mock('git-remote-origin-url')
bpetetot marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/gitmoji-changelog-markdown/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"homepage": "https://github.com/frinyvonnick/gitmoji-changelog#readme",
"dependencies": {
"handlebars": "^4.0.12",
"immutadot": "^1.0.0",
"lodash": "^4.17.11"
}
}
37 changes: 36 additions & 1 deletion packages/gitmoji-changelog-markdown/src/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
const fs = require('fs')
const path = require('path')
const handlebars = require('handlebars')
const { update } = require('immutadot')
const { isEmpty } = require('lodash')

const MARKDOWN_TEMPLATE = path.join(__dirname, 'template.md')

function convert(changelog) {
function convert({ meta, changes }) {
const template = fs.readFileSync(MARKDOWN_TEMPLATE, 'utf-8')

const generateMarkdown = handlebars.compile(template)

const changelog = update(changes, '[:].groups[:].commits[:]', commit => ({
...commit,
hash: getShortHash(commit.hash, meta.repository),
subject: autolink(commit.subject, meta.repository),
message: autolink(commit.message, meta.repository),
body: autolink(commit.body, meta.repository),
}))
bpetetot marked this conversation as resolved.
Show resolved Hide resolved

return generateMarkdown({ changelog })
}

function getShortHash(hash, repository) {
if (!hash) return null

const shortHash = hash.slice(0, 7)

if (isEmpty(repository) || !repository.url) return shortHash

return `[${shortHash}](${repository.url}/commit/${hash})`
}

const ISSUE_REGEXP = /#{1}(\d+)/gm

function autolink(message, repository) {
if (!message) return null

if (isEmpty(repository) || !repository.bugsUrl) return message

const matches = message.match(ISSUE_REGEXP)
if (!matches) return message

return message.replace(ISSUE_REGEXP, `[#$1](${repository.bugsUrl}/$1)`)
}

module.exports = {
convert,
getShortHash,
autolink,
}
Loading