Skip to content

Commit

Permalink
Merge pull request #49 from momocow/dev
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
momocow authored Dec 10, 2022
2 parents f98ef90 + 9c073b3 commit bd91068
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 13 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ module.exports = {
issueResolution: {
template: '{baseUrl}/{owner}/{repo}/issues/{ref}',
baseUrl: 'https://github.com',
source: 'github.com'
source: 'github.com',
removeFromCommit: false,
regex: /#\d+/g
}
}
}
Expand Down Expand Up @@ -152,11 +154,12 @@ Besides, You are allowed to provide helpers with the same names to override defa

`issueResolution` defines how issues are resolved to. The default and the only supported source currently is `github.com`, or you can provide your own `issueResolution.template` to override the default resolution to GitHub.

There are four variables that can be used in `issueResolution.template`:
There are five variables that can be used in `issueResolution.template`:
- `baseUrl`
- `owner`
- `repo`
- `ref`, which is the numeric ID of issue
- `issue`, which is the full issue

```ts
interface ReleaseNotesOptions {
Expand All @@ -167,6 +170,8 @@ interface ReleaseNotesOptions {
template?: string
baseUrl?: string
source?: 'github.com' | null // currently only GitHub is supported, PR welcome :)
regex?: RegExp, // regex to match the issue(s). If not provided, will find issues thanks to [issue-regex](https://www.npmjs.com/package/issue-regex)
removeFromCommit?: boolean // if true, will remove found issue(s) from commit name
}
}
```
Expand Down
2 changes: 2 additions & 0 deletions lib/assets/default-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ module.exports = {
// template
// baseUrl
// source
// removeFromCommit
// regex
}
}
}
25 changes: 16 additions & 9 deletions lib/helper/parse-commits.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function parseIssuesAndTasks ({ message = '' } = {}, options) {
const matched = message.match(/(?:^|\s)wip(#\w[\w-_]*)(?:$|\s)/)

// e.g. replace wip#1 to #1
const issues = (message.replace(/wip(#\d+)/g, '$1').match(issueRegex()) || [])
const issues = (message.replace(/wip(#\d+)/g, '$1').match(options.regex ?? issueRegex()) || [])
.map((issue) => ({
text: issue,
link: resolveIssueRef(issue, options)
Expand All @@ -24,8 +24,12 @@ function parseIssuesAndTasks ({ message = '' } = {}, options) {
}
}

function parseGitmoji ({ subject = '', message = '', body = '' } = {}) {
function parseGitmoji ({ subject = '', message = '', body = '' } = {}, issues = []) {
subject = emojify(subject.trim())
if (issues.length > 0) {
subject = issues.reduce((acc, curr) => acc.replace(curr.text, ''), subject).trim()
}

const matched = emojiRegex().exec(subject)
if (!matched || matched.index !== 0) return null

Expand All @@ -38,13 +42,16 @@ function parseGitmoji ({ subject = '', message = '', body = '' } = {}) {
module.exports = function parseCommits (commits = [], mixins = {}, options = {}) {
const taskMap = new Map()
return commits
.map(c => ({
...c,
...mixins,
...parseGitmoji(c),
...parseIssuesAndTasks(c, options.issues),
wip: []
}))
.map(c => {
const issues = parseIssuesAndTasks(c, options.issues)
return {
...c,
...mixins,
...parseGitmoji(c, options?.issues?.removeFromCommit ? issues.issues : []),
...issues,
wip: []
}
})
.reduce((acc, commit) => {
if (commit.gitmoji) {
if (!Array.isArray(acc[commit.gitmoji])) acc[commit.gitmoji] = []
Expand Down
6 changes: 4 additions & 2 deletions lib/helper/resolve-issue-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ module.exports = function (shorthand = '', {
repo,
baseUrl,
template,
regex,
source = 'github.com'
} = {}) {
const matched = shorthand.match(/(?:(\w[\w-.]+)\/(\w[\w-.]+)|\B)#([1-9]\d*)\b/)
const matched = shorthand.match(regex ?? /(?:(\w[\w-.]+)\/(\w[\w-.]+)|\B)#([1-9]\d*)\b/)
if (matched) {
const [, matchedOwner, matchedRepo, issueRef] = matched
const [issue, matchedOwner, matchedRepo, issueRef] = matched
return (template || getIssueUrlTemplate(source))
.replace('{baseUrl}', baseUrl || '')
.replace('{owner}', matchedOwner || owner || '')
.replace('{repo}', matchedRepo || repo || '')
.replace('{ref}', issueRef || '')
.replace('{issue}', issue)
}
return shorthand
}
47 changes: 47 additions & 0 deletions test/integration/fixtures/contexts/context-custom.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"options": {
"repositoryUrl": "git+https://github.com/momocow/semantic-release-gitmoji.git"
},
"lastRelease": {
"version": "0.0.0",
"gitHead": "gitHead",
"gitTag": "v0.0.0"
},
"nextRelease": {
"gitHead": "gitHead"
},
"commits": [
{
"commit": {
"short": "123456"
},
"message": "CUSTOM-4242 :sparkles: (scope1): use custom ticket regex",
"subject": "CUSTOM-4242 :sparkles: (scope1): use custom ticket regex",
"body": ""
},
{
"commit": {
"short": "654321"
},
"message": ":sparkles: (scope2): no ticket",
"subject": ":sparkles: (scope2): no ticket",
"body": ""
},
{
"commit": {
"short": "654456"
},
"message": ":bug: (scope3): with github issue #42",
"subject": ":bug: (scope3): with github issue #42",
"body": ""
},
{
"commit": {
"short": "123321"
},
"message": "CUSTOM-4242 CUSTOM-4243 :sparkles: (scope4): multiple issues",
"subject": "CUSTOM-4242 CUSTOM-4243 :sparkles: (scope4): multiple issues",
"body": ""
}
]
}
10 changes: 10 additions & 0 deletions test/integration/fixtures/notes/notes-custom.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# [v0.1.0](https://github.com/momocow/semantic-release-gitmoji/compare/v0.0.0...v0.1.0) ({datetime})

## ✨ New Features
- [`123321`](https://github.com/momocow/semantic-release-gitmoji/commit/123321) (scope4): multiple issues (Issues: [`CUSTOM-4242`](https://custom-url/CUSTOM-4242) [`CUSTOM-4243`](https://custom-url/CUSTOM-4243))
- [`654321`](https://github.com/momocow/semantic-release-gitmoji/commit/654321) (scope2): no ticket
- [`123456`](https://github.com/momocow/semantic-release-gitmoji/commit/123456) (scope1): use custom ticket regex (Issues: [`CUSTOM-4242`](https://custom-url/CUSTOM-4242))

## 🐛 Bug Fixes
- [`654456`](https://github.com/momocow/semantic-release-gitmoji/commit/654456) (scope3): with github issue #42

19 changes: 19 additions & 0 deletions test/integration/generate-notes.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ const CASES = [
}
}),
expectedNotes: readNotesSync('custom-helper')
},
{
name: 'default config + custom issueResolution',
pluginConfig: {
releaseNotes: {
issueResolution: {
removeFromCommit: true,
regex: /CUSTOM-\d{4}/g,
template: 'https://custom-url/{issue}'
}
}
},
context: getContext('custom', {
nextRelease: {
version: '0.1.0',
gitTag: 'v0.1.0'
}
}),
expectedNotes: readNotesSync('custom')
}
]

Expand Down

0 comments on commit bd91068

Please sign in to comment.