Update to be compatible with current version of Bitbucket API

This commit is contained in:
David Landry 2023-05-21 19:04:50 -04:00
parent 146948ca4f
commit bedb61aed6
2 changed files with 14 additions and 10 deletions

14
main.py
View file

@ -1,3 +1,4 @@
from base64 import b64encode
import props import props
import requests import requests
import json import json
@ -7,9 +8,12 @@ import re
logging.basicConfig(level=logging.INFO, filename='migration.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s') logging.basicConfig(level=logging.INFO, filename='migration.log', filemode='w', format='%(name)s - %(levelname)s - %(message)s')
BBauth = f"{props.migrationUsername}:{props.migrationPassword}"
headersBB = { headersBB = {
'Authorization': 'Bearer ' + props.authTokenBB 'Authorization': 'Basic ' + b64encode(BBauth.encode("utf-8")).decode("utf-8")
} }
headersGitea = { headersGitea = {
'Authorization': 'token ' + props.authTokenGitea, 'Authorization': 'token ' + props.authTokenGitea,
'Content-Type': 'application/json' 'Content-Type': 'application/json'
@ -92,9 +96,9 @@ def createGiteaOrganization(projectName, fullName, description):
def getReposBB(): def getReposBB():
""" Get all bitbucket repos and it's parameters: clone url, repo name, project name, description.""" """ Get all bitbucket repos and it's parameters: clone url, repo name, project name, description."""
BitBucketAPIurl = props.BitBucketURL + "/rest/api/1.0/repos?limit=1000" BitbucketAPIurl = f"{props.BitbucketURL}/2.0/repositories/{props.BitbucketWorkspace}?limit=1000"
try: try:
response = requests.get(BitBucketAPIurl, headers=headersBB) response = requests.get(BitbucketAPIurl, headers=headersBB)
response.raise_for_status() response.raise_for_status()
except Exception as e: except Exception as e:
print("Exception: %s \n Response: %s" % (e, response.text)) print("Exception: %s \n Response: %s" % (e, response.text))
@ -114,9 +118,9 @@ def getReposBB():
except KeyError: except KeyError:
description = 'None' description = 'None'
for cloneLinks in repo['links']['clone']: for cloneLinks in repo['links']['clone']:
if cloneLinks['name'] == "http": if cloneLinks['name'] == "https":
cloneLink = cloneLinks['href'] cloneLink = cloneLinks['href']
projectName = re.sub('[^A-Za-z0-9]+', '', projectName) projectName = f"{props.BitbucketWorkspace}_{re.sub('[^A-Za-z0-9]+', '', projectName)}"
print("Migrating: repo: %s with description: %s from project: %s via clonelink: %s" % (repoName, description, projectName, cloneLink)) print("Migrating: repo: %s with description: %s from project: %s via clonelink: %s" % (repoName, description, projectName, cloneLink))
logging.info("Migrating: repo: %s with description: %s from project: %s via clonelink: %s" % (repoName, description, projectName, cloneLink)) logging.info("Migrating: repo: %s with description: %s from project: %s via clonelink: %s" % (repoName, description, projectName, cloneLink))

View file

@ -1,5 +1,5 @@
BitBucketURL='https://bitbucket.example.com' BitbucketURL='https://api.bitbucket.org'
authTokenBB='bitbucket auth token' BitbucketWorkspace='your-workspace'
GiteaURL='https://gitea.example.com' GiteaURL='https://gitea.example.com'
authTokenGitea='gitea auth token' authTokenGitea='gitea auth token'
migrationUsername='user@example.com' migrationUsername='user@example.com'