-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#1) Initial changes to commitlint-github-utils.
- Loading branch information
Showing
5 changed files
with
338 additions
and
146 deletions.
There are no files selected for viewing
This file contains 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,37 +1,38 @@ | ||
export interface Rules { | ||
taskIdEmpty: string | ||
taskIdSeparator: string | ||
taskIdCase: string | ||
taskIdMaxLength: string | ||
taskIdMinLength: string | ||
commitMessageSeparator: string | ||
issueNumberMissing: string; | ||
issueNumberFormat: string; | ||
typeOrWip: string; | ||
subjectCase: string; | ||
} | ||
|
||
export interface TCommitlintGitHubConstants { | ||
GITHUB_RULES: Rules | ||
COMMIT_MESSAGE_SEPARATOR: string | ||
COMMIT_TASK_IDS_SEPARATOR: string | ||
TASK_ID_SEPARATOR: string | ||
UPPERCASE: string | ||
LOWERCASE: string | ||
COMMIT_DESCRIPTION_SEPARATOR: string | ||
GITHUB_RULES: Rules; | ||
ISSUE_NUMBER_PREFIX: string; | ||
ISSUE_NUMBERS_SEPARATOR: string; | ||
ISSUE_NUMBERS_PATTERN: RegExp; | ||
TYPE_SEPARATOR: string; | ||
COMMIT_DESCRIPTION_SEPARATOR: string; | ||
} | ||
|
||
export type TParseCommitMessage = ( | ||
commitMessage: string, | ||
) => { | ||
commitTaskIds: string[] | ||
commitHeader: string | ||
commitFooter: string | ||
} | ||
export type ParsedCommitMessage = { | ||
issueNumbers: number[]; | ||
isWip: boolean; | ||
type?: string; | ||
subject?: string; | ||
body: string[]; | ||
}; | ||
|
||
export type CommitParser = ( | ||
unparsedCommitMessage: string, | ||
) => ParsedCommitMessage; | ||
|
||
export interface CommitlintGitHubUtils { | ||
parseCommitMessage: TParseCommitMessage | ||
commitlintGitHubConstants: TCommitlintGitHubConstants | ||
parseCommitMessage: CommitParser; | ||
commitlintGitHubConstants: TCommitlintGitHubConstants; | ||
} | ||
|
||
export const commitlintGitHubConstants: TCommitlintGitHubConstants | ||
export const parseCommitMessage: TParseCommitMessage | ||
export const commitlintGitHubConstants: TCommitlintGitHubConstants; | ||
export const parseCommitMessage: CommitParser; | ||
|
||
declare const commitlintGitHubUtils: CommitlintGitHubUtils | ||
export default commitlintGitHubUtils | ||
declare const commitlintGitHubUtils: CommitlintGitHubUtils; | ||
export default commitlintGitHubUtils; |
19 changes: 7 additions & 12 deletions
19
packages/commitlint-github-utils/src/commitlintGitHubConstants.ts
This file contains 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,17 +1,12 @@ | ||
export const GITHUB_RULES = { | ||
issueNumberMissing: 'github-issue-number-missing', | ||
issueNumberFormat: 'github-issue-number-format', | ||
issueNumberBrackets: 'github-issue-number-brackets', | ||
typeOrWip: 'github-type-or-wip', | ||
typeMessageSeparator: 'github-type-message-separator', | ||
} | ||
subjectCase: 'github-subject-case', | ||
}; | ||
|
||
export const ISSUE_NUMBER_PREFIX = '#' | ||
export const ISSUE_NUMBERS_SEPARATOR = ',' | ||
export const ISSUE_NUMBERS_PATTERN = /^\(#.+\) / | ||
export const ISSUE_NUMBERS_WITH_TYPE_PATTERN = /\(#.+\) \w+: / | ||
export const TYPE_SEPARATOR = ':' | ||
export const PARENTHESES = '()' | ||
export const SQUARE_BRACKETS = '[]' | ||
export const ANGLE_BRACKETS = '<>' | ||
export const COMMIT_DESCRIPTION_SEPARATOR = '\n' | ||
export const ISSUE_NUMBER_PREFIX = '#'; | ||
export const ISSUE_NUMBERS_SEPARATOR = ','; | ||
export const ISSUE_NUMBERS_PATTERN = /^\((?<issues>.+)\) (?:(?<type>\w+): )?(?<description>.*)/; | ||
export const TYPE_SEPARATOR = ':'; | ||
export const COMMIT_DESCRIPTION_SEPARATOR = '\n'; |
This file contains 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,11 +1,11 @@ | ||
import * as commitlintGitHubConstants from './commitlintGitHubConstants' | ||
import parseCommitMessage from './parseCommitMessage' | ||
import { CommitlintGitHubUtils } from '../@types' | ||
import * as commitlintGitHubConstants from './commitlintGitHubConstants'; | ||
import parseCommitMessage from './parseCommitMessage'; | ||
import { CommitlintGitHubUtils } from '../@types'; | ||
|
||
const commitlintGitHubUtils: CommitlintGitHubUtils = { | ||
commitlintGitHubConstants, | ||
parseCommitMessage, | ||
} | ||
}; | ||
|
||
export { commitlintGitHubConstants, parseCommitMessage } | ||
export default commitlintGitHubUtils | ||
export { commitlintGitHubConstants, parseCommitMessage }; | ||
export default commitlintGitHubUtils; |
97 changes: 48 additions & 49 deletions
97
packages/commitlint-github-utils/src/parseCommitMessage.ts
This file contains 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,54 +1,53 @@ | ||
import { TParseCommitMessage } from '../@types' | ||
import { CommitParser, ParsedCommitMessage } from '../@types'; | ||
import { | ||
COMMIT_MESSAGE_SEPARATOR, | ||
COMMIT_TASK_IDS_SEPARATOR, | ||
ISSUE_NUMBERS_PATTERN, | ||
COMMIT_DESCRIPTION_SEPARATOR, | ||
} from './commitlintGitHubConstants' | ||
|
||
const parseCommitMessage: TParseCommitMessage = rawCommitMessage => { | ||
/** | ||
* Description separator is used to separe commit parts without description | ||
* Read more about this issue: https://github.com/Gherciu/commitlint-github/issues/6 | ||
*/ | ||
const commitMessage = rawCommitMessage | ||
.split(COMMIT_DESCRIPTION_SEPARATOR) | ||
.filter(commitMessageSeparatedPart => commitMessageSeparatedPart)[0] | ||
const commitMessageParts = commitMessage.split(COMMIT_MESSAGE_SEPARATOR) | ||
|
||
/** | ||
* if commit parts length is greater or equal with 2 return part one else | ||
* commit parts length is less than 2 it means that task ids is not provided | ||
* or is not separated corectly | ||
*/ | ||
const rawCommitHeader = | ||
commitMessageParts.length >= 2 ? commitMessageParts[0] : '' | ||
const commitHeader = rawCommitHeader.trim() | ||
/** | ||
* if commit parts length is greater than 2 return all parts without first part | ||
* because first part is commit header | ||
* Note: rest of parts should be joined with COMMIT_MESSAGE_SEPARATOR | ||
* because is posible that the commit message footer to contain symbols equal | ||
* with COMMIT_MESSAGE_SEPARATOR then the commit footer will be resolved incorect | ||
* More info about this issue: https://github.com/Gherciu/commitlint-github/issues/7 | ||
*/ | ||
const commitFooter = | ||
commitMessageParts.length > 2 | ||
? commitMessageParts | ||
.filter((_value, index) => index > 0) | ||
.join(COMMIT_MESSAGE_SEPARATOR) | ||
.trim() | ||
: commitMessageParts[commitMessageParts.length - 1].trim() | ||
|
||
const commitTaskIds = commitHeader | ||
.split(COMMIT_TASK_IDS_SEPARATOR) | ||
.map(taskId => taskId.trim()) | ||
.filter(taskId => taskId) | ||
} from './commitlintGitHubConstants'; | ||
|
||
return { | ||
commitTaskIds, | ||
commitFooter, | ||
commitHeader, | ||
const parseIssues = (issuesString: string): number[] => { | ||
// TODO: Implement | ||
return [0]; | ||
}; | ||
|
||
const parseCommitMessage: CommitParser = ( | ||
rawCommitMessage: string, | ||
): ParsedCommitMessage => { | ||
let issueNumbers: number[] = []; | ||
let type: string | undefined; | ||
let isWip = false; | ||
let subject: string | undefined; | ||
let body: string[] = []; | ||
|
||
const issueNumbersWithPossibleType = ISSUE_NUMBERS_PATTERN.exec( | ||
rawCommitMessage, | ||
); | ||
|
||
const issueNumbersWithPossibleTypeGroups = | ||
issueNumbersWithPossibleType && issueNumbersWithPossibleType.groups; | ||
|
||
if (issueNumbersWithPossibleTypeGroups) { | ||
issueNumbers = parseIssues(issueNumbersWithPossibleTypeGroups.issue); | ||
|
||
// eslint-disable-next-line prefer-destructuring | ||
type = issueNumbersWithPossibleTypeGroups.type; | ||
isWip = type === 'WIP'; | ||
|
||
const descriptionLines = issueNumbersWithPossibleTypeGroups.description.split( | ||
COMMIT_DESCRIPTION_SEPARATOR, | ||
); | ||
|
||
const [description] = descriptionLines; | ||
[subject] = description; | ||
body = descriptionLines.slice(1); | ||
} | ||
} | ||
|
||
export default parseCommitMessage | ||
return { | ||
issueNumbers, | ||
isWip, | ||
type, | ||
subject, | ||
body, | ||
}; | ||
}; | ||
|
||
export default parseCommitMessage; |
Oops, something went wrong.