【问题标题】:Create TFS work item with PowerShell使用 PowerShell 创建 TFS 工作项
【发布时间】:2016-04-15 22:45:37
【问题描述】:

我正在使用预构建 PowerShell 脚本进行 TFS 构建,该脚本(除了构建我的应用程序之外)会自动签出我们维护版本的文件,增加内部版本号,然后签入文件。

我已经能够做到这一点,除了我从脚本中得到一个错误,导致部分成功构建(橙色)。我需要有一个完全成功的(绿色)构建。

这是签到行(使用TFS Power Tools for VS 2013):

New-TfsChangeset -Item $versionFile -Override "Automated" -Notes "Code Reviewer=tfs" -Comment "Automated"

我收到的错误是变更集没有与工作项关联,但-Override 应该处理它。有趣的是它无论如何都会签入。

在我的机器上本地运行而不是构建服务器,我得到了同样的东西,除了我还看到一行写着The policies have been overridden. 这告诉我覆盖正在工作,但它仍然输出错误。

我尝试添加-ErrorAction SilentlyContinue,但没有效果。

我需要三个选项之一:

  1. 一种抑制签入错误输出的方法,
  2. 一种创建工作项并将其与签入相关联的方法,或者
  3. 其他第三个选项将导致绿色构建。

有什么想法吗?

【问题讨论】:

  • 什么是实际的构建错误/警告消息,表明它已部分成功?
  • 这是构建结果:“构建部分成功”。我收到的唯一其他消息是TF10139: The following check-in policies have not been satisfied: You must associate this check-in with one or more work items,正如我所解释的那样。

标签: powershell tfs


【解决方案1】:

感谢 Eddie - MSFT 引导我正确的方向,但我想在这里巩固一切。

警告这将签入工作区中的所有未决更改。

创建一个新的工作项 (source)

我确实对其进行了一些修改以支持自动化。它连接到 TFS 并生成一个新的工作项。

function New-WorkItem()
{
    # These *should* be registered in the GAC.
    # The version numbers will likely have to change as new versions of Visual Studio are installed on the server.
    Add-Type -Assembly "Microsoft.TeamFoundation.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
    Add-Type -Assembly "Microsoft.TeamFoundation.WorkItemTracking.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

    $server = "http://YOURTFSSERVER:8080/tfs"
    $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($server)
    $type = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore]
    $store = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore] $tfs.GetService($type)
    $workItem = New-Object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem($store.Projects[0].WorkItemTypes[0])
    $workItem.Title = "Automated work item"

    $workItem
}

关联工作项并签入

对 Eddie 给出的链接中的代码稍作修改,我们得到以下内容:

function CheckIn()
{
    param([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem] $workItem)

    $col = Get-TfsCollection("http://YOURTFSSERVER:8080/tfs/YOURCOLLECTION")
    $vcs = Get-TfsVersionControlServer($col)
    $ws = $vcs.GetWorkspace([System.IO.Path]::GetDirectoryName($anyPathInWorkspace))
    $pc = $ws.GetPendingChanges()
    $wici = Get-TfsWorkItemCheckinInfo($workItem)
    $changeset = $ws.CheckIn($pc, "Automated check in", $null, $wici, $null)
}

该帖子并未告诉您 Get-TfsCollection、Get-TfsVersionControlServer 和 Get-TfsWorkItemCheckinInfo 未定义。我必须找到他们。

我在http://nkdagility.com/powershell-tfs-2013-api-1-get-tfscollection-and-tfs-services/ 上找到了前两个。我不需要改变任何东西。

function Get-TfsCollection
{
    param([string] $CollectionUrl)
    if ($CollectionUrl -ne "")
    {
        #if collection is passed then use it and select all projects
        $tfs = [Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory]::GetTeamProjectCollection($CollectionUrl)
    }
    else
    {
        #if no collection specified, open project picker to select it via gui
        $picker = New-Object Microsoft.TeamFoundation.Client.TeamProjectPicker([Microsoft.TeamFoundation.Client.TeamProjectPickerMode]::NoProject, $false)
        $dialogResult = $picker.ShowDialog()
        if ($dialogResult -ne "OK")
        {
            #exit
        }
        $tfs = $picker.SelectedTeamProjectCollection
    }

    $tfs
}

function Get-TfsVersionControlServer
{
    param([Microsoft.TeamFoundation.Client.TfsTeamProjectCollection] $TfsCollection)

    $TfsCollection.GetService("Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer")
}

但我找不到Get-TfsWorkItemCheckinInfo。唯一的谷歌点击是来自 Eddie 的 kinook 链接(很快可能是这个答案)。这是我想出的:

function Get-TfsWorkItemCheckinInfo
{
    param([Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem] $workItem)

    $wi = New-Object Microsoft.TeamFoundation.VersionControl.Client.WorkItemCheckinInfo($workItem, [Microsoft.TeamFoundation.VersionControl.Client.WorkItemCheckinAction]::Resolve)

    $wi
}

现在我们可以使用它了

CheckIn (New-WorkItem)

就是这样!

【讨论】:

    【解决方案2】:

    您可以按照这篇文章从 PowerShell 创建工作项:http://halanstevens.com/blog/powershell-script-to-create-a-workitem/

    在此处引用代码以供参考:

    $key = Get-ItemProperty HKLM:\SOFTWARE\Microsoft\VisualStudio\8.0
    
    $dir = [string] (Get-ItemProperty $key.InstallDir)
    
    $dir += "PrivateAssemblies\"
    
    $lib = $dir + "Microsoft.TeamFoundation.WorkItemTracking.Client.dll"
    
    [Reflection.Assembly]::LoadFrom($lib)
    
    $lib = $dir + "Microsoft.TeamFoundation.Client.dll"
    
    [Reflection.Assembly]::LoadFrom($lib)
    
    "Please enter your Team Foundation Server Name:"
    $server = [Console]::ReadLine()
    $server = $server.Trim()
    
    "Connecting to " + $server + "..."
    $tfs = [Microsoft.TeamFoundation.Client.TeamFoundationServerFactory]::GetServer($server)
    
    $type = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore]
    
    $store = [Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemStore] $tfs.GetService($type)
    
    $workItem = new-object Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem($store.Projects[0].WorkItemTypes[0])
    
    "Created a new work item of type " + $workItem.Type.Name
    
    $workItem.Title = "Created by Windows PowerShell!"
    
    $workItem.Save()
    

    然后参考这篇文章将工作项关联到变更集:http://www.kinook.com/Forum/showthread.php?t=4502

    【讨论】:

    • 该示例更具交互性,但我想我明白了要点。谢谢。如果成功,您将获得梦寐以求的绿色复选标记!
    • 我能够在第二个链接中找到除Get-TfsWorkItemCheckinInfo 之外的所有函数的定义。有什么想法吗?
    • 请更新您的答案以指向我的答案。它有点完整,但我想给你一个功劳。
    • 你的第一个链接坏了
    猜你喜欢
    • 1970-01-01
    • 2014-10-15
    • 2012-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-24
    • 1970-01-01
    • 2017-07-20
    相关资源
    最近更新 更多