-
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.
Merge pull request #16 from beuluis/feature/add-more-hooks
Feature/add more hooks
- Loading branch information
Showing
8 changed files
with
505 additions
and
16 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
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import Listr from 'listr'; | ||
import { HookFailedError, registerCommandModule } from '../util/commandModule.helper'; | ||
import { execute, ExecuteError } from '../util/exec.helper'; | ||
|
||
export = registerCommandModule<{ commitMsgPath: string }>()({ | ||
command: 'checkCommitMessageIssueKey [commitMsgPath]', | ||
describe: 'Check the commit message for a issue key', | ||
builder: { | ||
'no-fail': { | ||
alias: 'n', | ||
type: 'boolean', | ||
description: 'If true only prints warning messages and do not exit with not zero code', | ||
default: false, | ||
}, | ||
message: { | ||
alias: 'm', | ||
type: 'string', | ||
description: 'Get message from command line instead of file', | ||
}, | ||
prefix: { | ||
alias: 'p', | ||
type: 'string', | ||
description: 'Prefix of the issue key', | ||
}, | ||
}, | ||
handler: async argv => { | ||
const { 'no-fail': noFail, prefix, message, commitMsgPath } = argv; | ||
const tasks = new Listr([ | ||
{ | ||
title: 'Check if commit message contains issue key', | ||
task: async (_context, task) => { | ||
if (!commitMsgPath && !message) { | ||
task.title = 'No commit message provided'; | ||
throw new HookFailedError(); | ||
} | ||
|
||
// eslint-disable-next-line canonical/id-match | ||
const command = __filename.endsWith('.ts') | ||
? 'npx ts-node src/index.ts checkCommitMessagePattern' | ||
: 'npx hook-cli checkCommitMessagePattern'; | ||
|
||
const messageArguments = message ? `-m "${message}"` : commitMsgPath; | ||
|
||
const ticketRegex = `^(${prefix ? prefix : '[A-Z]+'}-[0-9]{1,10})( ?/ ?(${ | ||
prefix ? prefix : '[A-Z]+' | ||
}-[0-9]{1,10}))*? (?!/).*`; | ||
|
||
await execute(`${command} ${messageArguments} -p "${ticketRegex}"`) | ||
.then(() => (task.title = 'Issue key found')) | ||
.catch(async (error: unknown) => { | ||
if (error instanceof ExecuteError) { | ||
task.title = 'Issue key not found'; | ||
throw new HookFailedError(); | ||
} else { | ||
// eslint-disable-next-line unicorn/prefer-type-error | ||
throw new Error('Unknown error'); | ||
} | ||
}); | ||
}, | ||
}, | ||
]); | ||
|
||
try { | ||
await tasks.run(); | ||
process.exit(0); | ||
} catch (error) { | ||
if (error instanceof HookFailedError && noFail) { | ||
process.exit(0); | ||
} | ||
|
||
process.exit(1); | ||
} | ||
}, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import fs from 'fs'; | ||
import Listr from 'listr'; | ||
import { HookFailedError, registerCommandModule } from '../util/commandModule.helper'; | ||
|
||
const ignoreWildcards: RegExp[] = [ | ||
// eslint-disable-next-line unicorn/no-unsafe-regex | ||
/^((Merge pull request)|(Merge (.*?) into (.*)|(Merge branch (.*)))(?:\r?\n)*$)/mu, | ||
// eslint-disable-next-line unicorn/no-unsafe-regex | ||
/^(Merge tag (.*))(?:\r?\n)*$/mu, | ||
/^(R|r)evert (.*)/u, | ||
/^(fixup|squash)!/u, | ||
/^(Merged (.*?)(in|into) (.*)|Merged PR (.*): (.*))/u, | ||
/^Merge remote-tracking branch(\s*)(.*)/u, | ||
/^Automatic merge(.*)/u, | ||
/^Auto-merged (.*?) into (.*)/u, | ||
]; | ||
|
||
export = registerCommandModule<{ commitMsgPath: string }>()({ | ||
command: 'checkCommitMessagePattern [commitMsgPath]', | ||
describe: 'Check the pattern of a commit message', | ||
builder: { | ||
'no-fail': { | ||
alias: 'n', | ||
type: 'boolean', | ||
description: 'If true only prints warning messages and do not exit with not zero code', | ||
default: false, | ||
}, | ||
message: { | ||
alias: 'm', | ||
type: 'string', | ||
description: 'Get message from command line instead of file', | ||
}, | ||
pattern: { | ||
alias: 'p', | ||
type: 'string', | ||
description: 'Regex pattern to check the message against', | ||
}, | ||
}, | ||
handler: async argv => { | ||
const { 'no-fail': noFail, message, pattern, commitMsgPath } = argv; | ||
const tasks = new Listr([ | ||
{ | ||
title: 'Check if commit message matches pattern', | ||
task: async (_context, task) => { | ||
if (!commitMsgPath && !message) { | ||
task.title = 'No commit message provided'; | ||
throw new HookFailedError(); | ||
} | ||
|
||
if (!pattern) { | ||
task.title = 'No pattern provided'; | ||
throw new HookFailedError(); | ||
} | ||
|
||
const finalMessage = message ?? fs.readFileSync(commitMsgPath, 'utf8'); | ||
|
||
for (const ignorePattern of ignoreWildcards) { | ||
if (ignorePattern.test(finalMessage)) { | ||
task.title = 'Commit message matches ignore pattern'; | ||
|
||
process.exit(0); | ||
} | ||
} | ||
|
||
if (!new RegExp(pattern, 'u').test(finalMessage)) { | ||
task.title = 'Commit message does not match pattern'; | ||
throw new HookFailedError(); | ||
} | ||
|
||
task.title = 'Commit message matches pattern'; | ||
}, | ||
}, | ||
]); | ||
|
||
try { | ||
await tasks.run(); | ||
process.exit(0); | ||
} catch (error) { | ||
if (error instanceof HookFailedError && noFail) { | ||
process.exit(0); | ||
} | ||
|
||
process.exit(1); | ||
} | ||
}, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { color } from 'console-log-colors'; | ||
import Listr from 'listr'; | ||
import { HookFailedError, registerCommandModule } from '../util/commandModule.helper'; | ||
import { execute, ExecuteError } from '../util/exec.helper'; | ||
|
||
export = registerCommandModule<{ filePath: string }>()({ | ||
command: 'checkForFileChanged [filePath]', | ||
describe: | ||
'Check if a staged file like a changelog was changed locale or remote compared to another branch', | ||
builder: { | ||
'no-fail': { | ||
alias: 'n', | ||
type: 'boolean', | ||
description: 'If true only prints warning messages and do not exit with not zero code', | ||
default: false, | ||
}, | ||
branch: { | ||
alias: 'b', | ||
type: 'string', | ||
description: 'Branch to compare to', | ||
default: 'main', | ||
}, | ||
}, | ||
handler: async argv => { | ||
const { 'no-fail': noFail, branch, filePath } = argv; | ||
const tasks = new Listr([ | ||
{ | ||
title: `Check if ${color.cyan(filePath)} differs from ${color.cyan(branch)}`, | ||
task: async (_context, task) => | ||
await execute(`! git diff origin/${branch} --cached --exit-code -- ${filePath}`) | ||
.then( | ||
() => | ||
(task.title = `${color.green( | ||
filePath, | ||
)} differs from the version on ${color.green(branch)}`), | ||
) | ||
.catch(async (error: unknown) => { | ||
if (error instanceof ExecuteError) { | ||
task.title = `${color.red(filePath)} do not differ from ${color.red( | ||
branch, | ||
)}`; | ||
throw new HookFailedError(); | ||
} else { | ||
// TODO: recheck if we dont have a message for such cases | ||
// eslint-disable-next-line unicorn/prefer-type-error | ||
throw new Error('Unknown error'); | ||
} | ||
}), | ||
}, | ||
]); | ||
|
||
try { | ||
await tasks.run(); | ||
process.exit(0); | ||
} catch (error) { | ||
if (error instanceof HookFailedError && noFail) { | ||
process.exit(0); | ||
} | ||
|
||
process.exit(1); | ||
} | ||
}, | ||
}); |
Oops, something went wrong.