我正在努力推动更新到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 .
我正在测试来自控制台应用程序的请求 . 正文是从稍后序列化的动态创建的 . filecontent是一个字符串 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并且可以告诉我如何做到这一点?
1 回答
正如评论中提到的,我能够
add
而不是edit
. 解决方案是 first 检入文件并 then 发送更新/编辑 . 如果没有现有文件,编辑将失败并显示异常 .