azure - 使用 Azure Devops Rest API 从特定分支创建 Azure Devops 分支

标签 azure azure-devops-rest-api

我有一些文件需要以编程方式更改,以及是否检测到更改。然后需要创建一个分支,然后创建一个 PR 进行手动审批。

当我尝试从主分支创建分支时,我陷入了困境。即使我只想使用初始提交 ( https://learn.microsoft.com/en-us/rest/api/azure/devops/git/pushes/create?view=azure-devops-rest-5.1&tabs=HTTP#initial-commit-(create-a-new-branch) ) 并想添加这样的 json 文件。

    $params = '{
    "refUpdates": [
      {
        "name": "refs/heads/feature/testBranchNish",
        "oldObjectId": "0000000000000000000000000000000000000000"
      }
    ],
    "commits": [
      {
        "comment": "Initial commit.",
        "changes": [
          {
            "changeType": "add",
            "item": {
              "path": "/setting.json"
            },
            "newContent": {
              "content": "{"version":1}",
              "contentType": "application/json"
            }
          }
        ]
      }
    ]
  }'

在上面的 Json 中,我需要以下两种解决方案之一,但目前它们都不起作用。

第一个选项:因为我需要更新 json 文件所以我应该将 contentType 提及为“application/json”,但它给出了 Newtonsoft.Json.JsonReaderException ,如下所述。 Invoke-RestMethod : {"$id":"1","innerException":null,"message":"TF400898: 发生内部错误。事件 ID: 65512b93-8924-4e40-8bd8-ba975418d8bd。","typeName":“Newtonsoft.Json.JsonReaderException,Newtonsoft.Json” ,"typeKey":"JsonReaderException","errorCode":0,"eventId":0} 行:27 字符:1

+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

第二个选项:我不想提供新内容,而是宁愿提供类似这样的文件路径。

$params = '{
    "refUpdates": [
      {
        "name": "refs/heads/feature/testBranchNish",
        "oldObjectId": "0000000000000000000000000000000000000000"
      }
    ],
    "commits": [
      {
        "comment": "Initial commit.",
        "changes": [
          {
            "changeType": "add",
            "item": {
              "path": "/setting.json"
            },
            "newContent": {
              "path": "C:/Users/abc/setting.json",
              "contentType": "application/json"
            }
          }
        ]
      }
    ]
  }'

但我在 Microsoft 文档中没有找到此选项。因此,在查看 MS 文档时,第一个选项与我更相关。

如果你们已经解决了这个问题,请回答。

提前致谢

最佳答案

对于您的第一个选项,它应该是请求正文问题。请注意,我们无法指定 "contentType": "application/json",这是不受支持的。 “contentType”只能设置为“base64Encoded”或“rawText”。请参阅ItemContentType , enter image description here

I don't want to provide the new content rather I would rather provide the file path

我认为我们无法像在第二个选项中那样仅提供文件路径来实现这一目标。基本上,对于 Git,我们必须首先将文件添加到本地存储库,然后提交更改,然后将更改推送到远程存储库。因此,我们不能像从 Windows 资源管理器上传文件一样直接从外部路径(本地存储库之外)添加文件。

但是,您可以尝试从 json 文件中获取内容并将其转换为字符串,然后在请求正文中使用它。

类似这样的:(但是我找不到将内容转换为有效字符串的正确方法,您可以根据此示例进行进一步的研究)

$content = (Get-Content "C:/Users/abc/setting.json" -Raw)

function CreateJsonBody
{

    $value = @"
    {
        "refUpdates": [
          {
            "name": "refs/heads/feature/testBranchNish",
            "oldObjectId": "0000000000000000000000000000000000000000"
          }
        ],
        "commits": [
          {
            "comment": "Initial commit.",
            "changes": [
              {
                "changeType": "add",
                "item": {
                  "path": "/setting.json"
                },
                "newContent": {
                  "content": "$content",
                  "contentType": "rawText"
                }
              }
            ]
          }
        ]
    }
"@

 return $value
}

$json = CreateJsonBody

关于azure - 使用 Azure Devops Rest API 从特定分支创建 Azure Devops 分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73140089/

相关文章:

azure - Actor 模型: how to reach data integrity in this specific situation?

Azure WebJob QueueTrigger 重试策略

azure-devops - 如何从 Azure DevOps Services REST API 获取可用区域路径?

Azure管道: is it mandatory to have azure pipelines and code in same branch

c# - Azure 媒体服务 - 生成新的 AES 加密 token 用于播放

php - Azure GitHub 部署问题

python - 使用 Pushes Create Api with Python 将文件上传/推送到 Azure Devops Repo 时如何查找 oldobjectid

python - 使用 python azure-devops 包时,如何使用 PAT 进行身份验证?

azure - 如何使用 Azure DevOps 的 MSAL.js 获取有效的 AAD v2 token

python-3.x - 使用 Python 3.6 使用个人访问 token 对 VisualStudioOnline REST API 进行身份验证