1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why do I keep getting a 400 Bad Request trying to import a private GitHub repo into...

Discussão em 'Python' iniciado por Stack, Setembro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make API calls to import a repo from a private GitHub repo to an Azure Devops repo in Python using a PAT. I tried using the SDK with no luck. My code DOES work with a public GitHub repo, just not a private one. I have tried adding the username/PAT in the GitHub repo url and tried creating my own service connection to no avail. It will always give me a 400 Bad Request with no text, no body to diagnose what's wrong with it.

    Below is the code on my final try to get this working, which follows the exact API calls that are made when you manually import a repo from a private GitHub repo using the UI.

    def create_github_service_connection(connection: Connection, project_id, github_pat, github_username):
    service_auth = EndpointAuthorization(
    scheme="UsernamePassword",
    parameters={"password": github_pat, "username":github_username}
    )

    svc_endpoint_client = connection.clients.get_service_endpoint_client()
    endpoint = ServiceEndpoint(
    name="github-conn-scaffold",
    type="git",
    url="https://github.com/org/repo",
    authorization=service_auth,
    service_endpoint_project_references=[
    ServiceEndpointProjectReference(
    name="github-conn-scaffold",
    project_reference=ProjectReference(id=project_id)
    )
    ],
    )

    created_svc = svc_endpoint_client.create_service_endpoint(endpoint)
    return created_svc.id

    def initialize_repo(connection: Connection, project_name: str, repo_name: str, source_url, azure_devops_pat, github_pat, github_username):
    try:
    core_client = connection.clients.get_core_client()
    project = core_client.get_project(project_name)
    repo_client = connection.clients.get_git_client()

    repos = repo_client.get_repositories(project=project_name)
    repo = next((r for r in repos if r.name.lower() == repo_name.lower()), None)
    if not repo:
    raise Exception(f"Repository '{repo_name}' not found in project '{project_name}'.")

    uri_encoded_project = quote(project_name)
    repository_url = f"{connection.base_url}/{uri_encoded_project}/_apis/git/repositories/{repo.id}/importRequests"

    repository_body = {
    "parameters": {
    "deleteServiceEndpointAfterImportIsDone":True,
    "gitSource": { "overwrite":False, "url" : source_url},
    "tfvcSource":None,
    "serviceEndpointId":create_github_service_connection(connection, project.id, github_pat, github_username)
    }
    }

    pprint(repository_body)
    response = requests.post(url=repository_url, json=repository_body, headers={"Content-Type": "application/json", "Accept":"api-version=5.0-preview.1"}, auth=HTTPBasicAuth('', azure_devops_pat))

    pprint(f"test: {response.text} ")
    response.raise_for_status()
    logger.info(f"Initiated repo for {project_name} with repoId {repo.id}")
    except Exception as e:
    logger.error("An error occurred while initializing the repository: %s", e)
    sys.exit(1)




    Checklist on what I've done:

    • I've tried making a github type service connection with a personalaccesstoken auth type. This still wont work. The UI API calls create the git type service connection.
    • I've tried different versions of the API including 7.1. The version I have in my code is what the UI uses.
    • I've tried using the username/password combo for the URL e.g https://blahUsername:longpathere:github.com/orgname/project

    Some notes:


    Continue reading...

Compartilhe esta Página