Skip to content

Commit

Permalink
Add GetWorkItemFiles cmdlet closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
vbjay committed Aug 27, 2016
1 parent 484beb6 commit 0930a1f
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 14 deletions.
90 changes: 90 additions & 0 deletions TFS Release Notes/Cmdlets/GetWorkItemFiles.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
Imports System.Management.Automation
Imports System.Text.RegularExpressions
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

<Cmdlet(VerbsCommon.Get, "WorkItemFiles")>
Public Class GetWorkItemFiles
Inherits PSCmdlet

<Parameter(Mandatory:=True,
ValueFromPipelineByPropertyName:=True,
ValueFromPipeline:=True,
Position:=0,
ParameterSetName:="URL",
HelpMessage:="The URL of the TFS collection to access.")>
Property ServerURL As Uri

<Parameter(Mandatory:=True,
ValueFromPipelineByPropertyName:=True,
ValueFromPipeline:=True,
Position:=0,
ParameterSetName:="Collection",
HelpMessage:="The TFS collection to use.")>
Property TFSCollection As TFSCollection

<Parameter(Mandatory:=True,
ValueFromPipelineByPropertyName:=True,
ValueFromPipeline:=True,
Position:=1,
HelpMessage:="The ID of the workitem to retrieve changed files from.")>
Property WorkItemID As Integer

<Parameter(Mandatory:=False,
ValueFromPipelineByPropertyName:=True,
ValueFromPipeline:=True,
HelpMessage:="Add this switch to get all child workitems recursively and get changes from all of them.")>
Property GetSubWorkItems As SwitchParameter
Get
Return _GetSubWorkItems
End Get
Set(value As SwitchParameter)
_GetSubWorkItems = value.ToBool
End Set
End Property

Private _GetSubWorkItems As Boolean = False

Protected Overrides Sub BeginProcessing()
MyBase.BeginProcessing()
Select Case True

Case TFSCollection IsNot Nothing
'we are good
Case TFSCollection Is Nothing AndAlso ServerURL IsNot Nothing

TFSCollection = GetTFSCollection(ServerURL)

End Select

End Sub

Protected Overrides Sub ProcessRecord()
MyBase.ProcessRecord()

Dim wi As WorkItem() = {TFSCollection.WIT.GetWorkItem(WorkItemID)}

If _GetSubWorkItems Then
wi = GetCHildWorkItems(wi.First, TFSCollection).ToArray
End If
Dim vcs = TFSCollection.VCS

Dim links = wi.SelectMany(Function(w) w.Links.Cast(Of Link).OfType(Of ExternalLink))
Dim changesetRegex As New Regex("vstfs:///VersionControl/Changeset/(\d+)", RegexOptions.Compiled)
Dim changesetLinks = links.Where(Function(l) changesetRegex.IsMatch(l.LinkedArtifactUri))

Dim changesets = changesetLinks.Select(Function(c) vcs.GetChangeset(CInt(changesetRegex.Match(c.LinkedArtifactUri).Groups(1).Value))).DistinctBy(Function(c) c.ChangesetId)

Dim changes = changesets.SelectMany(Function(cs) cs.Changes)

Dim byItem = From c In changes Group By ServerPath = c.Item.ServerItem Into ItemChanges = Group
Select New FileChangeInfo With {.ServerPath = ServerPath, .Changes = ItemChanges, .LastCheckin = ItemChanges.Max(Function(c) c.Item.CheckinDate)}

For Each item In byItem

For Each ch In item.Changes.OrderByDescending(Function(c) c.Item.CheckinDate)
WriteObject(item)
Next
Next

End Sub
End Class
6 changes: 6 additions & 0 deletions TFS Release Notes/Outputs/FileChangeInfo.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Imports Microsoft.TeamFoundation.VersionControl.Client
Public Class FileChangeInfo
Property Changes As IEnumerable(Of Change)
Property LastCheckin As Date
Property ServerPath As String
End Class
43 changes: 29 additions & 14 deletions TFS Release Notes/Services.vb
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

Module Services

Function GetTFSCollection(ServerURL As Uri) As TFSCollection

Dim tfs As New TFSCollection(
TfsTeamProjectCollectionFactory.GetTeamProjectCollection(ServerURL))

tfs.ProjectCollection.Authenticate()

Return tfs
End Function
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client

Module Services

Function GetTFSCollection(ServerURL As Uri) As TFSCollection

Dim tfs As New TFSCollection(
TfsTeamProjectCollectionFactory.GetTeamProjectCollection(ServerURL))

tfs.ProjectCollection.Authenticate()

Return tfs
End Function

Iterator Function GetChildWorkItems(WI As WorkItem, tfs As TFSCollection) As IEnumerable(Of WorkItem)

Yield WI

For Each item In WI.WorkItemLinks.Cast(Of WorkItemLink).Where(Function(l) l.LinkTypeEnd.Name = "Child")

Dim tmp As WorkItem = tfs.WIT.GetWorkItem(item.TargetId)

For Each tmpWi In GetChildWorkItems(tmp, tfs)

Yield tmpWi
Next
Next
End Function
End Module
2 changes: 2 additions & 0 deletions TFS Release Notes/TFS Release Notes.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Cmdlets\GetTFS.vb" />
<Compile Include="Cmdlets\GetWorkItemFiles.vb" />
<Compile Include="My Project\AssemblyInfo.vb" />
<Compile Include="My Project\Application.Designer.vb">
<AutoGen>True</AutoGen>
Expand All @@ -280,6 +281,7 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="Outputs\FileChangeInfo.vb" />
<Compile Include="Outputs\TFSCollection.vb" />
<Compile Include="Services.vb" />
</ItemGroup>
Expand Down

0 comments on commit 0930a1f

Please sign in to comment.