Skip to content

Commit 25ef28c

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. Note: - This change requires a tool to parse the response from github.com.
1 parent e83f2fb commit 25ef28c

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

scripts/install-git-hooks

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,40 @@ 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 remote.origin.url | sed -e 's/^.*github.com[\/:]\(.*\)\/\([^\/]*\)\(\.git\?\)$/\1/')
35+
REPO_NAME=$(git config remote.origin.url | sed -e 's/^.*github.com[\/:]\(.*\)\/\([^\/]*\)\(\.git\?\)$/\2/')
3536

3637
CURL=$(which curl)
3738
if [ $? -ne 0 ]; then
3839
throw "curl not installed."
3940
fi
4041

42+
JQ=$(which jq)
43+
if [ $? -ne 0 ]; then
44+
echo "jq not installed."
45+
exit 1
46+
fi
47+
4148
CURL_RES=$(${CURL} -s \
4249
-H "Accept: application/vnd.github.v3+json" \
43-
https://api.github.com/repos/${ACCOUNT}/lab0-c/actions/workflows)
50+
https://api.github.com/repos/${ACCOUNT}/${REPO_NAME}/actions/workflows)
4451

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

5158
# 3. Ensure this repository is frok from sysprog21/lab0-c'.
5259
((CURRENT_STEP++))
5360
progress "$CURRENT_STEP" "$TOTAL_STEPS"
5461

5562
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
63+
RESPONSE=$(${CURL} -s -H "Accept: application/vnd.github.v3+json" \
64+
"https://api.github.com/repos/${ACCOUNT}/${REPO_NAME}")
65+
[[ $(echo "$RESPONSE" | jq -r '.fork') != "true" || \
66+
$(echo "$RESPONSE" | jq -r '.parent.full_name') != "sysprog21/lab0-c" ]] && \
67+
throw "Your repository MUST be forked from 'sysprog21/lab0-c'."
6568
fi
6669

6770
# 4. Check GitHub Actions
@@ -72,13 +75,13 @@ if [ ${TOTAL_COUNT} -eq "0" ]; then
7275
printf "\n[!] GitHub Actions MUST be activated."
7376
case ${OSTYPE} in
7477
"linux"*)
75-
xdg-open https://github.com/${ACCOUNT}/lab0-c/actions > /dev/null 2>&1
78+
xdg-open https://github.com/${ACCOUNT}/${REPO_NAME}/actions > /dev/null 2>&1
7679
;;
7780
"darwin"*)
78-
open https://github.com/${ACCOUNT}/lab0-c/actions
81+
open https://github.com/${ACCOUNT}/${REPO_NAME}/actions
7982
;;
8083
*)
81-
echo "Please activate at https://github.com/${ACCOUNT}/lab0-c/actions"
84+
echo "Please activate at https://github.com/${ACCOUNT}/${REPO_NAME}/actions"
8285
;;
8386
esac
8487
throw "Check this article: https://docs.github.com/en/actions/managing-workflow-runs/disabling-and-enabling-a-workflow\n\

0 commit comments

Comments
 (0)