Skip to content

Commit eb59173

Browse files
committed
Improve fork check in Git hooks installation
This change allows users to modify the repository name when working with a fork. Modifications: - Introduced repository name to store the repository name dynamically. - Replaced fixed occurrences of lab0-c in API calls with current repository name. - Improved fork validation by: 1. Checking if the repository is a fork. 2. Verifying that the parent repository's full name.
1 parent e83f2fb commit eb59173

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

scripts/install-git-hooks

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ fi
3131
((CURRENT_STEP++))
3232
progress "$CURRENT_STEP" "$TOTAL_STEPS"
3333

34-
ACCOUNT=$(git config -l | grep -w remote.origin.url | sed -e 's/^.*github.com[\/:]\(.*\)\/lab0-c.*/\1/')
34+
ACCOUNT=$(git config --get remote.origin.url | awk -F'[:/]' '{print $(NF-1)}')
35+
REPO_NAME=$(git config --get remote.origin.url | awk -F'[:/]' '{gsub(/\.git$/, "", $NF); print $NF}')
3536

3637
CURL=$(which curl)
3738
if [ $? -ne 0 ]; then
@@ -40,28 +41,28 @@ fi
4041

4142
CURL_RES=$(${CURL} -s \
4243
-H "Accept: application/vnd.github.v3+json" \
43-
https://api.github.com/repos/${ACCOUNT}/lab0-c/actions/workflows)
44+
https://api.github.com/repos/${ACCOUNT}/${REPO_NAME}/actions/workflows)
4445

4546
TOTAL_COUNT=$(echo ${CURL_RES} | sed -e 's/.*"total_count": \([^,"]*\).*/\1/')
4647
case ${TOTAL_COUNT} in
4748
*"Not Found"*)
48-
throw "Check your repository. It should be located at https://github.com/${ACCOUNT}/lab0-c"
49+
throw "Check your repository. It should be located at https://github.com/${ACCOUNT}/${REPO_NAME}"
4950
esac
5051

5152
# 3. Ensure this repository is frok from sysprog21/lab0-c'.
5253
((CURRENT_STEP++))
5354
progress "$CURRENT_STEP" "$TOTAL_STEPS"
5455

5556
if [[ "${ACCOUNT}" != "sysprog21" ]]; then
56-
REPO_FORKED=$(${CURL} -s \
57-
-H "Accept: application/vnd.github.v3+json" \
58-
https://api.github.com/repos/${ACCOUNT}/lab0-c | grep -m 1 fork)
59-
case ${REPO_FORKED} in
60-
*true*)
61-
;;
62-
*)
63-
throw "Your repository MUST be forked from 'sysprog21/lab0-c'."
64-
esac
57+
RESPONSE=$(${CURL} -s -H "Accept: application/vnd.github.v3+json" \
58+
"https://api.github.com/repos/${ACCOUNT}/${REPO_NAME}")
59+
60+
IS_FORK=$(echo "$RESPONSE" | sed -n 's/.*"fork": \(true\|false\).*/\1/p' | head -n1)
61+
PARENT_NAME=$(echo "$RESPONSE" | awk -F'"' '/"parent": \{/{flag=1} flag && /"full_name":/{print $4; exit}')
62+
63+
if [[ "$IS_FORK" != "true" || "$PARENT_NAME" != "sysprog21/lab0-c" ]]; then
64+
throw "Your repository MUST be forked from 'sysprog21/lab0-c'."
65+
fi
6566
fi
6667

6768
# 4. Check GitHub Actions
@@ -72,13 +73,13 @@ if [ ${TOTAL_COUNT} -eq "0" ]; then
7273
printf "\n[!] GitHub Actions MUST be activated."
7374
case ${OSTYPE} in
7475
"linux"*)
75-
xdg-open https://github.com/${ACCOUNT}/lab0-c/actions > /dev/null 2>&1
76+
xdg-open https://github.com/${ACCOUNT}/${REPO_NAME}/actions > /dev/null 2>&1
7677
;;
7778
"darwin"*)
78-
open https://github.com/${ACCOUNT}/lab0-c/actions
79+
open https://github.com/${ACCOUNT}/${REPO_NAME}/actions
7980
;;
8081
*)
81-
echo "Please activate at https://github.com/${ACCOUNT}/lab0-c/actions"
82+
echo "Please activate at https://github.com/${ACCOUNT}/${REPO_NAME}/actions"
8283
;;
8384
esac
8485
throw "Check this article: https://docs.github.com/en/actions/managing-workflow-runs/disabling-and-enabling-a-workflow\n\

0 commit comments

Comments
 (0)