Skip to content

Instantly share code, notes, and snippets.

@estahn
Created December 2, 2012 07:56
Show Gist options
  • Save estahn/4187655 to your computer and use it in GitHub Desktop.
Save estahn/4187655 to your computer and use it in GitHub Desktop.
Remove branches that belong to closed tickets
#!/bin/bash
# Base Repository URL
SVN_URL=http://svn-repo-url
# Atlassian Jira URL
JIRA_URL=https://jira-url
# Jira Project Key
PROJECT=project-key
# Jira and SVN Username
USERNAME=jira-svn-username
# Jira and SVN Password
PASSWORD=jira-svn-password
# Return a list of existing branches prefixed with an jira issue key
BRANCHES=$(svn list --username $USERNAME --password $PASSWORD $SVN_URL/$PROJECT/branches | grep "$PROJECT-[0-9]\+" | xargs -n1 basename)
REPORT_ALL="| %-18.18s | %-13.13s | %-35.35s | %-10.10s |\n"
REPORT_CLOSED="| %-18.18s | %-13.13s | %-35.35s | \e[00;32m%-10.10s\e[00m |\n"
printf "$REPORT_ALL" "BRANCH" "ISSUE KEY" "SUMMARY" "STATUS"
for BRANCH in $BRANCHES; do
ISSUE_KEY=$(perl -pe 's|($PROJECT-[0-9]+).*|\1|' <<< $BRANCH)
# Request ticket information
TICKET=$(curl -s -u $USERNAME:$PASSWORD -H "Content-Type: application/json" $JIRA_URL/rest/api/2/issue/$ISSUE_KEY?fields=summary,status)
TICKET_STATUS=$(jshon -Q -e fields -e status -e name -u <<< $TICKET)
TICKET_SUMMARY=$(jshon -Q -e fields -e summary -u <<< $TICKET)
if [ "$TICKET_STATUS" == 'Closed' ] ; then
printf "$REPORT_CLOSED" ${BRANCH} "$ISSUE_KEY" "$TICKET_SUMMARY" "$TICKET_STATUS"
svn delete --username $USERNAME --password $PASSWORD -m "$ISSUE_KEY $TICKET_SUMMARY [CLOSED]" $SVN_URL/$PROJECT/branches/$BRANCH
else
printf "$REPORT_ALL" "$BRANCH" "$ISSUE_KEY" "$TICKET_SUMMARY" "$TICKET_STATUS"
fi
done
@sayaliM0412
Copy link

sayaliM0412 commented May 3, 2023

Thanks to this post but..
Not sure if anyone else would need this, but I did something similar for Github-Jira in GitHub Actions and then use Reusable workflows for other repositories

name: Loop through branches
        run: |
          for branch in $(git for-each-ref --format='%(refname:short)' refs/remotes/); do
            issue_key=$(echo $branch | grep -o 'DAT-[a-zA-Z0-9_-]*')
            if [ ! -z "$issue_key" ]; then
              jira_status=$(curl -s -u ORG_USER@ORG.com:${{ secrets.ORG_USER_API_TOKEN }} https://YOUR_ORG.atlassian.net/rest/api/2/issue/$issue_key?fields=status | jq -r '.fields.status.statusCategory.name') | awk '{print tolower($0)}'
              if [ "$jira_status" == "done" ]; then
                echo "Removing branch $branch because Jira task $issue_key is in Done status."
                git push origin --delete $branch
              fi
            fi
          done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment