Last active
December 20, 2020 23:18
-
-
Save stronk7/89c83ebe303cf2d9205099b0fb6d9905 to your computer and use it in GitHub Desktop.
pre-push hook to avoid Moodle upstream branches to perform travis/github actions builds all the time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# pre-push hook to enable/disable travis and github actions push builds | |
# based on the nature of the pushed branches. | |
# - upstream and deleted ones won't be built. | |
# - development ones will be built. | |
# Requires travis (client) gem binary installed. | |
# Requires gh (client) binary installed. | |
# Useful for people wanting to keep their upstream branches updated, | |
# and not causing them to perform travis/github actions builds all the time, like me :-) | |
IFS=' ' | |
hasupstreambranch= | |
hasdevelopmentbranches= | |
hasdeletedbranches= | |
while read localref localsha remoteref remotesha; do | |
if [[ $localref =~ (master|MOODLE_[[:digit:]]{2,3}_STABLE) ]]; then | |
hasupstreambranch="$hasupstreambranch $localref" | |
elif [[ $localref =~ \(delete\) ]]; then | |
hasdeletedbranches="$hasdeletedbranches $remoteref" | |
else | |
hasdevelopmentbranches="$hasdevelopmentbranches $localref" | |
fi | |
done | |
echo | |
echo "pre-push hook:" | |
echo " upstream branches: $hasupstreambranch" | |
echo "development branches: $hasdevelopmentbranches" | |
echo " deleted branches: $hasdeletedbranches" | |
echo | |
if [[ -n $hasupstreambranch ]] && [[ -n $hasdevelopmentbranches ]]; then | |
echo "Mixed upstream and development branches. Please push them separately." | |
exit 1 | |
fi | |
if [[ -n $hasdeletedbranches ]] && [[ -n $hasdevelopmentbranches ]]; then | |
echo "Mixed deleted and development branches. Please push them separately." | |
exit 1 | |
fi | |
if [[ -n $hasupstreambranch ]] && [[ -z $hasdevelopmentbranches ]]; then | |
echo "Upstream branches, disabling travis push builds." | |
travis settings build_pushes --com --disable | |
echo "Upstream branches, disabling github actions builds." | |
gh api repos/:owner/:repo/actions/permissions --method PUT --field "enabled=false" | |
exit 0 | |
fi | |
if [[ -n $hasdeletedbranches ]] && [[ -z $hasdevelopmentbranches ]]; then | |
echo "Deleted branches, disabling travis push builds." | |
travis settings build_pushes --com --disable | |
echo "Deleted branches, disabling github actions builds." | |
gh api repos/:owner/:repo/actions/permissions --method PUT --field "enabled=false" | |
exit 0 | |
fi | |
if [[ -z $hasupstreambranch ]] && [[ -n $hasdevelopmentbranches ]]; then | |
echo "Development branches, enabling travis push builds." | |
travis settings build_pushes --com --enable | |
echo "Development branches, enabling github actions builds." | |
gh api repos/:owner/:repo/actions/permissions --method PUT --field "enabled=true" | |
exit 0 | |
fi | |
echo "Nothing to push?" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment