【问题标题】:Azure DevOps import Git repositories requiring authorization via APIAzure DevOps 导入需要通过 API 授权的 Git 存储库
【发布时间】:2019-07-06 18:17:41
【问题描述】:

我能够使用记录在 here 的 Azure DevOps API 导入公共 Git 存储库。

但是我无法处理需要使用相同 API 进行授权的存储库。

我查看了使用 UI 时发送的请求,并尝试通过按以下方式提交各种请求来进行逆向工程:

{
  "parameters": {
    "gitSource": {
      "url": "[URL]",
      "username": "[USERNAME]",
      "password": "[PASSWORD]"
    }
  }
}

...但似乎没有任何效果。没有关于如何为 Git 导入传递授权参数的文档。我试图模仿其他允许这样做的调用,但无济于事。

有没有其他人尝试过这个并取得了任何成功或有任何其他我应该尝试的选项?

【问题讨论】:

    标签: azure-devops azure-devops-rest-api


    【解决方案1】:

    这是我在脚本中一直在做的事情:

    # create endpoint
    $endpoint = irm "$targetUrl/serviceendpoint/endpoints?api-version=5.0-preview" -Method:Post -ContentType "application/json" `
       -Headers @{Authorization = "Basic $base64AuthInfo"} `
       -Body ( '{{"name":"temporary-script-git-import","type":"git","url":"https://{3}@dev.azure.com/{3}/{0}/_git/{0}","authorization":{{"parameters":{{"username":"{1}","password":"{2}"}},"scheme":"UsernamePassword"}}}}' -f $sourceName, $username, $token, $organization )
    
    # import repository
    $importRepo = irm "$targetUrl/git/repositories/$sourceName/importRequests?api-version=5.0-preview" -Method:Post -ContentType "application/json" `
       -Headers @{Authorization = "Basic $base64AuthInfo"} `
       -Body ( '{{"parameters":{{"deleteServiceEndpointAfterImportIsDone":true,"gitSource":{{"url":"https://{2}@dev.azure.com/{2}/{0}/_git/{0}","overwrite":false}},"tfvcSource":null,"serviceEndpointId":"{1}"}}}}' -f $sourceName, $endpoint.id, $organization )
    

    这将创建一个服务端点用于连接到 git(在我的例子中是它的 Azure Devops,但它也可以与 Github 一起使用),然后它使用该服务端点来导入 repo。 $targetUrl - 你的 Azure Devops 路径:$targetUrl = "https://dev.azure.com/$organization/$targetName/_apis"

    【讨论】:

    • 嘿,感谢您添加这篇文章。我很难理解如何确定导入的目标项目。假设我想使用 REST 在同一组织中将 Git 存储库从 ProjectA 导入到 ProjectB,您将如何处理?非常感谢
    • 与 projectA 交谈以获取 repos 列表,与 projectB 交谈以将所需的 repo 从 A 推送到 B
    猜你喜欢
    • 1970-01-01
    • 2021-05-19
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2020-07-22
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多