Skip to content

Commit

Permalink
(#1) Initial changes to commitlint-github-utils.
Browse files Browse the repository at this point in the history
  • Loading branch information
danrivett committed Feb 22, 2020
1 parent 23c7053 commit 0262ca6
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 146 deletions.
53 changes: 27 additions & 26 deletions packages/commitlint-github-utils/@types/index.d.ts
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 packages/commitlint-github-utils/src/commitlintGitHubConstants.ts
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';
12 changes: 6 additions & 6 deletions packages/commitlint-github-utils/src/index.ts
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 packages/commitlint-github-utils/src/parseCommitMessage.ts
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;
Loading

0 comments on commit 0262ca6

Please sign in to comment.