试试这些方法:
如果您使用的是 GIT:
- 获取提交ID值:请求方法:GET;URL [集合 url]/[团队项目名称]/_apis/git/repositories/[存储库名称]/commits?api-version=1.0&branch=master&$top=1
- 更新文件内容:请求方式:Post;URL:[集合 url]/[团队项目名称]/_apis/git/repositories/[存储库名称]/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:
- 获取变更集 ID:请求方法:GET;网址:[集合网址]/_apis/tfvc/changesets?api-version=1.0&$top=1
- 更新文件内容:请求方式:Post;网址:[集合网址]/_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\”),则需要解析引号,与其他特殊包一样。
另一方面,你可以通过 vso-node-api 来实现,更多信息,你可以参考这个线程:TFS Rest API check-in to Version Control
更新1:
请参阅此代码以修改您的代码:
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: 'https://XXX.visualstudio.com/_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";
});
})