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

时间:2015-04-15 14:43:27

标签: c# git tfs tfs2013 tfs-sdk

我正在尝试为我的团队自动创建git存储库。我需要使用Web Api,而不是.NET API。我正在尝试使用的调用this one响应,但在HTTP / 1.1 400错误请求中返回以下错误正文:

{"$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);

我在没有项目“名称”的情况下尝试了这个 - (就像示例一样),没有“id”,从Get Repositories Api收集了不同的“id”guid。

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

1 个答案:

答案 0 :(得分:4)

我知道这已经过时了,但希望其他人偶然发现这个答案......

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

对此的代码解决方案,如果您没有可用项目的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; }
}

谢谢!