Skip to content

Commit

Permalink
skip validation when rebase, merge or cherry-pick (#13)
Browse files Browse the repository at this point in the history
* 馃攢 skip validation when rebase, merge or cherry-pick

* 馃攬 remove console.log
  • Loading branch information
thecarlo committed Jan 21, 2024
1 parent 1f0bb14 commit 956c4f6
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 7 deletions.
18 changes: 18 additions & 0 deletions src/functions/branch/isCherryPickInProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { promises as fs } from 'fs';
import { join } from 'path';

export const isCherryPickInProgress = async (): Promise<boolean> => {
try {
try {
await fs.access(join('.git', 'CHERRY_PICK_HEAD'));

return true;
} catch (error) {
return false;
}
} catch (error) {
console.error('isCherryPickInProgress error', error);

return false;
}
};
18 changes: 18 additions & 0 deletions src/functions/branch/isMergeInProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { promises as fs } from 'fs';
import { join } from 'path';

export const isMergeInProgress = async (): Promise<boolean> => {
try {
try {
await fs.access(join('.git', 'MERGE_HEAD'));

return true;
} catch (error) {
return false;
}
} catch (error) {
console.error('isMergeInProgress error', error);

return false;
}
};
32 changes: 32 additions & 0 deletions src/functions/branch/isRebaseInProgress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { promises as fs } from 'fs';
import { join } from 'path';

export const isRebaseInProgress = async (): Promise<boolean> => {
try {
const rebaseMergeExists = await fs
.access(join('.git', 'rebase-merge'))
.then(
() => {
console.log('rebase in progress...');

return true;
},
() => false,
);

const rebaseApplyExists = await fs
.access(join('.git', 'rebase-apply'))
.then(
() => {
return true;
},
() => false,
);

return rebaseMergeExists || rebaseApplyExists;
} catch (error) {
console.error('isRebaseInProgress error', error);

return false;
}
};
15 changes: 12 additions & 3 deletions src/functions/branch/validateBranch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,30 @@ import { Configuration } from '@interfaces/configuration';

import { createDefaultBranchTypes } from './createDefaultBranchTypes';
import { doBranchFullRegexCheck } from './doBranchFullRegexCheck';
import { isCherryPickInProgress } from './isCherryPickInProgress';
import { isMergeInProgress } from './isMergeInProgress';
import { isRebaseInProgress } from './isRebaseInProgress';
import { validateBranchJiraTicket } from './validateBranchJiraTicket';
import { validateBranchPrefix } from './validateBranchPrefix';

export const validateBranch = (
export const validateBranch = async (
branch: string,
configuration: Configuration,
): void => {
): Promise<void> => {
const { branchConfiguration, jiraTicketExample } = configuration;

const branchRegexSuccess = doBranchFullRegexCheck(
branch,
branchConfiguration,
);

if (branchRegexSuccess) {
const isRebase = await isRebaseInProgress();

const isMerge = await isMergeInProgress();

const isCherryPick = await isCherryPickInProgress();

if (branchRegexSuccess || isRebase || isMerge || isCherryPick) {
return;
}

Expand Down
8 changes: 6 additions & 2 deletions src/functions/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ export const main = async (): Promise<void> => {

const branch = await getCurrentBranchName();

validateBranch(branch, configuration);
await validateBranch(branch, configuration);

const message = await getCurrentCommitMessage();

const validateMessageResult = validateMessage(branch, message, configuration);
const validateMessageResult = await validateMessage(
branch,
message,
configuration,
);

const { success: validateMessageSuccess } = validateMessageResult;

Expand Down
19 changes: 17 additions & 2 deletions src/functions/message/validateMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import chalk from 'chalk';

import { getTicketFromBranch } from '@functions/branch/getTicketFromBranch';
import { isCherryPickInProgress } from '@functions/branch/isCherryPickInProgress';
import { isMergeInProgress } from '@functions/branch/isMergeInProgress';
import { isRebaseInProgress } from '@functions/branch/isRebaseInProgress';
import { getTicketFromMessage } from '@functions/message/getTicketFromMessage';
import { Configuration } from '@interfaces/configuration';
import { ValidateMessageResult } from '@interfaces/validateMessageResult';
Expand All @@ -9,13 +12,25 @@ import { validateCommitMessageWithJiraTicket } from './validateCommitMessageWith
import { validateJiraTicketFormatInMessage } from './validateJiraTicketFormatInMessage';
import { validateMessageLength } from './validateMessageLength';

export const validateMessage = (
export const validateMessage = async (
branch: string,
message: string,
configuration: Configuration,
): ValidateMessageResult => {
): Promise<ValidateMessageResult> => {
let hasValidationError = false;

const isRebase = await isRebaseInProgress();

const isMerge = await isMergeInProgress();

const isCherryPick = await isCherryPickInProgress();

if (isRebase || isMerge || isCherryPick) {
return {
success: true,
};
}

const ticketFromMessageResult = getTicketFromMessage(message, configuration);

const { messageConfiguration } = configuration;
Expand Down

0 comments on commit 956c4f6

Please sign in to comment.