Skip to content

Commit e4cf564

Browse files
Fix commit message (#24)
* add logs * fix vars in parsing for proper logging * update to use BASH_ENV to pass env vars * fix SC2002 * disable SC2018 * update test * call with correct args * disable shellcheck rules * fix validation of test * fix case where PR is present * fix test * fix test * fix test * fix test
1 parent 031bda7 commit e4cf564

File tree

4 files changed

+72
-24
lines changed

4 files changed

+72
-24
lines changed

scripts/pack-and-publish.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ PackAndPublish() {
1313
version_suffix="$1"
1414
fi
1515

16+
# Generate a random 4-letter suffix with LC_ALL=C specifically for tr
17+
# disable "lower" shellcheck rule because we only want a-z
18+
# shellcheck disable=SC2018
19+
random_suffix=$(LC_ALL=C tr -dc 'a-z' < /dev/urandom | fold -w 4 | head -n 1)
20+
21+
# Append the random suffix to version_suffix
22+
version_suffix="${version_suffix}-${random_suffix}"
23+
1624
rm -f "$output_path"
1725
circleci orb pack "$source_path" > "$output_path"
1826
circleci orb validate "$output_path" || { echo "Orb validation failed"; exit 1; }

src/scripts/commit_and_push_to_target.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ fi
1818

1919
ORB_TEST_ENV="bats-core"
2020
if [ "${0#*"$ORB_TEST_ENV"}" = "$0" ]; then
21+
echo "Final Commit Message: '$FINAL_COMMIT_MESSAGE'"
2122
CommitAndPushToTarget
2223
fi

src/scripts/parse_commit_info.sh

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
#! /bin/bash
22

3+
# Global variable declarations
4+
COMMIT_MESSAGE=""
5+
COMMIT_HASH=""
6+
REPO_URL=""
7+
REPO_NAME=""
8+
PR_NUMBER=""
9+
final_commit_message=""
10+
311
FetchCommitMessage() {
412
git log -1 --pretty=%B || { echo "Fetching commit message failed"; exit 1; }
513
}
@@ -21,30 +29,48 @@ ExtractPRNumber() {
2129
}
2230

2331
ConstructCommitMessage() {
32+
local REPO_NAME="$1"
33+
local COMMIT_MESSAGE="$2"
34+
local PR_NUMBER="$3"
35+
local COMMIT_HASH="$4"
36+
2437
if [ -n "$PR_NUMBER" ]; then
2538
PR_LINK="https://github.com/infinitered/$REPO_NAME/pull/$PR_NUMBER"
26-
echo "orb: $REPO_NAME -- $COMMIT_MESSAGE -- $PR_LINK"
39+
echo "orb($REPO_NAME): $COMMIT_MESSAGE $PR_LINK"
2740
else
2841
COMMIT_LINK="https://github.com/infinitered/$REPO_NAME/commit/$COMMIT_HASH"
29-
echo "orb: $REPO_NAME -- $COMMIT_MESSAGE -- $COMMIT_LINK"
42+
echo "orb($REPO_NAME): $COMMIT_MESSAGE $COMMIT_LINK"
3043
fi
3144
}
3245

3346
ParseCommitInfo() {
3447
cd "$SOURCE_REPO_DIRECTORY" || { echo "Changing directory failed"; exit 1; }
48+
49+
# Diagnostic output
50+
echo "Current directory: $(pwd)"
51+
ls -al
52+
3553
COMMIT_MESSAGE=$(FetchCommitMessage)
3654
COMMIT_HASH=$(FetchCommitHash)
3755
REPO_URL=$(FetchRepoURL)
3856
REPO_NAME=$(ParseRepoName)
3957
PR_NUMBER=$(ExtractPRNumber)
40-
41-
final_commit_message=$(ConstructCommitMessage)
42-
echo "$final_commit_message"
58+
final_commit_message=$(ConstructCommitMessage "$REPO_NAME" "$COMMIT_MESSAGE" "$PR_NUMBER" "$COMMIT_HASH")
59+
echo "Final constructed message: $final_commit_message"
4360
}
4461

4562
ORB_TEST_ENV="bats-core"
4663
if [ "${0#*"$ORB_TEST_ENV"}" = "$0" ]; then
4764
final_commit_message="$(ParseCommitInfo)"
65+
66+
# Logging statements to inspect variables
67+
echo "COMMIT_MESSAGE: $COMMIT_MESSAGE"
68+
echo "COMMIT_HASH: $COMMIT_HASH"
69+
echo "REPO_URL: $REPO_URL"
70+
echo "REPO_NAME: $REPO_NAME"
71+
echo "PR_NUMBER: $PR_NUMBER"
72+
echo "final_commit_message: $final_commit_message"
73+
4874
export FINAL_COMMIT_MESSAGE=$final_commit_message
75+
echo "export FINAL_COMMIT_MESSAGE='$final_commit_message'" >> "$BASH_ENV"
4976
fi
50-

src/tests/parse_commit_info.bats

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#! /bin/bash
2+
# shellcheck disable=SC2031
3+
# shellcheck disable=SC2030
24

3-
COMMIT_MESSAGE_WITH_PR="Fix: Commit for testing (#42)"
5+
setup() {
6+
export COMMIT_MESSAGE_WITH_PR="Fix: Commit for testing (#42)"
7+
}
48

59
# Mocking git commands and basename
610
git() {
711
case "$1" in
8-
log) echo $COMMIT_MESSAGE_WITH_PR;;
12+
log) echo "$COMMIT_MESSAGE_WITH_PR";;
913
rev-parse) echo "1234567890abcdef";;
1014
config) echo "https://github.com/infinitered/sample-repo.git";;
1115
*) return 1;;
@@ -21,32 +25,33 @@ source ./src/scripts/parse_commit_info.sh
2125

2226
@test "It fetches the last commit message" {
2327
run FetchCommitMessage
24-
echo "Debug: Output = '$output'" # Verbose log
28+
>&2 echo "Debug: Output = '$output'" # Verbose log
2529
[[ $output =~ Fix:\ Commit\ for\ testing\ \(#42\) ]]
2630
}
2731

2832
@test "It fetches the commit hash" {
2933
run FetchCommitHash
30-
echo "Debug: Output = '$output'" # Verbose log
34+
>&2 echo "Debug: Output = '$output'" # Verbose log
3135
[[ $output =~ 1234567890abcdef ]]
3236
}
3337

3438
@test "It fetches the repo URL" {
3539
run FetchRepoURL
36-
echo "Debug: Output = '$output'" # Verbose log
40+
>&2 echo "Debug: Output = '$output'" # Verbose log
3741
[[ $output =~ https://github.com/infinitered/sample-repo.git ]]
3842
}
3943

4044
@test "It fetches the repo name" {
4145
run ParseRepoName
42-
echo "Debug: Output = '$output'" # Verbose log
46+
>&2 echo "Debug: Output = '$output'" # Verbose log
4347
[[ $output =~ sample-repo ]]
4448
}
4549

4650
@test "It fetches PR number from commit message" {
51+
# shellcheck disable=SC2030
4752
export COMMIT_MESSAGE=$COMMIT_MESSAGE_WITH_PR
4853
run ExtractPRNumber
49-
echo "Debug: Output = '$output'" # Verbose log
54+
>&2 echo "Debug: Output = '$output'" # Verbose log
5055
[[ $output =~ \42 ]]
5156
}
5257

@@ -57,10 +62,9 @@ source ./src/scripts/parse_commit_info.sh
5762
export REPO_NAME="sample-repo"
5863
export COMMIT_MESSAGE="Fix: Commit for testing (#42)"
5964

60-
run ConstructCommitMessage
61-
echo "Debug: Output = '$output'" # Verbose log
62-
[[ $output =~ https://github.com/infinitered/sample-repo/pull/42 ]]
63-
[[ $output =~ orb:\ sample-repo\ --\ Fix:\ Commit\ for\ testing\ \(#42\)\ --\ https://github.com/infinitered/sample-repo/pull/42 ]]
65+
run ConstructCommitMessage "$REPO_NAME" "$COMMIT_MESSAGE" "$PR_NUMBER" "$COMMIT_HASH"
66+
>&2 echo "Debug: Output = '$output'" # Verbose log
67+
[[ $output == "orb(sample-repo): Fix: Commit for testing (#42) https://github.com/infinitered/sample-repo/pull/42" ]]
6468
unset PR_NUMBER
6569
unset REPO_NAME
6670
unset COMMIT_MESSAGE
@@ -69,23 +73,32 @@ source ./src/scripts/parse_commit_info.sh
6973
@test "It constructs commit link and commit message when PR number is absent" {
7074
# Unset PR number to simulate absence
7175
unset PR_NUMBER
72-
# shellcheck disable=SC2031
7376
export REPO_NAME="sample-repo"
7477
export COMMIT_HASH="1234567890abcdef"
7578
export COMMIT_MESSAGE="Fix: Commit for testing"
7679

77-
run ConstructCommitMessage
78-
echo "Debug: Output = '$output'" # Verbose log
79-
[[ $output =~ https://github.com/infinitered/sample-repo/commit/1234567890abcdef ]]
80-
[[ $output =~ orb:\ sample-repo\ --\ Fix:\ Commit\ for\ testing\ --\ https://github.com/infinitered/sample-repo/commit/1234567890abcdef ]]
80+
run ConstructCommitMessage "$REPO_NAME" "$COMMIT_MESSAGE" "$PR_NUMBER" "$COMMIT_HASH"
81+
[[ $output =~ orb\(sample-repo\):\ Fix:\ Commit\ for\ testing\ https://github.com/infinitered/sample-repo/commit/$COMMIT_HASH ]]
82+
>&2 echo "Debug: Output = '$output'" # Verbose log
83+
8184
unset REPO_NAME
8285
unset COMMIT_HASH
8386
unset COMMIT_MESSAGE
8487
}
8588

8689
@test "It parses and constructs the final commit message" {
8790
run ParseCommitInfo
88-
echo "Debug: Output = '$output'" # Verbose log
89-
[[ $output == "orb: sample-repo -- Fix: Commit for testing (#42) -- https://github.com/infinitered/sample-repo/pull/42" ]]
91+
final_msg=$(echo "$output" | grep "^Final constructed message:" | cut -d ':' -f 2- | xargs)
92+
>&2 echo "Debug: Extracted Final Commit Message = '$final_msg'"
93+
[[ $final_msg == "orb(sample-repo): Fix: Commit for testing (#42) https://github.com/infinitered/sample-repo/pull/42" ]]
9094
}
9195

96+
97+
teardown() {
98+
unset COMMIT_MESSAGE_WITH_PR
99+
unset PR_NUMBER
100+
unset REPO_NAME
101+
unset COMMIT_MESSAGE
102+
unset COMMIT_HASH
103+
unset final_msg
104+
}

0 commit comments

Comments
 (0)