Skip to content

feat(validator): add body length parameter #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ Then run `pre-commit install --hook-type commit-msg` to install the
- if `no-revert-sha1` is set, no validation is done on revert commits.
- if `--jira-in-header` jira reference can be put in the commit header.
- `--header-length` allow to override the max length of the header line.
- `--body-length` allow to override the max length of body lines.
- `--jira-types` takes a space separated list `"feat fix"` as a parameter to override the default types requiring a jira

<!-- ROADMAP -->
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ inputs:
header_length:
description: 'If not empty, max header_length'
required: false
body_length:
description: 'If not empty, max body_length'
required: false
jira_types:
description: 'If not empty, space separated list of types that require Jira refs'
required: false
Expand Down Expand Up @@ -46,5 +49,6 @@ runs:
COMMIT_VALIDATOR_NO_REVERT_SHA1: ${{ inputs.no_revert_sha1 }}
GLOBAL_JIRA_TYPES: ${{ inputs.jira_types }}
GLOBAL_MAX_LENGTH: ${{ inputs.header_length }}
GLOBAL_BODY_MAX_LENGTH: ${{ inputs.body_length }}
GLOBAL_JIRA_IN_HEADER: ${{ inputs.jira_in_header }}
shell: bash
5 changes: 3 additions & 2 deletions check_message.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

set -eu

OPTIONS=$(getopt --longoptions no-jira,allow-temp,jira-in-header,header-length:,jira-types: --options "" -- "$@")
unset COMMIT_VALIDATOR_ALLOW_TEMP COMMIT_VALIDATOR_NO_JIRA COMMIT_VALIDATOR_NO_REVERT_SHA1 GLOBAL_JIRA_IN_HEADER GLOBAL_MAX_LENGTH GLOBAL_JIRA_TYPES
OPTIONS=$(getopt --longoptions no-jira,allow-temp,jira-in-header,header-length,body-length:,jira-types: --options "" -- "$@")
unset COMMIT_VALIDATOR_ALLOW_TEMP COMMIT_VALIDATOR_NO_JIRA COMMIT_VALIDATOR_NO_REVERT_SHA1 GLOBAL_JIRA_IN_HEADER GLOBAL_MAX_LENGTH GLOBAL_BODY_MAX_LENGTH GLOBAL_JIRA_TYPES

eval set -- $OPTIONS
while true; do
Expand All @@ -13,6 +13,7 @@ while true; do
--no-revert-sha1 ) COMMIT_VALIDATOR_NO_REVERT_SHA1=1; shift ;;
--jira-in-header ) GLOBAL_JIRA_IN_HEADER=1; shift ;;
--header-length ) GLOBAL_MAX_LENGTH="$2"; shift 2 ;;
--body-length ) GLOBAL_BODY_MAX_LENGTH="$2"; shift 2 ;;
--jira-types ) GLOBAL_JIRA_TYPES="$2"; shift 2 ;;
-- ) shift; break ;;
* ) break ;;
Expand Down
4 changes: 4 additions & 0 deletions validator.bats
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,10 @@ LUM-2345'
[[ "$status" -eq 0 ]]
}

@test "body length cannot be more than 150 with spaces. overridden" {
GLOBAL_BODY_MAX_LENGTH=150 validate_body_length "012345678 012345678 012345678 012345678 012345678 012345678 012345678 1"
}

@test "body with trailing space on line should not be valid" {
MESSAGE='pdzofjzf '

Expand Down
5 changes: 3 additions & 2 deletions validator.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ GLOBAL_FOOTER=""
# Overridable variables
GLOBAL_JIRA_TYPES="${GLOBAL_JIRA_TYPES:-feat fix}"
GLOBAL_MAX_LENGTH="${GLOBAL_MAX_LENGTH:-100}"
GLOBAL_BODY_MAX_LENGTH="${GLOBAL_BODY_MAX_LENGTH:-100}"
GLOBAL_JIRA_IN_HEADER="${GLOBAL_JIRA_IN_HEADER:-}"

GLOBAL_TYPE=""
Expand Down Expand Up @@ -197,8 +198,8 @@ validate_body_length() {

LENGTH="$(echo -n "$LINE" | wc -c)"

if [[ $LENGTH -gt 100 ]]; then
echo -e "body message line length is more than 100 charaters"
if [[ $LENGTH -gt ${GLOBAL_BODY_MAX_LENGTH} ]]; then
echo -e "body message line length is more than ${GLOBAL_BODY_MAX_LENGTH} characters"
exit $ERROR_BODY_LENGTH
fi
done <<< "$BODY"
Expand Down
Loading