Skip to content

Instantly share code, notes, and snippets.

@klonos
Created November 24, 2021 16:00
Show Gist options
  • Save klonos/d5677774aa70e6291bb39ed003c0c761 to your computer and use it in GitHub Desktop.
Save klonos/d5677774aa70e6291bb39ed003c0c761 to your computer and use it in GitHub Desktop.
Clone a Backdrop project; optionally fetching one or more PRs locally.
#!/usr/bin/env bash
##
# Clone a Backdrop project; optionally fetching one or more PRs locally.
#
PROJECT=$1
PRS="${@:2}" # Get all arguments after the 1st one.
PROJECT_TYPES=("module" "theme" "layout" "profile")
PROJECT_TYPE=`wget --quiet --output-document=- https://raw.githubusercontent.com/backdrop-contrib/${PROJECT}/HEAD/${PROJECT}.info | awk '/type/ {print $3}'`
if [[ "${PROJECT_TYPES[@]}" =~ "${PROJECT_TYPE}" ]]; then
if [[ $(basename $PWD) = "docroot" ]]; then
if [[ ${PROJECT_TYPE} = "profile" && ! -d "profiles" ]]; then
mkdir profiles
fi
cd ${PROJECT_TYPE}s
fi
else
echo "Project type could not be determined. Aborting."
return
fi
if [[ $(basename $PWD) = ${PROJECT_TYPE}s ]]; then
git clone https://github.com/backdrop-contrib/${PROJECT} ${PROJECT}
cd ${PROJECT}
BRANCH=$(git branch --show-current)
REPO_EXISTS=`curl -s https://api.github.com/repos/klonos/${PROJECT} | jq .name --raw-output`
if [[ ${REPO_EXISTS} = ${PROJECT} ]]; then
echo "Repo klonos/${PROJECT} already forked. Updating the fork and moving on..."
git remote add klonos https://github.com/klonos/${PROJECT}
git push -u klonos ${BRANCH}:${BRANCH} --force
else
echo "Repo klonos/${PROJECT} does not exit. Forking it afresh from backdrop-contrib/${PROJECT}..."
gh repo fork backdrop-contrib/${PROJECT} --remote=true --remote-name=klonos --clone=false
git remote add klonos https://github.com/klonos/${PROJECT}
fi
fi
if [[ ! -z "${PRS}" ]]; then
DEFAULT_BRANCH=`curl -s https://api.github.com/repos/backdrop-contrib/${PROJECT} | jq .default_branch`
# DEFAULT_BRANCH=$(git branch -rl '*/HEAD' | rev | cut -d/ -f1 | rev)
echo "default branch is: $DEFAULT_BRANCH"
for PR in $PRS; do
IS_MERGED=`curl -s https://api.github.com/repos/backdrop-contrib/${PROJECT}/pulls/${PR} | jq .merged`
PR_OWNER=`curl -s https://api.github.com/repos/backdrop-contrib/${PROJECT}/pulls/${PR} | jq .user.login --raw-output`
if [[ ${IS_MERGED} = null ]]; then
echo "PR #${PR} not found. Wrong PR number perhaps?"
elif [[ ${IS_MERGED} = "true" ]]; then
echo "PR #${PR} by ${PR_OWNER} already merged in backdrop-contrib/${PROJECT}. Skipping..."
elif [[ ${IS_MERGED} = "false" ]]; then
echo "Fetching PR #${PR} by ${PR_OWNER}..."
git fetch origin pull/${PR}/head:pr-${PR}-by-${PR_OWNER}
# Merge, accepting the default merge message (to avoid the default text
# editor prompt).
git merge pr-${PR}-by-${PR_OWNER} --no-edit
fi
done
fi
cd ../..
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment