Skip to content

Commit b4d2c69

Browse files
Parse org name and repo name from CIRCLE_REPOSITORY_URL (#28)
* parse org name from repo url * remove logs * add logs * push logs to stderr * use test repo for tests * log to stdout * mock cd * fix missing params in test calls * fix typo * try making things conditional on tests * fix commit hash * update test * remove vars * update test * fix var * update test
1 parent d79c1a0 commit b4d2c69

File tree

3 files changed

+193
-77
lines changed

3 files changed

+193
-77
lines changed

.circleci/test-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
cd ..
4141
fi
4242
# Run Bats tests
43-
bats ./src/tests/*.bats
43+
bats --tap ./src/tests/*.bats
4444
workflows:
4545
test-deploy:
4646
jobs:

src/scripts/parse_commit_info.sh

Lines changed: 88 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ REPO_NAME=""
88
PR_NUMBER=""
99
final_commit_message=""
1010

11+
ExtractPRNumber() {
12+
local commit_msg="$1"
13+
echo "$commit_msg" | grep -o "#[0-9]\+" | grep -o "[0-9]\+" | tail -n 1 || true
14+
}
15+
1116
FetchCommitMessage() {
1217
git log -1 --pretty=%B || { echo "Fetching commit message failed"; exit 1; }
1318
}
@@ -16,45 +21,106 @@ FetchCommitHash() {
1621
git rev-parse HEAD || { echo "Fetching commit hash failed"; exit 1; }
1722
}
1823

19-
FetchRepoURL() {
20-
git config --get remote.origin.url || { echo "Fetching repo URL failed"; exit 1; }
24+
# Normalize the repository URL for consistent usage
25+
GetNormalizedRepoURL() {
26+
local circle_repo_url="$1"
27+
if [[ $circle_repo_url == https://* ]]; then
28+
# If it's already an HTTPS URL, just ensure it doesn't have the .git suffix
29+
echo "${circle_repo_url%.git}"
30+
else
31+
# Convert SSH format to https format for consistency
32+
local https_url="${circle_repo_url/git@github.com:/https://github.com/}"
33+
echo "${https_url%.git}" # Strip trailing .git if present
34+
fi
2135
}
2236

23-
ParseRepoName() {
24-
basename -s .git "$REPO_URL" || { echo "Parsing repo name failed"; exit 1; }
25-
}
37+
ExtractGitHubOrgAndRepo() {
38+
local repo_url="$1"
39+
if [[ $repo_url != *github.com* ]]; then
40+
echo "Error: Not a GitHub URL." >&2
41+
exit 1
42+
fi
2643

27-
ExtractPRNumber() {
28-
echo "$COMMIT_MESSAGE" | grep -o "#[0-9]\+" | grep -o "[0-9]\+" || true
44+
local extracted
45+
extracted=$(echo "$repo_url" | awk -F'/' '{gsub(".git", "", $NF); print $(NF-1), $NF}')
46+
47+
if [ -z "$extracted" ]; then
48+
echo "Error: Invalid GitHub URL format." >&2
49+
exit 1
50+
fi
51+
52+
echo "$extracted"
2953
}
3054

3155
ConstructCommitMessage() {
32-
local REPO_NAME="$1"
33-
local COMMIT_MESSAGE="$2"
34-
local PR_NUMBER="$3"
35-
local COMMIT_HASH="$4"
36-
37-
if [ -n "$PR_NUMBER" ]; then
38-
PR_LINK="https://github.com/infinitered/$REPO_NAME/pull/$PR_NUMBER"
39-
echo "orb($REPO_NAME): $COMMIT_MESSAGE $PR_LINK"
56+
local org="$1"
57+
local repo="$2"
58+
local commit_msg="$3"
59+
local pr_num="$4"
60+
local commit_hash="$5"
61+
62+
local link
63+
if [ -n "$pr_num" ]; then
64+
link=$(CreatePRLink "$org" "$repo" "$pr_num")
4065
else
41-
COMMIT_LINK="https://github.com/infinitered/$REPO_NAME/commit/$COMMIT_HASH"
42-
echo "orb($REPO_NAME): $COMMIT_MESSAGE $COMMIT_LINK"
66+
link=$(CreateCommitLink "$org" "$repo" "$commit_hash")
4367
fi
68+
echo "orb($repo): $commit_msg $link"
69+
}
70+
71+
# Create PR Link
72+
CreatePRLink() {
73+
local org="$1"
74+
local repo="$2"
75+
local pr_number="$3"
76+
echo "https://github.com/$org/$repo/pull/$pr_number"
77+
}
78+
79+
# Create Commit Link
80+
CreateCommitLink() {
81+
local org="$1"
82+
local repo="$2"
83+
local commit_hash="$3"
84+
echo "https://github.com/$org/$repo/commit/$commit_hash"
4485
}
4586

4687
ParseCommitInfo() {
47-
cd "$SOURCE_REPO_DIRECTORY" || { echo "Changing directory failed"; exit 1; }
88+
if [ "$ORB_TEST_ENV" = "bats-core" ]; then
89+
REPOSITORY_URL="$TEST_REPO_URL"
90+
else
91+
cd "$SOURCE_REPO_DIRECTORY" || { echo "Changing directory failed"; exit 1; }
92+
REPOSITORY_URL="$CIRCLE_REPOSITORY_URL"
93+
fi
94+
95+
# Debug logs
96+
# >&2 echo "DEBUG: REPOSITORY_URL = $REPOSITORY_URL"
4897

4998
COMMIT_MESSAGE=$(FetchCommitMessage)
5099
COMMIT_HASH=$(FetchCommitHash)
51-
REPO_URL=$(FetchRepoURL)
52-
REPO_NAME=$(ParseRepoName)
53-
PR_NUMBER=$(ExtractPRNumber)
54-
final_commit_message=$(ConstructCommitMessage "$REPO_NAME" "$COMMIT_MESSAGE" "$PR_NUMBER" "$COMMIT_HASH")
100+
101+
# Debug logs
102+
# >&2 echo "DEBUG: COMMIT_MESSAGE = $COMMIT_MESSAGE"
103+
# >&2 echo "DEBUG: COMMIT_HASH = $COMMIT_HASH"
104+
105+
REPO_URL=$(GetNormalizedRepoURL "$REPOSITORY_URL")
106+
107+
# Debug logs
108+
# >&2 echo "DEBUG: REPO_URL = $REPO_URL"
109+
110+
read -r ORG_NAME REPO_NAME <<< "$(ExtractGitHubOrgAndRepo "$REPO_URL")"
111+
PR_NUMBER=$(ExtractPRNumber "$COMMIT_MESSAGE")
112+
113+
# # Debug logs
114+
# >&2 echo "DEBUG: ORG_NAME = $ORG_NAME"
115+
# >&2 echo "DEBUG: REPO_NAME = $REPO_NAME"
116+
# >&2 echo "DEBUG: PR_NUMBER = $PR_NUMBER"
117+
118+
final_commit_message=$(ConstructCommitMessage "$ORG_NAME" "$REPO_NAME" "$COMMIT_MESSAGE" "$PR_NUMBER" "$COMMIT_HASH")
119+
55120
echo "$final_commit_message"
56121
}
57122

123+
58124
ORB_TEST_ENV="bats-core"
59125
if [ "${0#*"$ORB_TEST_ENV"}" = "$0" ]; then
60126
final_commit_message="$(ParseCommitInfo)"

src/tests/parse_commit_info.bats

Lines changed: 104 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,103 +3,153 @@
33
# shellcheck disable=SC2030
44

55
setup() {
6-
export COMMIT_MESSAGE_WITH_PR="Fix: Commit for testing (#42)"
6+
export COMMIT_HASH="commitHash1234567890"
7+
export COMMIT_MESSAGE_WITHOUT_PR="Test: Commit message without PR"
8+
export COMMIT_MESSAGE_WITH_PR="Test: Commit message with PR (#42)"
9+
export ORG_NAME="org-name"
10+
export REPO_NAME="repo-name"
11+
export TEST_REPO_URL="https://github.com/org-name/repo-name.git"
12+
export TEST_COMMIT_MESSAGE="$COMMIT_MESSAGE_WITH_PR"
713
}
814

915
# Mocking git commands and basename
1016
git() {
1117
case "$1" in
12-
log) echo "$COMMIT_MESSAGE_WITH_PR";;
13-
rev-parse) echo "1234567890abcdef";;
14-
config) echo "https://github.com/infinitered/sample-repo.git";;
18+
log) echo "$TEST_COMMIT_MESSAGE";;
19+
rev-parse) echo "$COMMIT_HASH";;
20+
config) echo "$REPO_URL_MOCK";;
1521
*) return 1;;
1622
esac
1723
}
1824

25+
# Mock the cd command
26+
cd() {
27+
# Store the directory change in a variable for inspection
28+
# shellcheck disable=SC2034
29+
LAST_CD_DIRECTORY="$1"
30+
# You can also echo a message to indicate that cd was called, if needed
31+
# echo "cd $1"
32+
}
33+
1934
basename() {
20-
echo "sample-repo"
35+
echo "$REPO_NAME"
2136
}
2237

2338
# Source the script to get ParseCommitInfo function
2439
source ./src/scripts/parse_commit_info.sh
2540

26-
@test "It fetches the last commit message" {
41+
@test "FetchCommitMessage: It fetches the last commit message" {
2742
run FetchCommitMessage
28-
>&2 echo "Debug: Output = '$output'" # Verbose log
29-
[[ $output =~ Fix:\ Commit\ for\ testing\ \(#42\) ]]
43+
[[ "$output" =~ Test:\ Commit\ message\ with\ PR\ \(#42\) ]]
3044
}
3145

32-
@test "It fetches the commit hash" {
46+
@test "FetchCommitHash: It fetches the commit hash" {
3347
run FetchCommitHash
34-
>&2 echo "Debug: Output = '$output'" # Verbose log
35-
[[ $output =~ 1234567890abcdef ]]
48+
[[ $output =~ $COMMIT_HASH ]]
49+
}
50+
51+
@test "GetNormalizedRepoURL: It fetches and normalizes HTTPS repo URL" {
52+
export REPO_URL_MOCK="https://github.com/org-name/repo-name.git"
53+
run GetNormalizedRepoURL $REPO_URL_MOCK
54+
echo "DEBUG: output \"$output\""
55+
[[ $output == "https://github.com/org-name/repo-name" ]]
56+
}
57+
58+
@test "GetNormalizedRepoURL: It fetches and normalizes SSH repo URL" {
59+
export REPO_URL_MOCK="[email protected]:org-name/repo-name.git"
60+
run GetNormalizedRepoURL $REPO_URL_MOCK
61+
echo "DEBUG: output \"$output\""
62+
[[ $output == "https://github.com/org-name/repo-name" ]]
63+
}
64+
65+
@test "ExtractGitHubOrgAndRepo: It extracts org and repo from HTTPS URL with .git suffix" {
66+
run ExtractGitHubOrgAndRepo "https://github.com/sample-org/sample-repo.git"
67+
[ "$status" -eq 0 ]
68+
[ "$output" = "sample-org sample-repo" ]
3669
}
3770

38-
@test "It fetches the repo URL" {
39-
run FetchRepoURL
40-
>&2 echo "Debug: Output = '$output'" # Verbose log
41-
[[ $output =~ https://github.com/infinitered/sample-repo.git ]]
71+
@test "ExtractGitHubOrgAndRepo: It extracts org and repo from HTTPS URL without .git suffix" {
72+
run ExtractGitHubOrgAndRepo "https://github.com/sample-org/sample-repo"
73+
[ "$status" -eq 0 ]
74+
[ "$output" = "sample-org sample-repo" ]
4275
}
4376

44-
@test "It fetches the repo name" {
45-
run ParseRepoName
46-
>&2 echo "Debug: Output = '$output'" # Verbose log
47-
[[ $output =~ sample-repo ]]
77+
@test "ExtractGitHubOrgAndRepo: It throws an error when given an invalid GitHub URL" {
78+
run ExtractGitHubOrgAndRepo "https://invalid.com/sample-org/sample-repo.git"
79+
[ "$status" -eq 1 ]
80+
[[ "$output" == "Error: Not a GitHub URL."* ]]
4881
}
4982

50-
@test "It fetches PR number from commit message" {
51-
# shellcheck disable=SC2030
52-
export COMMIT_MESSAGE=$COMMIT_MESSAGE_WITH_PR
53-
run ExtractPRNumber
54-
>&2 echo "Debug: Output = '$output'" # Verbose log
83+
84+
@test "ExtractPRNumber: It fetches PR number from commit message" {
85+
run ExtractPRNumber "$COMMIT_MESSAGE_WITH_PR"
5586
[[ $output =~ \42 ]]
5687
}
5788

58-
@test "It constructs PR link and commit message when PR number is present" {
59-
# Set up PR number
60-
export PR_NUMBER=42
61-
# shellcheck disable=SC2030
62-
export REPO_NAME="sample-repo"
63-
export COMMIT_MESSAGE="Fix: Commit for testing (#42)"
89+
@test "ExtractPRNumber: It takes the last possible PR number in messages with multiple potential PR numbers" {
90+
export TEST_COMMIT_MESSAGE="Fix: Commit for testing (#123) (#42)"
91+
run ExtractPRNumber "$TEST_COMMIT_MESSAGE"
92+
echo "DEBUG: output \"$output\""
93+
[[ $output =~ \42 ]] # Assuming it extracts the last PR number by default
94+
}
6495

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" ]]
96+
@test "CreatePRLink: It constructs PR link" {
97+
export PR_NUMBER=42
98+
run CreatePRLink "$ORG_NAME" "$REPO_NAME" "$PR_NUMBER"
99+
[[ $output == https://github.com/org-name/repo-name/pull/42 ]]
68100
unset PR_NUMBER
69-
unset REPO_NAME
70-
unset COMMIT_MESSAGE
71101
}
72102

73-
@test "It constructs commit link and commit message when PR number is absent" {
74-
# Unset PR number to simulate absence
75-
unset PR_NUMBER
76-
export REPO_NAME="sample-repo"
77-
export COMMIT_HASH="1234567890abcdef"
78-
export COMMIT_MESSAGE="Fix: Commit for testing"
103+
@test "CreateCommitLink: It constructs commit link" {
104+
run CreateCommitLink "$ORG_NAME" "$REPO_NAME" "$COMMIT_HASH"
105+
[[ $output == "https://github.com/org-name/repo-name/commit/$COMMIT_HASH" ]]
106+
}
79107

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 ]]
82108

83-
>&2 echo "Debug: Output = '$output'" # Verbose log
109+
@test "FetchCommitMessage: It handles commit messages with special characters" {
110+
export TEST_COMMIT_MESSAGE="Fix & Improve: Commit for !testing (#42)"
111+
run FetchCommitMessage
112+
echo "DEBUG: output \"$output\""
113+
[[ $output =~ Fix\ \&\ Improve:\ Commit\ for\ \!testing\ \(#42\) ]]
114+
}
84115

85-
unset REPO_NAME
86-
unset COMMIT_HASH
87-
unset COMMIT_MESSAGE
116+
117+
@test "ParseCommitInfo: It handles commit messages with URL-like strings" {
118+
export TEST_COMMIT_MESSAGE="Fix: See https://example.com/issues/42 for more info (#42)"
119+
run ParseCommitInfo
120+
FINAL_MSG=$(echo "$output" | xargs)
121+
echo "DEBUG FINAL_MSG: \"$FINAL_MSG\""
122+
echo "EXPECTED FINAL_MSG: \"orb($REPO_NAME): $COMMIT_MESSAGE_WITHOUT_PR https://github.com/$ORG_NAME/$REPO_NAME/commit/$COMMIT_HASH\""
123+
[[ $FINAL_MSG == "orb($REPO_NAME): Fix: See https://example.com/issues/42 for more info (#42) https://github.com/org-name/repo-name/pull/42" ]]
88124
}
89125

90-
@test "It parses and constructs the final commit message" {
126+
@test "ParseCommitInfo: It parses and constructs the final commit message with PR link" {
91127
run ParseCommitInfo
92-
final_msg=$(echo "$output" | grep "^Final constructed message:" | cut -d ':' -f 2- | xargs)
93-
>&2 echo "Debug: Extracted Final Commit Message = '$final_msg'"
94-
[[ $final_msg == "orb(sample-repo): Fix: Commit for testing (#42) https://github.com/infinitered/sample-repo/pull/42" ]]
128+
FINAL_MSG=$(echo "$output" | xargs)
129+
echo "DEBUG FINAL_MSG: \"$FINAL_MSG\""
130+
echo "EXPECTED FINAL_MSG: \"orb($REPO_NAME): $COMMIT_MESSAGE_WITHOUT_PR https://github.com/$ORG_NAME/$REPO_NAME/commit/$COMMIT_HASH\""
131+
[[ $FINAL_MSG == "orb(repo-name): $COMMIT_MESSAGE_WITH_PR https://github.com/org-name/repo-name/pull/42" ]]
132+
}
133+
134+
@test "ParseCommitInfo: It parses and constructs the final commit message with commit link" {
135+
export TEST_COMMIT_MESSAGE="$COMMIT_MESSAGE_WITHOUT_PR"
136+
run ParseCommitInfo
137+
FINAL_MSG=$(echo "$output" | xargs)
138+
echo "DEBUG FINAL_MSG: \"$FINAL_MSG\""
139+
echo "EXPECTED FINAL_MSG: \"orb($REPO_NAME): $COMMIT_MESSAGE_WITHOUT_PR https://github.com/$ORG_NAME/$REPO_NAME/commit/$COMMIT_HASH\""
140+
[[ $FINAL_MSG == "orb($REPO_NAME): $COMMIT_MESSAGE_WITHOUT_PR https://github.com/$ORG_NAME/$REPO_NAME/commit/$COMMIT_HASH" ]]
95141
}
96142

97143

98144
teardown() {
145+
unset COMMIT_HASH
146+
unset COMMIT_MESSAGE
147+
unset COMMIT_MESSAGE_WITHOUT_PR
99148
unset COMMIT_MESSAGE_WITH_PR
149+
unset FINAL_MSG
150+
unset ORG_NAME
100151
unset PR_NUMBER
101152
unset REPO_NAME
102-
unset COMMIT_MESSAGE
103-
unset COMMIT_HASH
104-
unset final_msg
153+
unset TEST_REPO_URL
154+
unset TEST_COMMIT_MESSAGE
105155
}

0 commit comments

Comments
 (0)