如何通过 Web Api 创建 TFS 2013 Git 存储库?

How do I create a TFS 2013 Git repository via Web Api?

我正在尝试为我的团队自动创建 git 存储库。我需要使用 Web Api,而不是 .NET API。我正在尝试使用的调用是 this one 响应,但是 returns HTTP/1.1 400 Bad Request:

中的以下错误正文
{"$id":"1","innerException":null,"message":"Bad parameters. A repository with a team project and a name are required.","typeName":"System.ArgumentException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089","typeKey":"ArgumentException","errorCode":0,"eventId":0}

错误消息是:参数错误。需要包含团队项目和名称的存储库。

这是我的代码:

    var projectName = "testing";
    var url = ConfigurationManager.AppSettings["TFS-Url"] + "/_apis/git/repositories/?api-version=1.0";
    var data = "{ \"name\": \"" + projectName + "\", \"project\": { \"id\": \"" + ConfigurationManager.AppSettings["TFS-Parent-Project-Guid"] + "\", \"name\": \"" + ConfigurationManager.AppSettings["TFS-Parent-Project-Name"] + "\" } }";

    var wc = new WebClient();
    wc.Credentials = new NetworkCredential("user", "pass");
    var res = wc.UploadString(url, data);

我在没有项目的 "name" 的情况下尝试过这个 - (就像示例一样),没有 "id",从 [=15= 收集的不同 "id" guid ].

无论我尝试什么,都返回相同的错误。有什么想法吗?

我知道这已经过时了,但希望有人偶然发现这里的答案...

MS 网站上的文档不正确。当通过 WebApi 提交 postdata 以在 TFS 中创建新存储库时,项目对象的 ID 是必需的 属性(在 TFS 2015 Update 2、Update 3 和 VSTS 上测试)。

如果您没有可用的项目 GUID 列表,此问题的代码解决方案是:

public static TfsProject GetTfsProjectGuid(string projectName, string collectionName)
{
    var tfsInstance = ConfigurationManager.AppSettings["TFSInstance"];
    using (var client = new WebClient())
    {
        client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
        client.UseDefaultCredentials = true;

        var tfsUri = new Uri(tfsInstance + collectionName + "/_apis/projects/" + projectName + "?api-version=1.0");
        var response = client.DownloadString(tfsUri);

        JavaScriptSerializer jss = new JavaScriptSerializer();
        return jss.Deserialize<TfsProject>(response.ToString());
    }
}

TfsProject 看起来像:

public class TfsProject
{
    public string id { get; set; }
    public string name { get; set; }
    public string description { get; set; }
    public string url { get; set; }
    public string state { get; set; }
    public int revision { get; set; }
}

为了实际创建存储库,我使用的是:

public static OperationResult CreateTfsGitRepository(string projectName, string repositoryName, string collectionName)
{
    var tfsInstance = ConfigurationManager.AppSettings["TFSInstance"];
    using (var client = new WebClient())
    {
        client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
        client.UseDefaultCredentials = true;

        var tfsNewRepository = new TfsRepository();
        tfsNewRepository.name = repositoryName;
        tfsNewRepository.project.id = TfsHelper.GetTfsProjectGuid(projectName, collectionName).id;
        var tfsUri = new Uri(tfsInstance + collectionName + "/_apis/git/repositories/?api-version=1.0");

        JavaScriptSerializer jss = new JavaScriptSerializer();
        var jsonValues = jss.Serialize(tfsNewRepository);
        try
        {
            var response = client.UploadString(tfsUri, "POST", jsonValues);
        }
        catch (WebException ex)
        {
            //Handle WebExceptions here.  409 is the error code for a repository with the same name already exists within the specified project
        }
        return new OperationResult { ReturnValue = 0, Message = "Repository created successfully." };
    }
}

OperationResult 对象是:

public class OperationResult
{
    public int ReturnValue { get; set; }
    public string Message { get; set; }
}

谢谢!