Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Managing Stale Repos and Branches

Is there a way to determine what branches and repositories are not being used so we can slate them for decommission? When we migrated from GitLab to Stash we had nearly 100 repos that were little labs that people created and just left untouched. 

I'd like to have a way to find repositories or branches that have not had commits to them in the past, say six months. Is there a tool for this or has anyone used Stash's APIs to accomplish something like this?

1 answer

4 votes
Marcin Atlassian Team Dec 10, 2014

This can be done with the REST API and a script - basically the steps are:

  • retrieve all the repositories
  • for each repository get the branches ordered by modification date (most recent first)
  • get the latest commit on the top branch - that should be the most recent commit to the repo

I've put together a small Python script that does this - the only dependency is the Requests library for easier HTTP. You'll need to change some variables at the top to match your environment.

import requests
import datetime
 
STASH_URL = 'http://localhost:7990/stash'
USERNAME = 'admin'
PASSWORD = 'admin'
auth = (USERNAME, PASSWORD)

THRESHOLD = datetime.datetime.now() - datetime.timedelta(weeks=26) # only show repos with commits older than half a year ago

repos = requests.get(STASH_URL + '/rest/api/1.0/repos?limit=9999', auth=auth)
values = repos.json()['values']
print("Searching %d repos..." % len(values))
for repo in values:
  repo = (repo['project']['key'], repo['slug'])
  branches_request = requests.get(STASH_URL + '/rest/api/1.0/projects/%s/repos/%s/branches?orderBy=MODIFICATION&limit=1' % repo, auth=auth)
  branches = branches_request.json()['values']
  if len(branches) > 0:
    most_recent_branch = branches[0]
    most_recent_commit = most_recent_branch['latestChangeset']
    commit_request = requests.get(STASH_URL + '/rest/api/1.0/projects/%s/repos/%s/commits/%s' % (repo + (most_recent_commit,)), auth=auth)
    commit = commit_request.json()
    commit_date = datetime.datetime.fromtimestamp(commit['authorTimestamp'] / 1000)
    if commit_date <= THRESHOLD:
      print("Most recent commit in repo %s in project %s was at %s by %s (%s)" % (repo + (commit_date, commit['author']['name'], commit['message'])))

The branches_request list does not support 'values' key . It is throwing the error it key should be an integer not a string. Stuck there.

Will this script works for Bitbucket to find stale branches and unused Bitbucket repos ? we are using Bitbucket cloud

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Bitbucket

New improvements to user management in Bitbucket Cloud 👥

Hey Community! We’re willing to wager that quite a few of you not only use Bitbucket, but administer it too. Our team is excited to share that we’ll be releasing improvements throughout this month of...

3,845 views 10 16
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you