Skip to content

Commit

Permalink
ci(i): Respect ignore decorator on PR title validation (#1538)
Browse files Browse the repository at this point in the history
## Relevant issue(s)
Resolves #1176

## Description
- Any previous valid label can now also support an additional decorator
like so: `(i)` that will help signal that this commit/pr-title can be
ignored from change-log.
  • Loading branch information
shahzadlone authored May 29, 2023
1 parent 3e212bc commit c056713
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
22 changes: 22 additions & 0 deletions tools/scripts/scripts_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,33 @@ TestReturnCode() {

# Test the script that is responsible for the validation of pr title.
readonly T1="./validate-conventional-style.sh"

TestReturnCode "${T1}" 2;

TestReturnCode "${T1} 'chore: This title has everything valid except that its too long'" 3;
TestReturnCode "${T1} 'bot Bump github.com/alternativesourcenetwork/defradb from 1.1.0.1.0.0 to 1.1.0.1.0.1'" 3;

TestReturnCode "${T1} 'chore: This title has more than one : colon'" 4;
TestReturnCode "${T1} 'chore This title has no colon'" 4;
TestReturnCode "${T1} 'bot Bump github.com/short/short from 1.2.3 to 1.2.4'" 4;

TestReturnCode "${T1} 'feat: a'" 5;
TestReturnCode "${T1} 'feat: '" 5;
TestReturnCode "${T1} 'feat:'" 5;

TestReturnCode "${T1} 'feat:There is no space between label & desc.'" 6;
TestReturnCode "${T1} 'feat:there is no space between label & desc.'" 6;

TestReturnCode "${T1} 'ci: lowercase first character after label'" 7;

TestReturnCode "${T1} 'ci: Last character should not be period.'" 8;
TestReturnCode "${T1} 'ci(i): Last character should not be period.'" 8;
TestReturnCode "${T1} 'ci: Last character is a space '" 8;
TestReturnCode "${T1} 'ci: Last character is a \\\`tick\\\`'" 8;

TestReturnCode "${T1} 'bug: This is an invalid label'" 9;
TestReturnCode "${T1} 'bug(i): This is an invalid label'" 9;

TestReturnCode "${T1} 'ci: Last character is a number v1.5.0'" 0;
TestReturnCode "${T1} 'ci: Last character is not lowercase alphabeT'" 0;
TestReturnCode "${T1} 'chore: This is a valid title'" 0;
Expand All @@ -51,3 +62,14 @@ TestReturnCode "${T1} 'refactor: This is a valid title'" 0;
TestReturnCode "${T1} 'test: This is a valid title'" 0;
TestReturnCode "${T1} 'tools: This is a valid title'" 0;
TestReturnCode "${T1} 'bot: Bump github.com/alternativesourcenetwork/defradb from 1.1.0.1.0.0 to 1.1.0.1.0.1'" 0;
TestReturnCode "${T1} 'ci(i): Valid ignore title'" 0;
TestReturnCode "${T1} 'chore(i): Valid ignore title'" 0;
TestReturnCode "${T1} 'docs(i): Valid ignore title'" 0;
TestReturnCode "${T1} 'feat(i): Valid ignore title'" 0;
TestReturnCode "${T1} 'fix(i): Valid ignore title'" 0;
TestReturnCode "${T1} 'perf(i): Valid ignore title'" 0;
TestReturnCode "${T1} 'refactor(i): Valid ignore title'" 0;
TestReturnCode "${T1} 'test(i): Valid ignore title'" 0;
TestReturnCode "${T1} 'tools(i): Valid ignore title'" 0;
TestReturnCode "${T1} 'bot(i): Bump github.com/alternativesourcenetwork/defradb from 1.1.0.1.0.0 to 1.1.0.1.0.1'" 0;
TestReturnCode "${T1} 'bot(i): Bump githurk/defradb from 1.1.0.1.0.0 to 1.1.0.1.0.1'" 0;
33 changes: 17 additions & 16 deletions tools/scripts/validate-conventional-style.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
# Usage: ./validate-conventional-style.sh "feat: Add a new feature"
#========================================================================================

readonly BOT_LABEL="bot";
readonly IGNORE_DECORATOR="(i)";

# Declare a non-mutable indexed array that contains all the subset of conventional style
# labels that we deem valid for our use case. There should always be insync with the
# labels we have defined for the change log in: `defradb/tools/configs/chglog/config.yml`.
Expand All @@ -19,42 +22,37 @@ readonly -a VALID_LABELS=("chore"
"refactor"
"test"
"tools"
"bot");

BOTPREFIX="bot"
"${BOT_LABEL}");

if [ "${#}" -ne 1 ]; then
printf "Error: Invalid number of arguments (pass title as 1 string argument).\n";
exit 2;
fi

TITLE=${1};
IS_BOT=false;
readonly TITLE="${1}";

# Detect if title is prefixed with `bot`
if [[ "${TITLE}" =~ ^"${BOTPREFIX}:" ]]; then
# Detect if title is prefixed with `bot`, skips length validation if it is.
if [[ "${TITLE}" =~ ^"${BOT_LABEL}:" ]] || [[ "${TITLE}" =~ ^"${BOT_LABEL}${IGNORE_DECORATOR}:" ]]; then
printf "Info: Title is from a bot, skipping length-related title validation.\n";
IS_BOT=true;
fi

# Validate that the entire length of the title is less than or equal to our character limit.
if [ "${#TITLE}" -gt 60 ] && [ "${IS_BOT}" = false ]; then
elif [[ "${#TITLE}" -gt 60 ]]; then
printf "Error: The length of the title is too long (should be 60 or less).\n";
exit 3;
fi

# Split the title at ':' and store the result in ${SPLIT_TOKENS}.
# Doing eval to ensure the split works for elements that contain spaces.
eval "SPLIT_TOKENS=($(echo "\"$TITLE\"" | sed 's/:/" "/g'))";
eval "SPLIT_TOKENS=($(echo "\"${TITLE}\"" | sed 's/:/" "/g'))";

# Validate the `:` token exists exactly once.
if [ "${#SPLIT_TOKENS[*]}" -ne 2 ]; then
printf "Error: Splitting title at ':' didn't result in 2 elements.\n";
exit 4;
fi

LABEL="${SPLIT_TOKENS[0]}";
DESCRIPTION="${SPLIT_TOKENS[1]}";
readonly LABEL="${SPLIT_TOKENS[0]}";
readonly DESCRIPTION="${SPLIT_TOKENS[1]}";

printf "Info: label = [%s]\n" "${LABEL}";
printf "Info: description = [%s]\n" "${DESCRIPTION}";
Expand All @@ -65,9 +63,9 @@ if [ "${#DESCRIPTION}" -le 2 ]; then
exit 5;
fi

CHECK_SPACE="${DESCRIPTION::1}"; # First character
CHECK_FIRST_UPPER_CASE="${DESCRIPTION:1:1}"; # Second character
CHECK_LAST_VALID="${DESCRIPTION: -1}"; # Last character
readonly CHECK_SPACE="${DESCRIPTION::1}"; # First character
readonly CHECK_FIRST_UPPER_CASE="${DESCRIPTION:1:1}"; # Second character
readonly CHECK_LAST_VALID="${DESCRIPTION: -1}"; # Last character

# Validate that there is a space between the label and description.
if [ "${CHECK_SPACE}" != " " ]; then
Expand All @@ -92,6 +90,9 @@ for validLabel in "${VALID_LABELS[@]}"; do
if [ "${LABEL}" == "${validLabel}" ]; then
printf "Success: Title's label and description style is valid.\n";
exit 0;
elif [ "${LABEL}" == "${validLabel}${IGNORE_DECORATOR}" ]; then
printf "Success: Title's label and description style is valid with ignore/internal decorator.\n";
exit 0;
fi
done

Expand Down

0 comments on commit c056713

Please sign in to comment.