-
Notifications
You must be signed in to change notification settings - Fork 0
WIP #7 common: removed managing with process, refactored validators #8
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
Open
derlagg
wants to merge
3
commits into
dev
Choose a base branch
from
bug/7-check-is-not-case-sensitive
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| global.console.log = jest.fn(); | ||
| global.console.info = jest.fn(); | ||
| global.console.warn = jest.fn(); | ||
| global.console.error = jest.fn(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,42 +1,32 @@ | ||
| import { Config } from '../types'; | ||
| import { TokenDictionary } from './core.interface'; | ||
| import { exitWithSuccess, getCommitMessage, getConfig } from '../lib'; | ||
| import { StatusMessage } from '../common/const'; | ||
| import { checkForBodies, checkForIgnoreMatching, checkForIssuePrefix } from '../linter'; | ||
| import { getCommitMessage, getConfig } from '../lib'; | ||
| import { checkForBodies, checkForIssuePrefix, isMessageShouldBeIgnored } from '../linter'; | ||
| import { getTokensFrom } from '../token-extractor'; | ||
|
|
||
| /** | ||
| * A main function for commit linting. If there are no any errors exit process with 0, else with 1. | ||
| * A main function for commit linting. | ||
| */ | ||
| export function validate(): void { | ||
| const config = getConfig(); | ||
| const commitMessage = getCommitMessage(); | ||
| const tokens = getTokensFrom(commitMessage, config); | ||
|
|
||
| checkForIgnoreMatching(config, tokens); | ||
| checkForIssuePrefix(config, tokens); | ||
| checkForBodies(config, tokens); | ||
| const isIgnored = isMessageShouldBeIgnored(config, tokens); | ||
|
|
||
| exitWithSuccess(StatusMessage.VALID); | ||
| } | ||
|
|
||
| /** | ||
| * Parse tokens from commit message. | ||
| * | COREUI-220123 common: added the ability to parse library; card: added user | | ||
| * | <whole string > | | ||
| * | <issue prefix> <bodies > | | ||
| * | <body 1 ><body 2 > | | ||
| * | ||
| * @param message | ||
| * @param config | ||
| */ | ||
| function getTokensFrom(message: string, config: Config): TokenDictionary { | ||
| const wholeString = message; | ||
| if (isIgnored) { | ||
| return; | ||
| } | ||
|
|
||
| const hasIssuePrefixes = config.issuePrefixes && !config.issuePrefixes.includes('.*'); | ||
| const [issuePrefix, ...rest] = hasIssuePrefixes ? message.split(' ') : ['', message]; | ||
| const bodies = rest.join(' ') | ||
| .split(';') | ||
| .map(body => body.trim()); | ||
| const checkers = [checkForIssuePrefix, checkForBodies]; | ||
|
|
||
| return { wholeString, issuePrefix, bodies }; | ||
| checkers.forEach(checker => { | ||
| const error = checker(config, tokens); | ||
| if (error) { | ||
| throw new Error(error); | ||
| } | ||
| }); | ||
| const error = checkers[i](config, tokens); | ||
| if (error) { | ||
| throw new Error(error); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import { checkForIssuePrefix } from './linter'; | ||
|
|
||
| describe('Linter', () => { | ||
|
|
||
| describe('#checkForIssuePrefix', () => { | ||
|
|
||
| it('should pass validating prefix', () => { | ||
| const config = { issuePrefixes: ['SC-[0-9]+'] }; | ||
| const result = checkForIssuePrefix(config, { issuePrefix: 'SC-123' }); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('should return error for a prefix in another case', () => { | ||
| const config = { issuePrefixes: ['SC-[0-9]+'] }; | ||
|
|
||
| const result = checkForIssuePrefix(config, { issuePrefix: 'sc-123' }); | ||
|
|
||
| expect(result).toBeDefined(); | ||
| expect(result).not.toBeNull(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,50 @@ | ||
| import { Config } from '../types'; | ||
| import { TokenDictionary } from '../core/core.interface'; | ||
| import { exitWithError, exitWithSuccess } from '../lib'; | ||
| import { ErrorMessage, StatusMessage, StringValue } from '../common/const'; | ||
| import { ErrorMessage, StringValue } from '../common/const'; | ||
|
|
||
| type ValidationError = string; | ||
| type ValidationResult = ValidationError | null; | ||
|
|
||
| /** | ||
| * Exit from process with code 0 if commit message matches with some of ignore patterns. | ||
| * Return true if message should be ignored. | ||
| * | ||
| * For instance, if there is a ignore pattern '^Merge .*' commit with message "Merge to dev" will | ||
| * be accepted. | ||
| * | ||
| * @param config | ||
| * @param wholeString | ||
| */ | ||
| export function checkForIgnoreMatching(config: Config, { wholeString = '' }: TokenDictionary): void { | ||
| if ((config.ignore || []).some((rule: string) => wholeString.match(rule))) { | ||
| exitWithSuccess(StatusMessage.VALID); | ||
| } | ||
| export function isMessageShouldBeIgnored(config: Config, { wholeString = '' }: TokenDictionary): boolean { | ||
| return (config.ignore || []).some((rule: string) => wholeString.match(rule)); | ||
| } | ||
|
|
||
| /** | ||
| * Exit from process with error code if issue prefix doesn't match any of patterns. | ||
| * Return error message if checking was failed. | ||
| * | ||
| * @param config | ||
| * @param issuePrefix | ||
| */ | ||
| export function checkForIssuePrefix(config: Config, { issuePrefix = '' }: TokenDictionary): void { | ||
| export function checkForIssuePrefix(config: Config, { issuePrefix = '' }: TokenDictionary): ValidationResult { | ||
| if ((config.issuePrefixes || []).some(value => issuePrefix.match(value))) { | ||
| return; | ||
| return null; | ||
| } | ||
|
|
||
| exitWithError( | ||
| ErrorMessage.ISSUE_PREFIX_ERROR.replace(StringValue.ISSUE_PREFIX, issuePrefix) | ||
| .replace(StringValue.DOC_LINK, config.linkToRule as string) | ||
| ); | ||
| return ErrorMessage.ISSUE_PREFIX_ERROR.replace(StringValue.ISSUE_PREFIX, issuePrefix) | ||
| .replace(StringValue.DOC_LINK, config.linkToRule as string); | ||
| } | ||
|
|
||
| /** | ||
| * Exit from process with error code if at least one of bodies doesn't match rule. | ||
| * Also prints in console list of not matched bodies. | ||
| * Return error message if checking was failed. | ||
| * | ||
| * @param config | ||
| * @param bodies | ||
| */ | ||
| export function checkForBodies(config: Config, { bodies = [] }: TokenDictionary): void { | ||
| export function checkForBodies(config: Config, { bodies = [] }: TokenDictionary): ValidationResult { | ||
| if (bodies.every(body => body.match(config.body as RegExp))) { | ||
| return; | ||
| return null; | ||
| } | ||
|
|
||
| const notMatchedBodies = bodies.filter(body => !body.match(config.body as RegExp)); | ||
| exitWithError( | ||
| ErrorMessage.BODIES_ERROR.replace(StringValue.BODIES, notMatchedBodies.join('", "')) | ||
| .replace(StringValue.DOC_LINK, config.linkToRule as string) | ||
| ); | ||
| return ErrorMessage.BODIES_ERROR.replace(StringValue.BODIES, notMatchedBodies.join('", "')) | ||
| .replace(StringValue.DOC_LINK, config.linkToRule as string); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './token-extractor'; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have doubts about it. The more expected behavior is returning true or false. True in success cases. In a more classic way (as I observed), the function returns [result, error]. But these functions are for network interaction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are not right if you speak that returning boolean is more classic. Do you have any proofs? Otherwise, you can see any validation lib.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anyway it depends on your wishes. If you wanna return some specific validation error, it's a good solution. Otherwise, if you are enough boolean, you can change it to boolean.