【问题标题】:How do I create a TFS 2013 Git repository via Web Api?如何通过 Web Api 创建 TFS 2013 Git 存储库?
【发布时间】:2015-06-21 14:20:16
【问题描述】:

我正在尝试为我的团队自动创建 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。

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

【问题讨论】:

  • 您是否尝试过在通过 .Net API 或 Visual Studio Team Explorer 或其他方式创建存储库时使用 Wireshark 或 Fiddler 拦截 Web 请求,然后将其与您生成的内容进行匹配?那将是我的第一步,但真的无能为力。
  • 我也是这么想的,但是 VS Team Explorer 不支持创建存储库,除非必须,否则我宁愿不要花时间深入研究 .NET API。听起来我可能不得不...

标签: c# git tfs tfs-sdk


【解决方案1】:

我知道这是旧的,但希望其他人在这里偶然发现答案......

MS 网站上的文档不正确。在通过 WebApi 提交 postdata 以在 TFS 中创建新存储库时,Id 是 Project 对象的必需属性(在 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; }
}

为了实际创建 repo,我使用的是:

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; }
}

谢谢!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2019-12-06
    • 2015-04-07
    • 2013-02-23
    • 2019-09-22
    • 2014-11-26
    相关资源
    最近更新 更多