Created
June 10, 2019 19:12
-
-
Save jackdpeterson/2699436f7cc4cf8a4d9192d1cd23b6e6 to your computer and use it in GitHub Desktop.
clean up closed PRs in a Jenkins multibranch pipeline environment. Enable as a CRON job
This file contains hidden or 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
#!/usr/bin/env bash | |
#set -x | |
GITHUB_ACCESS_TOKEN=TOKEN_GOES_HERE | |
GITHUB_USERNAME=USERNAME_GOES_HERE | |
GITHUB_ORGANIZATION=com.example | |
GITHUB_REPOSITORY=microservice | |
## Jenkins project name | |
PROJECT="com.example.microservice" | |
function clean_closed_prs { | |
local PAGE; | |
local CURRENT_RESPONSE_SIZE; | |
PAGE_SIZE=100; | |
PAGE=1 | |
CURRENT_RESPONSE_SIZE=${PAGE_SIZE}; | |
RESPONSE="" | |
while [[ ${CURRENT_RESPONSE_SIZE} -eq ${PAGE_SIZE} ]]; do | |
echo >&2 "requesting page: ${PAGE}" | |
CURRENT_RESPONSE=$(curl -vs -u ${GITHUB_USERNAME}:${GITHUB_ACCESS_TOKEN} "https://api.github.com/repos/${GITHUB_ORGANIZATION}/${GITHUB_REPOSITORY}/pulls?state=closed&page=${PAGE}&per_page=${PAGE_SIZE}" 2>/dev/null | jq -r '.[] .url' | cut -d '/' -f 8) | |
CURRENT_RESPONSE_SIZE=$(echo -e "${CURRENT_RESPONSE}" | wc -l) | |
echo >&2 "Response size: ${CURRENT_RESPONSE_SIZE}" | |
PAGE=$(($PAGE+1)) | |
RESPONSE=$(echo -e "${RESPONSE}\n${CURRENT_RESPONSE}") | |
for CLOSED_PR in ${RESPONSE}; do | |
if [ -z "${CLOSED_PR}" ]; then | |
continue; | |
fi | |
CLEAN_DIR_CMD="rm -rf /var/lib/jenkins/workspace/${PROJECT}-${CLOSED_PR}*" | |
$($CLEAN_DIR_CMD) | |
done | |
done; | |
} | |
clean_closed_prs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment