Skip to content

Instantly share code, notes, and snippets.

@karancode
Created July 26, 2020 08:34
Show Gist options
  • Select an option

  • Save karancode/ef2c6c61f20de50fe721e7624a2ee509 to your computer and use it in GitHub Desktop.

Select an option

Save karancode/ef2c6c61f20de50fe721e7624a2ee509 to your computer and use it in GitHub Desktop.
Merge Github PRs using bash
#!/bin/bash
set -u -o pipefail
WORKSPACE_DIR=".github-workspace-for-merging-pr"
# export file with list of PRs
LIST_OF_PR_FILE=${LIST_OF_PR_FILE:-"list-of-prs"}
mkdir -p ${WORKSPACE_DIR}
if [ ! -f "${LIST_OF_PR_FILE}" ]; then
log "${LIST_OF_PR_FILE} not present. Exiting!"
exit 1
fi
# logger
function log(){
LOG_FILE="${WORKSPACE_DIR}/execution.log"
echo "[`date '+%b %e %H:%M:%S'`] ${*}" >> ${LOG_FILE}
}
# merge PR
function merge(){
log "starting to merge PRs in ${LIST_OF_PR_FILE}"
counter=0
while read pr_url;
do
repo_owner=$(echo ${pr_url} | cut -d '/' -f 4 )
repo_name=$(echo ${pr_url} | cut -d '/' -f 5 )
pr_to_merge_id=${pr_url##*/}
log "Merging PR for ${repo_name}, PR ID: ${pr_to_merge_id}."
curl \
-H "Authorization: token ${TOKEN}" \
-X PUT \
-s "https://api.github.com/repos/${repo_owner}/${repo_name}/pulls/${pr_to_merge_id}/merge"
log "Merged. Sleeping for 5s."
sleep 5
done < ${LIST_OF_PR_FILE}
}
merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment