Skip to content

Commit

Permalink
Refactor to have a base WorkItemCmdlet
Browse files Browse the repository at this point in the history
  • Loading branch information
vbjay committed Sep 6, 2016
1 parent dd5879e commit 780766e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 51 deletions.
54 changes: 3 additions & 51 deletions TFS Release Notes/Cmdlets/GetWorkItemFiles.vb
Original file line number Diff line number Diff line change
Expand Up @@ -4,73 +4,25 @@ 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 IDs of the work items to retrieve changed files from.")>
Property WorkItemIDs 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
Inherits WorkItemCmdlet

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()

'No need to check for array length because PowerShell handles it for us

Dim wi As WorkItem() = WorkItemIDs.Distinct.Select(Function(w) TFSCollection.WIT.GetWorkItem(w)).ToArray

If _GetSubWorkItems Then
If GetSubWorkItems Then
wi = wi.SelectMany(Function(w) GetChildWorkItems(w, TFSCollection)).DistinctBy(Function(w) w.Id).ToArray
End If
Dim vcs = TFSCollection.VCS

Dim links = wi.SelectMany(Function(w) w.Links.Cast(Of Link).OfType(Of ExternalLink))
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))

Expand Down
55 changes: 55 additions & 0 deletions TFS Release Notes/Cmdlets/WorkItemCmdlet.vb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
Imports System.Management.Automation

Public MustInherit Class WorkItemCmdlet
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 IDs of the work items to retrieve changed files from.")>
Property WorkItemIDs As Integer()
Private _GetSubWorkItems As Boolean = False
<Parameter(Mandatory:=False,
ValueFromPipelineByPropertyName:=True,
ValueFromPipeline:=True,
HelpMessage:="Add this switch to get all child work items 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

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
End Class
1 change: 1 addition & 0 deletions TFS Release Notes/TFS Release Notes.vbproj
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@
<ItemGroup>
<Compile Include="Cmdlets\GetTFS.vb" />
<Compile Include="Cmdlets\GetWorkItemFiles.vb" />
<Compile Include="Cmdlets\WorkItemCmdlet.vb" />
<Compile Include="My Project\AssemblyInfo.vb" />
<Compile Include="My Project\Application.Designer.vb">
<AutoGen>True</AutoGen>
Expand Down

0 comments on commit 780766e

Please sign in to comment.