简体   繁体   English

Azure DevOps API - 如何创建存储库?

[英]Azure DevOps API- How to create a repository?

I'm developing a internal app that creates the skeleton of a solution according to internal guidelines.我正在开发一个内部应用程序,根据内部指南创建解决方案的框架。

As a improvement, I would like to enable the user to automatically have the solution "formalized" on our DevOps, where he would clone and start coding right away, instead of the current download as ZIP.作为一项改进,我希望让用户能够在我们的 DevOps 上自动“正式化”解决方案,他可以在其中克隆并立即开始编码,而不是将当前下载为 ZIP。

In order to do that, i started looking at the azure devops docs, but could not figure out a way to create a repository via API...为了做到这一点,我开始查看 azure devops 文档,但找不到通过 API 创建存储库的方法......

How can I do that?我怎样才能做到这一点?

Here is a sample of code we use in a PowerShell script to create repos这是我们在 PowerShell 脚本中用于创建存储库的代码示例

$repoBody = @{
    name = "YourRepoName"
} | ConvertTo-Json

Invoke-WebRequest -method Post -Headers @{Authorization = "Basic $encodedPat"} -body $repoBody -Uri $projUrl -ContentType "application/json" -UseBasicParsing

This is the API call you are looking for:这是您正在寻找的 API 调用:

POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.0

Source: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/create?view=azure-devops-rest-5.0来源: https : //docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/create?view=azure-devops-rest-5.0

You have the Repositories - Create Rest API:您拥有存储库 - 创建Rest API:

POST https://dev.azure.com/{organization}/{project}/_apis/git/repositories?api-version=5.0

Body:身体:

{
  "name": "NewRepository",
  "project": {
    "id": "6ce954b1-ce1f-45d1-b94d-e6bf2464ba2c"
  }
}

In addition, if you have the AZ modules loaded in powershell, you can simply execute另外,如果你在powershell中加载了AZ模块,你可以简单地执行

    az repos create --name "MyNewRepoName" --org "https://dev.azure.com/OrganizationName/" --project "ProjectNameToCreateRepoIn"

To install AZ modules in powershell use command要在 powershell 中安装 AZ 模块,请使用命令

    Install-Module -Name Az -AllowClobber -Scope CurrentUser

With AZ modules installed execute this before creating repo安装 AZ 模块后,在创建 repo 之前执行此操作

    Set-AzContext -SubscriptionId "YourSubscriptionID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM