How to bulk rename Bitbucket Repos (Quick and dirty)

I have a ton of old code on my dorment Bitbucket account and I have to move all my new code onto Bitbucket because I am decommisioning my personal Gitlab server. (too risky to self-host Git… losing all my source code would be really bad.)

To make repositories easier to identify, I want to bulk rename them and prefix the old repos with "zz_" indicating they are old code.

The first step is to get a list of all repositories. The quickest way is to use Postman and make an authenticated GET request to https://api.bitbucket.org/2.0/repositories/danobot?pagelen=100. (Use basic auth with your login credentials or create an App Password) This returns a ton of information about each of your repositories. (for 44 repos this is 4000 lines of formatted JSON).

All i need is the "slug" attribute, so the quickest way to get this is to coipy the response into VS Code and use the Text Power Tools extension to delete all lines NOT containig the word "slug". Press Ctrl + Shift + P, type filter and select "Text Power Tools: Filter lines not including string". Type slug and hit Enter.

You now have the "slug" attributes and nothing else. Use multi cursor and column editing to convert this string into a Python array.

Copy and paste into the program below and run.

Bulk Rename Script

import requests
repos = [
  'your_repo_slugs_here'
]

import requests

for r in repos :
  url = "https://api.bitbucket.org/2.0/repositories/danobot/"+r

  payload = "{\n\t\"name\": \"zz_"+r+"\"\n}"
  headers = {
      'Content-Type': "application/json",
      'Authorization': "Basic YOUR_CREDENTIAL_HERE",
      'User-Agent': "PostmanRuntime/7.15.0",
      'Accept': "*/*",
      'Cache-Control': "no-cache",
      'Postman-Token': "ff50db59-409b-407c-afd7-c13ebe5e46ab,cd70e757-f923-478a-aa05-12e1951dd66a",
      'Host': "api.bitbucket.org",
      'accept-encoding': "gzip, deflate",
      'content-length': "33",
      'Connection': "keep-alive",
      'cache-control': "no-cache"
      }

  response = requests.request("PUT", url, data=payload, headers=headers)

  print(response.text)

Donesies.

Related posts

How to Optimize Docker Builds with Nexus OSS for Apt, Maven, Docker and NPM Dependencies

Docker could not find an available, non-overlapping IP address pool

Github Workflow for Electron React Boilerplate with Auto Updates

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More