【问题标题】:Pushing an update to VSTS via REST API通过 REST API 向 VSTS 推送更新
【发布时间】:2018-01-10 10:01:42
【问题描述】:

我正在努力将更新推送到 VSTS Git 存储库中的现有文件。 文档https://docs.microsoft.com/en-us/rest/api/vsts/git/pushes/create#update_a_file 显示了该怎么做。

当我使用GET 而不是POST 时,我会得到所有现有的推送。所以基本上这个请求是有效的。这是我的网址https://mine.visualstudio.com/_apis/git/repositories/ad3d7615-6e4a-4988-bd3f-ca80d7ec9791/pushes?api-version=4.1-preview

我正在测试来自控制台应用程序的请求。主体是从稍后序列化的动态创建的。文件内容是一个字符串var x=1;

dynamic body = new
{
   refUpdates = new[] { new { name = "refs/heads/master", oldObjectId = branchObjectId } },
   commits = new[] {
         new {
            comment = "Updated Configuration" ,
            changes = new[] {
               new {
                  changeType = "edit",
                  item = new { path = "/files/filename.js"},
                  newContent = new { content = filecontent, contentType = "rawtext" }
               }
            }
         }
      }
};

请求如下所示:

using (HttpClient client = new HttpClient())
{
   client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
   byte[] login = Encoding.ASCII.GetBytes(string.Format("{0}:{1}", "", personalaccesstoken));
   client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(login));

   var content = new StringContent(body, Encoding.UTF8, "application/json");
   using (HttpResponseMessage response = client.PostAsync(url, content).Result)
   {
      response.EnsureSuccessStatusCode();

      string responseBody = await response.Content.ReadAsStringAsync();
      responseBodyAction.Invoke(responseBody);
   }
}

不成功,抛出如下异常:

System.Net.Http.HttpRequestException: '响应状态码不 表示成功:404(未找到)。

正文是有效的 JSON。

某些 API 调用需要 URL .vsrm.visualstudio.com 而不是 .visualstudio.com。但在这种情况下不是。

我没有想法。有人成功推送了 VSTS 的更新并能告诉我怎么做吗?

【问题讨论】:

  • 我只是尝试了changeType = "add", 而不是编辑,它成功了。当然有一个新的文件名。进一步挖掘......

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


【解决方案1】:

正如评论中提到的,我能够add 而不是edit。 解决方案是首先签入文件,然后然后发送更新/编辑。 如果没有现有文件,编辑将失败并出现异常。

【讨论】:

  • 感谢您分享您的解决方案,如果可能,您可以将其标记为答案,这样可以帮助遇到相同问题的其他社区成员。
猜你喜欢
  • 2020-03-30
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 2021-06-11
相关资源
最近更新 更多