Shashank
Shashank

Reputation: 13

Gitlab Repo importing in Azure devops using Rest api

I created a service connection in Azure DevOps to connect to GitLab using the REST API. Here's the payload I used: Payload:

payload = {
    "authorization":{"scheme":"UsernamePassword","parameters":{"username":"","password":f"{git personal access token}"}},
    "data":{"accessExternalGitServer":"true"},
    "name":"",
    "serviceEndpointProjectReferences":[{"description":"","name":"GitLabServiceConnection","projectReference":{"id":f"{project_id}","name":f"{project_name}"}}],
    "type":"git",
    "url":f"{gitlab_url}",
    "isShared":False,
    "owner":"library"
  }

enter image description here

The service connection was created successfully. However, when I attempted to import a GitLab repository using this service connection, I received a 400 Bad Request error. Below is the payload for the repository import:

{
                "parameters": {
                    "gitSource": {
                        "url": f"{gitlabURL}",
                        "overwrite": False},
                "serviceEndpointId": f"{service_connection_id}",
                "tfvcSource": None,
                "deleteServiceEndpointAfterImportIsDone": True
                            }
                            }

Error:

400 Client Error: Bad Request for url

What could be causing this error? Is there an issue with the payload or the setup of the service connection? Any help would be appreciated.

Upvotes: 0

Views: 65

Answers (1)

Suresh Chikkam
Suresh Chikkam

Reputation: 3448

The service connection was created successfully. However, when I attempted to import a GitLab repository using this service connection, I received a 400 Bad Request error.

Even though the service connection was created successfully, check it has the correct type (git) and is correctly linked to the project.

There is a chance of permissions issue from the GitLab side as if the repository is private. configure the PAT settings enable for read_repository for basic access; add write_repository if overwriting.

Payload for importing a repository:

payload = {
    "parameters": {
        "gitSource": {
            "url": "https://gitlab.com/username/repository.git",  # URL of the GitLab repo
            "overwrite": False  # Set to True if you want to overwrite the destination
        },
        "serviceEndpointId": "service_connection_id",  # Replace with the actual ID
        "deleteServiceEndpointAfterImportIsDone": False,  # Keep the service connection
    }
}

Repository import in Azure DevOps:

import requests
from requests.auth import HTTPBasicAuth

# Azure DevOps and GitLab details
organization = "your_organization"
project = "your_project"
pat = "your_azure_devops_pat"
service_connection_id = "your_service_connection_id"
gitlab_repo_url = "https://gitlab.com/username/repository.git"

# API endpoint
url = f"https://dev.azure.com/{organization}/{project}/_apis/git/repositories/importRequests?api-version=7.1-preview.1"

# Payload
payload = {
    "parameters": {
        "gitSource": {
            "url": gitlab_repo_url,
            "overwrite": False
        },
        "serviceEndpointId": service_connection_id,
        "deleteServiceEndpointAfterImportIsDone": False
    }
}

# Make the request
response = requests.post(
    url,
    json=payload,
    headers={"Content-Type": "application/json"},
    auth=HTTPBasicAuth('', pat)
)

# Check response
if response.status_code == 200:
    print("Repository imported successfully!")
else:
    print(f"Error: {response.status_code} - {response.text}")
  • Fields such as tfvcSource are not required in this context. Removing unnecessary fields may resolve the issue.

enter image description here

Azure DevOps PAT has the Repositories and Service Connections (Read & Manage) scopes.

Upvotes: 0

Related Questions