使用REST Api Visual Studio Team Services更新文件

编程入门 行业动态 更新时间:2024-10-14 02:23:24
本文介绍了使用REST Api Visual Studio Team Services更新文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有任何方法可以使用HTTP动词来更新Visual Studio Team Services帐户上项目中文件的内容,类似于在github developer.github/v3/repos/contents/#update-a-file .

Is there any way to update the contents of a file in a project on your visual studio team services account using HTTP verbs, similar to how it is done here with github developer.github/v3/repos/contents/#update-a-file.

var json={ "comment": "Update scripts.json", "changes": [{ "changeType": 2, "item": { "path": "$/ExtensionsTest/scripts.json", "contentMetadata": { "encoding": 65001 }, "version": 47 }, "newContent": { "content": "[ {\"hello\" : \"Test\"} ]", "contentType":"RawText" } }] }; $.ajax({ type: 'POST', url: 'xxxxx.visualstudio/_apis/tfvc/changesets?api-version=3.0-preview.2', contentType: 'application/json', data: JSON.stringify(json), cache: false, dataType: "json", beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", "Basic " + btoa("my UserName" + ":" + "my PW")); } }).done(function (data) { console.log(data); }); };

上面的代码是我正在使用的代码,但出现400错误.关于我在做什么错的任何建议.

The code above is what I am using and I get a 400 error with it. Any suggestions on what I am doing wrong.

推荐答案

尝试以下方法:

如果您使用的是GIT:

If you are using GIT:

  • 获取提交ID值:请求方法:GET; URL [集合URL]/[团队项目名称]/_ apis/git/存储库/[存储库名称]/commits?api-version=1.0&branch=master&$top=1
  • 更新文件内容:请求方法:发布; URL:[集合URL]/[团队项目名称]/_ apis/git/存储库/[存储库名称]/pushes?api-version=3.0-preview.2;内容类型:application/json;
  • JSON数据:

    { "refUpdates": [{ "name": "refs/heads/master", "oldObjectId": "[step 1 commit ID]" }], "commits": [{ "comment": "Updated BuildLog.cs", "changes": [{ "changeType": 2, "item": {"path": "/BuildLog.cs"}, "newContent": { "content": "using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication1 { public class BuildLog { public int count; public string[] value6; } } ", "contentType": 0 } }] }] }

    如果您使用的是TFVC:

    If you are using TFVC:

  • 获取变更集ID:请求方法:GET;网址:[集合网址]/_ apis/tfvc/changesets?api-version = 1.0& $ top = 1
  • 更新文件内容:请求方法:发布;网址:[集合网址]/_ apis/tfvc/changesets?api-version = 3.0-preview.2;内容类型:application/json;
  • Json数据:

    { "comment": "Updated Class1.cs", "changes": [{ "changeType": 2, "item": { "path": "$/Scrum2015/ClassLibraryB/ClassLibraryB/Class1.cs", "contentMetadata": {"encoding": 65001}, "version": [step changeset id] }, "newContent": { "content": "using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClassLibraryB { public class Class1 { string sgs = \"\"; public void T() { ClassLibraryA.Class1 c = new ClassLibraryA.Class1(); c.TestOther2(); } } } ", "contentType": 0 } }] }

    注意:如果文件内容包含引号(\"test \"),则需要解析引号,与其他特殊宪章相同.

    Note: You need to parse quotes if file content contains quotes (\"test\"), the same as other special charters.

    另一方面,您可以通过vso-node-api实现更多信息,您可以参考以下线程: TFS Rest API签入到版本控制中

    On the other hand, you can achieve that through vso-node-api, more information, you can refer to this thread: TFS Rest API check-in to Version Control

    更新1:

    请参考以下代码来修改您的代码:

    Refer to this code to modify your code:

    var json={ "comment": "Updated tt.json", "changes": [{ "changeType": 2, "item": { "path": "$/Scrum2015/Buildtest/CoreSolutionDemo/WebApplication1/tt.json", "contentMetadata": { "encoding": 65001 }, "version": 754 }, "newContent": { "content": "[ {\"hello\" : \"Test2\"} ]", "contentType": "RawText" } }] }; $.ajax({ type: 'POST', url: 'XXX.visualstudio/_apis/tfvc/changesets?api-version=3.0-preview.2', contentType: 'application/json', data: JSON.stringify(json), cache: false, dataType: 'json', beforeSend: function (xhr) { xhr.setRequestHeader("Authorization", "Basic " + btoa("name" + ":" + "password or PAT")); }, }).done(function (data) { var s1 = "ss"; }).error(function (e) { var s = "ss"; }); })

    更多推荐

    使用REST Api Visual Studio Team Services更新文件

    本文发布于:2023-11-28 13:36:23,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1642622.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:文件   Visual   Api   REST   Services

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!