Azure DevOps REST API 损坏的导入私有 Git 存储库

Azure DevOps REST API Broken Importing Private Git Repository

正在尝试使用 REST API:

将私有 GitHub 存储库导入 Azure DevOps

https://docs.microsoft.com/en-us/rest/api/azure/devops/git/import%20requests/create?view=azure-devops-rest-6.0

不出所料,文档不起作用。

我在 DevOps 项目中有一个基于 PAT 的服务端点,它可以访问我正在尝试导入的 GitHub 存储库。

我有以下重现问题的 PowerShell 片段

$headers = @{
    Authorization = "Bearer ****"
}
$org = '***'
$project = '***'
$targetRepositoryId = '***'
$sourceRepositoryUrl = '***'
$gitHubServiceEndpointId = '***'
irm https://dev.azure.com/$org/$project/_apis/git/repositories/$targetRepositoryId/importRequests?api-version=6.0-preview.1 -Method Post -Headers $headers -ContentType application/json -Body @"
{
  "parameters":  {
    "gitSource": {
      "overwrite":  false,
      "url":  "$sourceRepositoryUrl"
    },
    "serviceEndpointId":  "$gitHubServiceEndpointId",
    "deleteServiceEndpointAfterImportIsDone": false
  }
}
"@

抛出一个没有附加信息的 400 错误(错误请求)。

如果我创建 GitHub 存储库 public,完全相同的 API 请求和上面完全相同的代码可以正常工作。

我也 100% 确定服务端点可以访问相关存储库,因为我在 Azure DevOps 中有管道使用此服务端点从 GitHub.

克隆所述存储库

根据捕获的网络日志,创建服务连接时服务连接类型需要为“其他Git”,然后输入Git repository URL和在 GitHub:

中创建的个人访问令牌

有了这个服务连接ID,应该可以导入成功了。

只有当您使用 UsernamePassword 授权方案创建服务连接时,我才能使用 GitHub 服务连接进行导入工作——您不能从 DevOps 本身执行此操作,您必须从API:

irm "https://dev.azure.com/org/project/_apis/serviceendpoint/endpoints?api-version=6.0-preview.1" -Method Post -Headers $headers -ContentType application/json -Body @"
{
    "authorization": {
        "scheme": "UsernamePassword",
        "parameters": {
            "username": "foo",
            "password": "github access token"
        }
    },
    "name": "Test-5",
    "serviceEndpointProjectReferences": [
        {
            "description": "",
            "name": "Test-5",
            "projectReference": {
                "id": "project id",
                "name": "projectName"
            }
        }
    ],
    "type": "github",
    "url": "https://github.com",
    "isShared": false,
    "owner": "library"
}
"@