Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Can we get all remote branches in a Repo which are not updated for more than 180 days?

sudheer kommi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 19, 2021

I would like to leverage Bitbucket api to get all remote branches(including merged and unmerged) in a repo which have not been updated for more than 180 days

1 answer

0 votes
Michael Walker
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
October 21, 2021

Hey Sudheer! It definitely looks like what you're wanting to do is possible with just 2 API endpoints. You'd need to first iterate through the values (repos) provided from the /2.0/repositories/{workspace} endpoint to get a list of your repos and their respective repo-slugs. Once you've got that, you can pass the slug into the /2.0/repositories/{workspace}/{repo-slug}/refs/branches endpoint to get details on the last updated timestamp. Then you simply need to compare the timestamps with whatever date/time you wish.

At a super high level this would look like the following in python:

def repos():
    # https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D
    yield repo.get('name), repo.get('slug')
def branches(repo):
    # https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Bworkspace%7D/%7Brepo_slug%7D/refs/branches
    yield branch.get('name'), branch.get('target').get('date')
def main():
    for repo_name, repo_slug in repos():
        for branch_name, branch_date in branches(repo_slug):
            if branch_date < desired_date: #less than == older than
                print(f'{branch_name} on {repo_name} is older than desired.')



Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events