Skip to content

Instantly share code, notes, and snippets.

@oleg-nenashev
Last active June 19, 2017 17:02
Show Gist options
  • Save oleg-nenashev/ec7dea01dcf272e9b891 to your computer and use it in GitHub Desktop.
Save oleg-nenashev/ec7dea01dcf272e9b891 to your computer and use it in GitHub Desktop.
Simple git merge with squash for Jenkins repos
#!/bin/bash -ex
REPO_NAME=${PWD##*/}
TARGET_ORG="jenkinsci"
#TODO: fetch from GitHib API
#TODO: if no, process parameters correctly
GITHUB_PR_NUMBER=${1}
FROM_USER=${2}
BRANCH=${3}
MESSAGE=${4}
echo "Merging ${FROM_USER}/${REPO_NAME}, branch ${BRANCH}"
GITHUB_USER_REPO_URL="https://github.com/${FROM_USER}/${REPO_NAME}"
GITHUB_TARGET_REPO_URL="https://github.com/${TARGET_ORG}/${REPO_NAME}"
LOCAL_REMOTE="jenkins-merge_${FROM_USER}_${BRANCH}"
LOCAL_BRANCH="${LOCAL_REMOTE}/${BRANCH}"
GITHUB_PR_URL="${GITHUB_TARGET_REPO_URL}/pull/${GITHUB_PR_NUMBER}"
# Init repo for merge
git remote remove "${LOCAL_REMOTE}" || res=$?
git remote add "${LOCAL_REMOTE}" "${GITHUB_USER_REPO_URL}"
git fetch "${LOCAL_REMOTE}"
# Extract data
PR_AUTHOR=$(git log "${LOCAL_BRANCH}" | sed -n '2p' | sed "s/^\s*Author:\s*//")
# Perform merge
git merge --squash "${LOCAL_BRANCH}"
git commit -a --author="${PR_AUTHOR}" --message="${MESSAGE}.
This pull request integrates ${GITHUB_PR_URL}"
# Cleanup
#TODO: It's unreliable, of course
git remote remove "${LOCAL_REMOTE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment