以编程方式触发?

我在TFS中使用Workspace.Checkin命令得到错误,因为我试图检入的文件是Gated构建定义的一部分。 有很多人问如何使用TFS API来覆盖门控签到。 但是一个非常不同的问题是:如果我真的想要通过编程方式执行门控签入? 我如何在TFS API中触发这个?

我想要做的是:

  • 对一组挂起的更改执行正常的签入操作
  • 接收有关启动的门控版本的信息
  • 可以订阅该构建的完成事件,也可以轮询构建直至完成。
  • 如果构建成功,则继续,并且提交更改(如果可能,请获取生成的变更集)
  • 如果构建失败,请停止并报告错误。
  • 我找不到任何答案来回答这个问题。 也许是这样一个边缘案例,我是唯一足够疯狂想要这样做的人。 也就是说,我对此的需求是合理的。 我唯一可以想到的是,我需要通过门控签入过程的低级功能:1)通过构建定义来查看任何文件的路径是否与任何路径一致在任何门控版本中。 2)如果存在交集,则创建挂起更改的shelveset 3)使用shelveset排队门控定义的新构建4)查询构建定义并刷新信息,直到构建状态完成5)如果构建成功,取消保留待处理的更改,然后使用覆盖登记。

    但是,请注意,即使我执行这些步骤,最终的构建看起来也不会像门控构建。 将发送策略重写通知,让别人知道构建实际执行的唯一方法是将这些信息包含在策略重写注释中。 有没有更直接的方法来做到这一点,使它看起来像任何其他门控构建?


    Scordo的帖子提醒我能够弄清楚如何做到这一点。 代码如下:

    //Initialize connection to server
    TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(ServerAddress));
    try
    {
        tpc.EnsureAuthenticated();
    }
    catch (Exception e)
    {
        System.Environment.Exit(-1);
    }
    VersionControlServer vcServer = tpc.GetService<VersionControlServer>();
    IBuildServer buildServer = tpc.GetService<IBuildServer>();
    
    //.
    //.
    //Program logic goes here...
    //.
    //.
    
    //Get the workspace and the respective changes
    Workspace workspace = vcServer.TryGetWorkspace(LocalProjectPath);  
    PendingChange[] changes = workspace.GetPendingChanges();  
    
    //Perform the check-in
    bool checkedIn = false;
    
    //Either wait for the gated check-in or continue asynchronously...  
    bool waitForGatedCheckin = true;  
    
    //Attempt a normal check-in
    try
    {
        //First, see what policy failures exist, and override them if necessary
        CheckinEvaluationResult result = workspace.EvaluateCheckin(CheckinEvaluationOptions.All, 
        changes, changes, "Test", null, null);
    
        //Perform the check-in, with overrides if necessary, including Work Item policies
        //or include additional code to comply with those policies
        PolicyOverrideInfo override = null;
        if (result.PolicyFailures != null)
        {
            override = new PolicyOverrideInfo("override reason", result.PolicyFailures);
        }
        workspace.CheckIn(changes, comment, null, null, override);
        checkedIn = true;
    }
    catch (GatedCheckinException gatedException)
    {
        //This exception tells us that a gated check-in is required.
        //First, we get the list of build definitions affected by the check-in
        ICollection<KeyValuePair<string, Uri>> buildDefs = gatedException.AffectedBuildDefinitions;
    
        if (buildDefs.Count == 1)
        {
            //If only one affected build definition exists, then we have everything we need to proceed
            IEnumerator<KeyValuePair<string, Uri>> buildEnum = buildDefs.GetEnumerator();
            buildEnum.MoveNext();
            KeyValuePair<string, Uri> buildDef = buildEnum.Current;
            String gatedBuildDefName = buildDef.Key;
            Uri gatedBuildDefUri = buildDef.Value;
            string shelvesetSpecName = gatedException.ShelvesetName;
            string[] shelvesetTokens = shelvesetSpecName.Split(new char[] { ';' });
    
            //Create a build request for the gated check-in build
            IBuildRequest buildRequest = buildServer.CreateBuildRequest(gatedBuildDefUri);  
            buildRequest.ShelvesetName = shelvesetTokens[0]; //Specify the name of the existing shelveset
            buildRequest.Reason = BuildReason.CheckInShelveset; //Check-in the shelveset if successful 
            buildRequest.GatedCheckInTicket = gatedException.CheckInTicket; //Associate the check-in
    
            //Queue the build request
            IQueuedBuild queuedBuild = buildServer.QueueBuild(buildRequest);
    
            //Wait for the build to complete, or continue asynchronously
            if (waitForGatedBuild)
            {
                while (!queuedBuild.Build.BuildFinished)
                {
                    //Get the latest status of the build, and pause to yield CPU
                    queuedBuild.Refresh(QueryOptions.Process);  
                    System.Threading.Thread.Sleep(1000)
                }
                if (queuedBuild.Build.Status == BuildStatus.Succeeded)
                {
                    checkedIn = true;
                }
            }
        }
        else
        {
            //Determine a method for specifying the appropriate build definition
            //if multiple build definitions are affected
        }
    }
    catch (CheckinException checkinException)
    {
        //Handle other checkin exceptions such as those occurring with locked files, 
        //permissions issues, etc.
    }
    
    if (checkedIn)
    {
        //Additional logic here, if the check-in was successful
    }
    

    我从来没有做过,但这应该会对你有所帮助:

    当您使用工作区签入更改并且此检入受到门控构建保护时,会引发GatedCheckinException。 这个例外应该有所有必要的信息来创建一个shelveset并用这个shelveset触发正确的构建。 例如, AffectedBuildDefinitions属性应该包含门控构建定义的构建定义。

    希望有所帮助。

    链接地址: http://www.djcxy.com/p/45021.html

    上一篇: in be triggered programmatically?

    下一篇: TFS Gated check