-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Retrieve stale branches #269
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<!-- #include "./common/header.md" --> | ||
|
||
# Get-VSTeamStaleBranch | ||
|
||
## SYNOPSIS | ||
|
||
<!-- #include "./synopsis/Get-VSTeamStaleBranch.md" --> | ||
|
||
## SYNTAX | ||
|
||
## DESCRIPTION | ||
|
||
Retrieve Stale Branches | ||
|
||
You must call Set-VSTeamAccount before calling this function. | ||
|
||
## EXAMPLES | ||
|
||
### -------------------------- EXAMPLE 1 -------------------------- | ||
|
||
```PowerShell | ||
PS C:\> Get-VSTeamStaleBranch | ||
``` | ||
|
||
This will return all branches that have not been committed to within 90 days (default value) | ||
|
||
### -------------------------- EXAMPLE 2 -------------------------- | ||
|
||
```PowerShell | ||
PS C:\> Get-VSTeamStaleBranch -top 5 | Format-Wide | ||
``` | ||
|
||
This will return the top five Process Templates only showing their name | ||
|
||
## PARAMETERS | ||
|
||
<!-- #include "./params/ProcessName.md" --> | ||
|
||
### -RepositoryId | ||
|
||
Specifies the Repository Id to process | ||
|
||
```yaml | ||
Type: Guid | ||
Parameter Sets: ByRepositoryId | ||
``` | ||
|
||
### -MaximumAgeDays | ||
|
||
The maximum number of days a branch has not been committed to rending it "stale" | ||
|
||
```yaml | ||
Type: Int32 | ||
Default value: 90 | ||
``` | ||
|
||
## INPUTS | ||
|
||
## OUTPUTS | ||
|
||
## NOTES | ||
|
||
## RELATED LINKS |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Retrieve Stale Branches |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
function Get-VSTeamStaleBranch { | ||
[CmdletBinding(DefaultParameterSetName="ByProjectId")] | ||
param ( | ||
[Parameter(ValueFromPipelineByPropertyName = $true, Mandatory = $true, ParameterSetName = "ByRepositoryId")] | ||
[Alias("Id")] | ||
[guid] $RepositoryId, | ||
|
||
[int] $MaximumAgeDays = 90 | ||
) | ||
|
||
DynamicParam { | ||
try { [void] $RepositoryId } | ||
catch { $RepositoryId = $null } | ||
|
||
if ($null -ne $RepositoryId) { | ||
$dynamic = _buildProjectNameDynamicParam -ParameterSetName "ByRepositoryId" -Mandatory $true | ||
} else { | ||
$dynamic = _buildProjectNameDynamicParam -ParameterSetName "ByProjectId" -Mandatory $false | ||
} | ||
|
||
$dynamic | ||
} | ||
|
||
process { | ||
# Bind the parameter to a friendly variable | ||
$ProjectName = $PSBoundParameters["ProjectName"] | ||
$maximumAge = (Get-Date).AddDays(-$MaximumAgeDays) | ||
|
||
try { | ||
if ($RepositoryId) # Retrieve single repository | ||
{ | ||
Write-Verbose "Retrieving Branches for Repository ID $RepositoryId in Project $ProjectName" | ||
$repository = Get-VSTeamGitRepository -ProjectName $ProjectName -RepositoryId $RepositoryId | ||
$branches = $repository | Get-VSTeamGitRef -Filter "heads" | ||
foreach ($branch in $branches) | ||
{ | ||
Write-Verbose "Processing Branch $($branch.RefName)" | ||
$isStale = ((Get-VSTeamGitCommit -ProjectName $ProjectName -RepositoryId $RepositoryId -FromDate $maximumAge -Top 1) | Measure-Object).Count -ne 1 | ||
if ($isStale) | ||
{ | ||
Write-Verbose "Branch $($branch.RefName) is stale!" | ||
$branchStats = Get-VSTeamGitStat -ProjectName $ProjectName -RepositoryId $RepositoryId -BranchName ($branch.RefName -replace 'refs/heads/', '') | ||
|
||
$object = | ||
[PSCustomObject]@{ | ||
ProjectName = $ProjectName | ||
RepositoryName = $repository.Name | ||
BranchName = ($branch.RefName -replace 'refs/heads/', '') | ||
Creator = $branch.Creator | ||
LastCommitId = $branchStats.commit.commitId | ||
LastCommitter = $branchStats.commit.committer.name | ||
LastCommitDate = $branchStats.commit.committer.date | ||
Ahead = $branchStats.aheadCount | ||
Behind = $branchStats.behindCount | ||
} | ||
|
||
_applyTypes $object "Team.GitStaleBranch" | ||
Write-Output $object | ||
} | ||
} | ||
} elseif ($ProjectName) { # Retrieve whole project (recursive) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @MichelZ it would be better if we use cmdlets that are more atomic and simpler. When this is changed that way I would let the scripting person choose to add the logic himself. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But why though? This is optional, and you're free to use the repoID if you want to... |
||
Write-Verbose "Retrieving Repositories for Project $ProjectName" | ||
$repos = Get-VSTeamGitRepository -ProjectName $ProjectName | ||
foreach ($repo in $repos) | ||
{ | ||
$staleBranchesResult = Get-VSTeamStaleBranch -ProjectName $ProjectName -RepositoryId $repo.Id -MaximumAgeDays $MaximumAgeDays | ||
foreach ($result in $staleBranchesResult) | ||
{ | ||
Write-Output $result | ||
} | ||
} | ||
} else { # Retrieve all projects (recursive) | ||
Write-Verbose "Retrieving all Projects" | ||
$projects = Get-VSTeamProject | ||
foreach ($project in $projects) | ||
{ | ||
$staleBranchesResult = Get-VSTeamStaleBranch -ProjectName $project.Name -MaximumAgeDays $MaximumAgeDays | ||
foreach ($result in $staleBranchesResult) | ||
{ | ||
Write-Output $result | ||
} | ||
} | ||
} | ||
} | ||
catch { | ||
throw $_ | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<Configuration> | ||
<ViewDefinitions> | ||
<View> | ||
<Name>Team.GitStaleBranch.ListView</Name> | ||
<ViewSelectedBy> | ||
<TypeName>Team.GitStaleBranch</TypeName> | ||
</ViewSelectedBy> | ||
<ListControl> | ||
<ListEntries> | ||
<ListEntry> | ||
<ListItems> | ||
<ListItem> | ||
<PropertyName>ProjectName</PropertyName> | ||
</ListItem> | ||
<ListItem> | ||
<PropertyName>RepositoryName</PropertyName> | ||
</ListItem> | ||
<ListItem> | ||
<PropertyName>BranchName</PropertyName> | ||
</ListItem> | ||
<ListItem> | ||
<PropertyName>Creator</PropertyName> | ||
</ListItem> | ||
<ListItem> | ||
<PropertyName>LastCommitId</PropertyName> | ||
</ListItem> | ||
<ListItem> | ||
<PropertyName>LastCommitter</PropertyName> | ||
</ListItem> | ||
<ListItem> | ||
<PropertyName>LastCommitDate</PropertyName> | ||
</ListItem> | ||
<ListItem> | ||
<PropertyName>Behind</PropertyName> | ||
</ListItem> | ||
<ListItem> | ||
<PropertyName>Ahead</PropertyName> | ||
</ListItem> | ||
</ListItems> | ||
</ListEntry> | ||
</ListEntries> | ||
</ListControl> | ||
</View> | ||
</ViewDefinitions> | ||
</Configuration> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<Configuration> | ||
<ViewDefinitions> | ||
<View> | ||
<Name>Team.GitStaleBranch.TableView</Name> | ||
<ViewSelectedBy> | ||
<TypeName>Team.GitStaleBranch</TypeName> | ||
</ViewSelectedBy> | ||
<TableControl> | ||
<TableHeaders> | ||
<TableColumnHeader> | ||
<Label>ProjectName</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>RepositoryName</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>BranchName</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>Creator</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>LastCommitId</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>LastCommitter</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>LastCommitDate</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>Ahead</Label> | ||
</TableColumnHeader> | ||
<TableColumnHeader> | ||
<Label>Behind</Label> | ||
</TableColumnHeader> | ||
</TableHeaders> | ||
<TableRowEntries> | ||
<TableRowEntry> | ||
<TableColumnItems> | ||
<TableColumnItem> | ||
<PropertyName>ProjectName</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<PropertyName>RepositoryName</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<PropertyName>BranchName</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<PropertyName>Creator</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<PropertyName>LastCommitId</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<PropertyName>LastCommitter</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<PropertyName>LastCommitDate</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<PropertyName>Ahead</PropertyName> | ||
</TableColumnItem> | ||
<TableColumnItem> | ||
<PropertyName>Behind</PropertyName> | ||
</TableColumnItem> | ||
</TableColumnItems> | ||
</TableRowEntry> | ||
</TableRowEntries> | ||
</TableControl> | ||
</View> | ||
</ViewDefinitions> | ||
</Configuration> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<Types> | ||
<Type> | ||
<Name>Team.GitStaleBranch</Name> | ||
<Members> | ||
<MemberSet> | ||
<Name>PSStandardMembers</Name> | ||
<Members> | ||
<PropertySet> | ||
<Name>DefaultDisplayPropertySet</Name> | ||
<ReferencedProperties> | ||
<Name>ProjectName</Name> | ||
<Name>RepositoryName</Name> | ||
<Name>BranchName</Name> | ||
<Name>Creator</Name> | ||
<Name>LastCommitId</Name> | ||
<Name>LastCommitter</Name> | ||
<Name>LastCommitDate</Name> | ||
<Name>Ahead</Name> | ||
<Name>Behind</Name> | ||
</ReferencedProperties> | ||
</PropertySet> | ||
</Members> | ||
</MemberSet> | ||
</Members> | ||
</Type> | ||
</Types> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you trying to do here?