错误 400:通过 REST API 将 GitLab 存储库导入 Azure DevOps 时出现“错误请求”

问题描述 投票:0回答:1

我正在尝试使用 REST API 将 GitLab 存储库导入到 Azure DevOps 中。但是,我在此过程中遇到了 400 Bad Request 错误。下面是代码的核心部分:

python代码:

payload = {
"authorization":{"scheme":"UsernamePassword","parameters":{"username":"{User name}","password":"{github access token }"}},
"data":{"accessExternalGitServer":"true"},
"name":"{name}",
"serviceEndpointProjectReferences":[{"description":"","name":"{Service connection name}","projectReference":{"id":"{Project Id}","name":"{Project Name}"}}],
"type":"git",
"url":"{Target Git URL}",
"isShared":false,
"owner":"library"

}

    service_endpoint_url = f"https://dev.azure.com/{ORGANIZATION_NAME}/{project_name}/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4"

    headers = {"Content-Type": "application/json"}

    response = requests.post(
        service_endpoint_url,
        auth=HTTPBasicAuth("", PERSONAL_ACCESS_TOKEN),
        headers=headers,
        data=json.dumps(payload),
        verify=False,
    )

connection_id = response.json()["id"]

# Prepare import request
import_url = f"https://dev.azure.com/{ORG_NAME}/{PROJECT}/_apis/git/repositories/{REPO_NAME}/importRequests?api-version=7.1-preview.1"

import_payload = {
    "parameters": {
        "gitSource": {
            "url": GITLAB_REPO_URL,
            "overwrite": False,
        
        },
        "serviceEndpointId": connection_id,
        "deleteServiceEndpointAfterImportIsDone": True
    }
}

headers = {
    "Content-Type": "application/json"
}

# Perform repository import
import_response = requests.post(
    import_url,
    auth=HTTPBasicAuth("", AZURE_DEVOPS_PAT),  # Placeholder for Azure DevOps PAT
    headers=headers,
    data=json.dumps(import_payload),
    verify=False
)

错误:

400 客户端错误:URL 请求错误:https://dev.azure.com///_apis/git/repositories//importRequests?api-version=7.1-preview.1

我尝试过的事情:

GitLab 存储库 URL (GITLAB_REPO_URL) 正确且可公开访问。 Azure DevOps 个人访问令牌 (AZURE_DEVOPS_PAT) 具有存储库导入和相关操作的完全权限。 存储库名称 (ADO_REPO_NAME) 和项目名称 (PROJECT_NAME) 存在并可在 Azure DevOps 中访问。 GITLAB_REPO_URL 的格式为:https://gitlab.example.com//.git。 我正在根据 Microsoft 文档使用适当的 API 版本 (7.1-preview.1)。 添加服务连接

python azure azure-devops gitlab azure-devops-rest-api
1个回答
0
投票

我在此过程中遇到 400 Bad Request 错误。

问题在于发送到 Azure DevOps 的 REST API 的有效负载的格式或内容。

在这里,我按照以下步骤在我的环境中复制了相同的内容。

下面是在 Azure DevOps 中创建/生成服务端点的代码。

create_service_endpoint.py:

import requests
from requests.auth import HTTPBasicAuth
import json

# Load configurations from the config file
with open("config.json", "r") as config_file:
    config = json.load(config_file)

AZURE_ORGANIZATION = config["azure_organization"]
AZURE_PROJECT = config["azure_project"]
AZURE_DEVOPS_PAT = config["azure_devops_pat"]
GITLAB_REPO_URL = config["gitlab_repo_url"]
GITLAB_USERNAME = config["gitlab_username"]
GITLAB_PERSONAL_ACCESS_TOKEN = config["gitlab_personal_access_token"]
SERVICE_ENDPOINT_NAME = config["service_endpoint_name"]

# Service Endpoint API URL
service_endpoint_url = f"https://dev.azure.com/{AZURE_ORGANIZATION}/{AZURE_PROJECT}/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4"

# Service Endpoint Payload
service_endpoint_payload = {
    "authorization": {
        "scheme": "UsernamePassword",
        "parameters": {
            "username": GITLAB_USERNAME,
            "password": GITLAB_PERSONAL_ACCESS_TOKEN
        }
    },
    "data": {"accessExternalGitServer": "true"},
    "name": SERVICE_ENDPOINT_NAME,
    "serviceEndpointProjectReferences": [
        {
            "description": "",
            "name": SERVICE_ENDPOINT_NAME,
            "projectReference": {
                "id": AZURE_PROJECT,
                "name": AZURE_PROJECT
            }
        }
    ],
    "type": "git",
    "url": GITLAB_REPO_URL,
    "isShared": False,
    "owner": "library"
}

# Request Headers
headers = {"Content-Type": "application/json"}

# Create Service Endpoint
response = requests.post(
    service_endpoint_url,
    auth=HTTPBasicAuth("", AZURE_DEVOPS_PAT),
    headers=headers,
    data=json.dumps(service_endpoint_payload),
    verify=False
)

# Output Results
if response.status_code in [200, 201]:
    service_endpoint_id = response.json()["id"]
    print(f"Service Endpoint created successfully. ID: {service_endpoint_id}")
else:
    print(f"Failed to create Service Endpoint. Status Code: {response.status_code}")
    print(response.text)

在 Azure DevOps 中生成具有服务连接代码(读取和写入)权限的 PAT。

enter image description here

这是我的 Gitlab 存储库,我还需要在 Gitlab 中生成 PAT。

enter image description here

enter image description here

服务端点创建成功,并返回其ID。

存储库导入请求成功,存储库已导入到 Azure DevOps 中。

Service Endpoint created successfully.
Response:
{
    "id": "12345678-abcd-1234-efgh-1234567890ab",
    "name": "GitLab Service Connection",
    "type": "git",
    "url": "https://gitlab.com/XXXXXxxx/XXXXX.git",
    "authorization": {
        "scheme": "UsernamePassword",
        "parameters": {
            "username": "your-gitlab-username"
        }
    }
}

HTTP 202 已接受

Repository import initiated successfully.
Response:
{
    "id": 42,
    "status": "queued",
    "repository": {
        "id": "abcdef12-3456-7890-abcd-ef1234567890",
        "name": "XXXXXXXXX",
        "url": "https://dev.azure.com/MyOrg/MyProject/_git/Stackoverflow"
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.