简体   繁体   English

在 Azure DevOps 中自动创建文件夹?

[英]Automate folder creation in Azure DevOps?

I'm new to Azure, and I was wondering how I could automate folder creation and file creation in Azure DevOps using Python and the Rest API? I'm new to Azure, and I was wondering how I could automate folder creation and file creation in Azure DevOps using Python and the Rest API? Would I have to use Azure Automation?我必须使用 Azure 自动化吗?

You could refer to this API: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pushes/create?view=azure-devops-rest-6.0&tabs=HTTP#add-a-text-file你可以参考这个API: https://docs.microsoft.com/en-us/rest/api/azure/devops/git/pushes/create?view=azure-devops-rest-6.0&tabs=HTTP#add-一个文本文件

By getting the latest commit ID on the target branch, you could use the commit ID in the above API to create a file with a new folder as its root.通过获取目标分支上的最新提交 ID,您可以使用上述 API 中的提交 ID 创建一个以新文件夹为根的文件。

Here is the sample PowerShell script:这是示例 PowerShell 脚本:

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


$URL = "https://dev.azure.com/{OrgName}/{ProjectName}/_apis/git/repositories/{RepoName}/commits?searchCriteria.itemVersion.version={BranchName}&api-version=6.0"
$header = @{
'Authorization' = 'Basic ' + $B64Pat
'Content-Type' = 'application/json'
}


$response2=Invoke-RestMethod -Method GET -Uri $URL -Headers $header


$CommitID=$response2.value[0].commitId


echo $CommitID


$URL = "https://dev.azure.com/{OrgName}/{ProjectName}/_apis/git/repositories/{RepoName}/pushes?api-version=6.1-preview.2"
$header = @{


   'Content-Type' = 'application/json'
}
$Body = "
{
  `"refUpdates`": [
    {
      `"name`": `"refs/heads/{BranchName}`",
      `"oldObjectId`": `"$CommitID`"
    }
  ],
  `"commits`": [
    {
     `"comment`": `"Added task markdown file.`",
      `"changes`": [
        {
          `"changeType`": `"add`",
          `"item`": {
            `"path`": `"test/tasks.md`"
          },
          `"newContent`": {
            `"content`": `"created by API`",
            `"contentType`": `"rawtext`"
          }
        }
      ]
    }
  ]
}
"


$response = Invoke-RestMethod -Uri $URL -Headers @{Authorization = "Basic $B64Pat"} -Method Post -Body $Body  -ContentType application/json

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

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