Azure cli 存储库导入失败,操作返回 400 状态代码

Azure cli repository import fails with Operation returned a 400 status code

What specific syntax and/or configuration needs to be changed below in order for an Azure CLI command to successfully import a private source repo from GitHub into a private destination repo in Azure Repos?

当前命令和错误响应:

C:\path\to\working\directory>az repos import create --git-source-url https://github.com/ValidGitHubOrgName/valid-gh-repo-name --repository ValidAzureReposDestinationRepoName --organization https://dev.azure.com/ValidOrgName --project ValidProjectName --requires-authorization  
Git Password / PAT:  
Confirm Git Password / PAT:  
ClientRequestError: Operation returned a 400 status code.  

什么有效:

当我们使用上面的方法将 public 存储库从 GitHub 导入私有 Azure Repos 存储库时,上面的操作没有错误地完成我们当然删除了 --requires-authorization 标志。

失败的原因:

然而,当我们尝试使用上面显示的完整语法导入 private GitHub 存储库时,上述操作失败。

我们不仅使用有效的 GitHub 密码进行了尝试,还使用了我们在如上所示提示时提供的有效 Azure DevOps 个人访问令牌进行了尝试。

Do we need to add configuration in order for the import to work without error?
And also, how can we make sure the import command executes without an interactive request of credentials?

这需要 运行 在没有交互式人类用户的情况下实现自动化。

文档 here 似乎没有包含我们需要的说明。

与 GitHub 集成(私有)当前的 az cli 没有选择密码的选项,因此向您显示交互式凭据输入,因此建议设置环境变量 AZURE_DEVOPS_EXT_GIT_SOURCE_PASSWORD_OR_PAT 作为您的 batch/shell 脚本的一部分 它会起作用,建议问题已打开并根据 https://github.com/Azure/azure-devops-cli-extension/issues/1050

进行处理

我也可以在我这边重现您的问题,要使用 Azure CLI 通过非交互方式将私有 GitHub 存储库导入 DevOps,请按照以下步骤操作。

1.Navigate 到 devops 门户中的 Project Settings -> Service connections -> New service connection -> Other Git(不是 GitHub) -> 使用您的 github 用户名和密码创建连接。

2.Use REST API - Endpoints - Get Service Endpoints By Names or CLI az devops service-endpoint list获取服务连接,然后记下id(不只有一个id,使用正确的如下)。

3.Then 运行 命令如下,将步骤 2 中的 id 传递给 --git-service-endpoint-id,还要确保您已经创建了 devops 存储库并且它是空的。

az repos import create --git-source-url https://github.com/Joyw1/privaterepo.git --repository testrep6 --git-service-endpoint-id 3c04f8a9-43c8-40cb-baf5-90faf292b9ab --organization https://dev.azure.com/xxxx --project testpro1 --requires-authorization

查看结果:

更新:

要以自动方式创建 Other Git 服务连接,您可以 powershell 直接调用 REST API - Endpoints - Create

样本:

$MyPat = '<your devops PAT>'
$B64Pat = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(":$MyPat"))

$header = @{
    'Authorization' = 'Basic ' + $B64Pat
    'Content-Type' = 'application/json'
}

$body = '{
  "data": {},
  "name": "MyNewServiceEndpoint",
  "type": "git",
  "url": "https://github.com/Joyw1/privaterepo.git",
  "authorization": {
    "parameters": {
      "username": "joyw1",
      "password": "xxxxxx"
    },
    "scheme": "UsernamePassword"
  },
  "isShared": false,
  "isReady": true,
  "serviceEndpointProjectReferences": [
    {
      "projectReference": {
        "id": "53f719f7-2968-4035-8e56-163f239b4161",
        "name": "testpro1"
      },
      "name": "MyNewServiceEndpoint"
    }
  ]
}'

Invoke-RestMethod -Method Post -Uri https://dev.azure.com/v-joyw/_apis/serviceendpoint/endpoints?api-version=6.0-preview.4 -Headers $header -Body $body