Skip to content
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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions .docs/Get-VSTeamStaleBranch.md
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
1 change: 1 addition & 0 deletions .docs/synopsis/Get-VSTeamStaleBranch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Retrieve Stale Branches
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 6.4.5

Merged [Pull Request](https://github.com/DarqueWarrior/vsteam/pull/269) from [Michel Zehnder](https://github.com/MichelZ) which included the following:

- Add Get-VSTeamGitStaleBranch to retrieve branches which have not been committed to recently (default: 90 days)

## 6.4.4

Merged [Pull Request](https://github.com/DarqueWarrior/vsteam/pull/257) from [Michel Zehnder](https://github.com/MichelZ) which included the following:
Expand Down
6 changes: 5 additions & 1 deletion Source/Classes/VSTeamGitCommitRef.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ class VSTeamGitCommitRef : VSTeamLeaf {
$this.Committer = [VSTeamGitUserDate]::new($obj.committer, $ProjectName)
$this.CommitId = $obj.commitId
$this.Comment = $obj.comment
$this.RemoteUrl = $obj.remoteUrl

if ($obj.PSobject.Properties.Name -contains "remoteurl") {
$this.RemoteUrl = $obj.remoteUrl
}

$this.Url = $obj.url

$this._internalObj = $obj
Expand Down
89 changes: 89 additions & 0 deletions Source/Public/Get-VSTeamStaleBranch.ps1
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 }
Copy link
Collaborator

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?

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.
Could you please change it so that always a repository ID is needed and a project is used?

When this is changed that way I would let the scripting person choose to add the logic himself.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 $_
}
}
}
46 changes: 46 additions & 0 deletions Source/formats/Team.GitStaleBranch.ListView.ps1xml
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>
75 changes: 75 additions & 0 deletions Source/formats/Team.GitStaleBranch.TableView.ps1xml
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>
4 changes: 3 additions & 1 deletion Source/formats/_formats.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
"Team.WorkItemDeleted.ListView.ps1xml",
"Team.JobRequest.TableView.ps1xml",
"Team.GitCommitRef.TableView.ps1xml",
"Team.GitUserDate.TableView.ps1xml"
"Team.GitUserDate.TableView.ps1xml",
"Team.GitStaleBranch.ListView.ps1xml",
"Team.GitStaleBranch.TableView.ps1xml"
]
}
27 changes: 27 additions & 0 deletions Source/types/Team.GitStaleBranch.ps1xml
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>
Loading