Skip to content

Commit 989d86e

Browse files
feat: Add newest_only commit validation setting (#649)
* feat: Add newest_only commit validation setting * chore: update changelog Co-authored-by: Shine Lee <aungshine@gmail.com>
1 parent 10ba0b2 commit 989d86e

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

__tests__/unit/validators/commit.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,44 @@ test('oldest_only sub option', async () => {
7676
expect(validation.status).toBe('pass')
7777
})
7878

79+
test('newest_only sub option', async () => {
80+
const commit = new Commit()
81+
const settings = {
82+
do: 'commit',
83+
message: {
84+
regex: 'feat:',
85+
newest_only: true
86+
}
87+
}
88+
const date = Date.now()
89+
const commits = [
90+
{
91+
commit: {
92+
author: {
93+
date
94+
},
95+
message: 'fix: that'
96+
}
97+
},
98+
{
99+
commit: {
100+
author: {
101+
date: date + 1
102+
},
103+
message: 'fix: this'
104+
}
105+
}
106+
]
107+
108+
let validation = await commit.processValidate(createMockContext(commits), settings)
109+
expect(validation.status).toBe('fail')
110+
111+
commits[1].commit.message = 'feat: this'
112+
113+
validation = await commit.processValidate(createMockContext(commits), settings)
114+
expect(validation.status).toBe('pass')
115+
})
116+
79117
test('skip_merge sub option', async () => {
80118
const commit = new Commit()
81119
const settings = {

docs/changelog.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
CHANGELOG
22
=====================================
3-
| August 20, 2022: fix: supported events for `request_review` action `#623 <https://github.com/mergeability/mergeable/pull/651>`
3+
| August 20, 2022: fix: supported events for `request_review` action `#623 <https://github.com/mergeability/mergeable/pull/651>`_
4+
| August 2, 2022: feat: Add newest_only commit validation setting `#649 <https://github.com/mergeability/mergeable/pull/649>`_
45
| April 7, 2022: feat: Support adding deployment labels from values `#631 <https://github.com/mergeability/mergeable/pull/631>`_
56
| March 22, 2022: fix: pass comment instance to removeErrorComments `#626 <https://github.com/mergeability/mergeable/pull/626>`_
67
| February 24, 2022: fix: correct indentation on documentation `#623 <https://github.com/mergeability/mergeable/pull/623>`_

docs/validators/commit.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Commit
99
message: 'Custom message' # Semantic release conventions must be followed
1010
skip_merge: true # Optional, Default is true. Will skip commit with message that includes 'Merge'
1111
oldest_only: false # Optional, Default is false. Only check the regex against the oldest commit
12+
newest_only: false # Optional, Default is false. Only check the regex against the newest commit
1213
single_commit_only: false # Optional, Default is false. only process this validator if there is one commit
1314
jira:
1415
regex: '[A-Z][A-Z0-9]+-\d+'

lib/validators/commit.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Commit extends Validator {
2828
message: 'string',
2929
skip_merge: 'boolean',
3030
oldest_only: 'boolean',
31+
newest_only: 'boolean',
3132
single_commit_only: 'boolean'
3233
}
3334
}
@@ -39,6 +40,7 @@ class Commit extends Validator {
3940

4041
const messageSettings = validationSettings.message
4142
const oldestCommitOnly = _.isUndefined(messageSettings.oldest_only) ? false : messageSettings.oldest_only
43+
const newestCommitOnly = _.isUndefined(messageSettings.newest_only) ? false : messageSettings.newest_only
4244
const skipMerge = _.isUndefined(messageSettings.skip_merge) ? true : messageSettings.skip_merge
4345
const singleCommitOnly = _.isUndefined(messageSettings.single_commit_only) ? false : messageSettings.single_commit_only
4446

@@ -67,6 +69,10 @@ class Commit extends Validator {
6769
orderedCommits = [orderedCommits[0]]
6870
}
6971

72+
if (newestCommitOnly) {
73+
orderedCommits = [orderedCommits[orderedCommits.length - 1]]
74+
}
75+
7076
const commitMessages = orderedCommits.map(commit => commit.message)
7177

7278
const result = await mustInclude.process(validatorContext, commitMessages, {

0 commit comments

Comments
 (0)