Skip to content

Commit

Permalink
(#1) Adding support for returning the unparsed issueNumbers to be abl…
Browse files Browse the repository at this point in the history
…e to distinguish missing vs invalid issue numbers.
  • Loading branch information
danrivett committed Feb 22, 2020
1 parent de1def4 commit 1e2bbf7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/commitlint-github-utils/@types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export type ParserOptions = {

export type ParsedCommitMessage = {
issueNumbers: number[];
rawIssueNumbers?: string;
isWip: boolean;
type?: string;
subject?: string;
Expand Down
5 changes: 4 additions & 1 deletion packages/commitlint-github-utils/src/parseCommitMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ const parseDescription = (groups: { [key: string]: string }): [string, string[]]

const parseCommitMessage: CommitParser = (rawCommitMessage: string): ParsedCommitMessage => {
let issueNumbers: number[] = [];
let rawIssueNumbers: string | undefined;
let type: string | undefined;
let isWip = false;
let subject: string | undefined;
let body: string[] = [];

const issueNumbersWithPossibleTypeGroups = parseCommit(rawCommitMessage, ISSUE_NUMBERS_PATTERN);
if (issueNumbersWithPossibleTypeGroups) {
issueNumbers = parseIssues(issueNumbersWithPossibleTypeGroups.issue);
rawIssueNumbers = issueNumbersWithPossibleTypeGroups.issue;
issueNumbers = parseIssues(rawIssueNumbers);

// eslint-disable-next-line prefer-destructuring
type = issueNumbersWithPossibleTypeGroups.type;
Expand All @@ -53,6 +55,7 @@ const parseCommitMessage: CommitParser = (rawCommitMessage: string): ParsedCommi

return {
issueNumbers,
rawIssueNumbers,
isWip,
type,
subject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,65 @@ describe('commitlintPluginGitHubTests', () => {
expect(
parseCommitMessage(`(#123,#45) WIP: ${COMMIT_MESSAGE}`).issueNumbers
).toEqual([123, 45]);

// Other issue numbers referenced later should not be included
expect(
parseCommitMessage(`(#123,#45) ${COMMIT_MESSAGE} (#22)`).issueNumbers
).toEqual([123, 45]);

expect(
parseCommitMessage(`(#123,#45) WIP: ${COMMIT_MESSAGE} (#22)`).issueNumbers
).toEqual([123, 45]);

// Issue Numbers string should be trimmed
expect(
parseCommitMessage(`( #123,#45 ) WIP: ${COMMIT_MESSAGE} (#22)`).issueNumbers
).toEqual([123, 45]);
});

it('should return raw issue numbers', () => {
// Non-numeric issue numbers prefix
expect(
parseCommitMessage(`(#2bob) ${COMMIT_MESSAGE}`).rawIssueNumbers
).toEqual('#2bob');

expect(
parseCommitMessage(`(#2, #1bob) ${COMMIT_MESSAGE}`).rawIssueNumbers
).toEqual('#2, #1bob');

expect(
parseCommitMessage(`(#2,#1bob) ${COMMIT_MESSAGE}`).rawIssueNumbers
).toEqual('#2,#1bob');

expect(
parseCommitMessage(`(#123,#45) WIP: ${COMMIT_MESSAGE} (#22)`).rawIssueNumbers
).toEqual('#123,#45');

// Issue Numbers string should be trimmed
expect(
parseCommitMessage(`( #123,#45 ) ${COMMIT_MESSAGE}`).rawIssueNumbers
).toEqual('#123,#45');
});

it('should return no raw issue numbers', () => {
expect(
parseCommitMessage('My commit message').rawIssueNumbers
).toEqual(null);

// Issue numbers not at the beginning
expect(
parseCommitMessage('My commit message (#1)').rawIssueNumbers
).toEqual(null);

// Empty brackets should return an empty String (not null)
expect(
parseCommitMessage(`() ${COMMIT_MESSAGE}`).rawIssueNumbers
).toEqual('');

// Raw Issue Numbers string should be trimmed
expect(
parseCommitMessage(`( ) ${COMMIT_MESSAGE}`).rawIssueNumbers
).toEqual('');
});

it('should return no issue numbers', () => {
Expand All @@ -70,11 +129,20 @@ describe('commitlintPluginGitHubTests', () => {
parseCommitMessage('My commit message').issueNumbers
).toEqual([]);

// Empty issue numbers prefix
// Issue numbers not at the beginning
expect(
parseCommitMessage(`() ${COMMIT_MESSAGE}`).issueNumbers
).toEqual([]);

expect(
parseCommitMessage(`WIP: ${COMMIT_MESSAGE} (#22)`).issueNumbers
).toEqual([]);

// Empty issue numbers prefix
expect(
parseCommitMessage(`${COMMIT_MESSAGE} (#1)`).issueNumbers
).toEqual([]);

// WIP commit without issue numbers
expect(
parseCommitMessage(`WIP: ${COMMIT_MESSAGE}`).issueNumbers
Expand Down

0 comments on commit 1e2bbf7

Please sign in to comment.