Last active
December 27, 2018 15:58
-
-
Save a4sh3u/c70bd4f9dcef6aafe785371e4713043d to your computer and use it in GitHub Desktop.
bitbucket_cleanup.sh
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 | |
| MAIN_URL='https://bitbucket.smava.de/rest/api/1.0/projects' | |
| CURL_GET_CMD="curl -s -H 'X-Atlassian-Token: no-check' -u${bitbucket_user}:${bitbucket_password}" | |
| PROJECTS=$(${CURL_GET_CMD} "${MAIN_URL}" | jq .values[].key | sed 's/"//g') | |
| echo "PROJECT GIT-REPO PULL-REQUEST-TITLE LAST-UPDATED-DATE" > /tmp/decline_prlist | |
| echo "PROJECT GIT-REPO PULL-REQUEST-TITLE LAST-UPDATED-DATE" > /tmp/delete_prlist | |
| echo "PROJECT GIT-REPO BRANCH-NAME LAST-UPDATED-DATE" > /tmp/delete_branchlist | |
| for project in ${PROJECTS}; do | |
| repos=$(${CURL_GET_CMD} "${MAIN_URL}/${project}/repos" | jq .values[].slug | sed 's/"//g') | |
| for repo in ${repos}; do | |
| pullrequests=$(${CURL_GET_CMD} "${MAIN_URL}/${project}/repos/${repo}/pull-requests" | jq .values[].id) | |
| for pullrequest in ${pullrequests}; do | |
| pullrequestjson=$(${CURL_GET_CMD} "${MAIN_URL}/${project}/repos/${repo}/pull-requests/${pullrequest}") | |
| pullrequestversion=$(echo ${pullrequestjson} | jq .version) | |
| pullrequesttitle=$(echo ${pullrequestjson} | jq .title) | |
| pullrequestlastupdateddate=$(echo ${pullrequestjson} | jq .updatedDate) | |
| echo "$pullrequestjson" | |
| pullrequestlastupdateddate=$(date -d @$(($pullrequestlastupdateddate/1000))) | |
| dayssincelastcommit=$[$[$(date +%s)-$(date -d "${pullrequestlastupdateddate}" +%s)]/60/60/24] | |
| if [ $dayssincelastcommit -gt 7 ]; then | |
| echo "${project} ${repo} ${pullrequesttitle} $(date --date="$pullrequestlastupdateddate" '+%d/%m/%y')" >> /tmp/decline_prlist | |
| fi | |
| if [ $dayssincelastcommit -gt 30 ]; then | |
| echo "${project} ${repo} ${pullrequesttitle} $(date --date="$pullrequestlastupdateddate" '+%d/%m/%y')" >> /tmp/delete_prlist | |
| fi | |
| done | |
| done | |
| done | |
| for project in ${PROJECTS}; do | |
| repos=$(${CURL_GET_CMD} "${MAIN_URL}/${project}/repos" | jq .values[].slug | sed 's/"//g') | |
| for repo in ${repos}; do | |
| branches=$(${CURL_GET_CMD} "${MAIN_URL}/${project}/repos/${repo}/branches" | jq .values[].id | sed 's/"//g') | |
| for branch in ${branches}; do | |
| if [[ ${branch} != 'refs/heads/master' && ${branch} != 'refs/heads/develop' ]]; then | |
| latestcommitdate=$(${CURL_GET_CMD} "${MAIN_URL}/${project}/repos/${repo}/commits?until=${branch}&limit=0&start=0" | jq .values[0].committerTimestamp) | |
| latestcommitdate=$(date -d @$(($latestcommitdate/1000))) | |
| dayssincelastcommit=$[$[$(date +%s)-$(date -d "${latestcommitdate}" +%s)]/60/60/24] | |
| if [ $dayssincelastcommit -gt 90 ]; then | |
| echo "${project} ${repo} ${branch:11} $(date --date="$latestcommitdate" '+%d/%m/%y')" >> /tmp/delete_branchlist | |
| fi | |
| fi | |
| done | |
| done | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment