Skip to content

Instantly share code, notes, and snippets.

@pwtyler
Last active May 9, 2023 17:37
Show Gist options
  • Save pwtyler/42085ef1922c1e864bc163d3f517db01 to your computer and use it in GitHub Desktop.
Save pwtyler/42085ef1922c1e864bc163d3f517db01 to your computer and use it in GitHub Desktop.
A bash script to copy commits from a fork to a brach to run CI when fork builds are disabled.
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
###
#
# Requires: git, gh (https://cli.github.com/)
# Usage: bash test-fork-pr.sh "${REPO}" "${PR_ID}"
# example: bash test-fork-pr.sh pantheon-systems/wp-redis 100
#
###
REPO=${1:-}
PULL_REQUEST_ID=${2:-}
if [[ -z "${REPO}" || -z "${PULL_REQUEST_ID}" ]]; then
echo "usage: $0 PULL_REQUEST_ID"
exit 1
fi
echo "Working on PR $PULL_REQUEST_ID of ${REPO}"
WORKDIR=/tmp/work
PROJECT="${REPO#*/}"
echo ${WORKDIR}
echo "${WORKDIR}/${PROJECT}"
# Clone the project if necessary
if [ ! -d "${WORKDIR}/${PROJECT}" ] ; then
mkdir -p "${WORKDIR}"
(
cd "${WORKDIR}" && gh repo clone "${REPO}"
)
fi
cd "${WORKDIR}/${PROJECT}"
pwd
TEST_BRANCH_NAME="TEST-FORK-PR-$PULL_REQUEST_ID"
gh pr checkout "${PULL_REQUEST_ID}"
# TODO: This checkout fails if the branch already exists.
git checkout -b "$TEST_BRANCH_NAME"
git push origin "${TEST_BRANCH_NAME}"
open "https://github.com/"${REPO}"/tree/${TEST_BRANCH_NAME}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment