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

Added Update-VSTeamGitRepositoryDefaultBranch #475

Merged
merged 9 commits into from
Aug 24, 2022
67 changes: 67 additions & 0 deletions .docs/Update-VSTeamGitRepositoryDefaultBranch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!-- #include "./common/header.md" -->

# Update-VSTeamGitRepositoryDefaultBranch

## SYNOPSIS

<!-- #include "./synopsis/Update-VSTeamGitRepositoryDefaultBranch.md" -->

## SYNTAX

## DESCRIPTION

<!-- #include "./synopsis/Update-VSTeamGitRepositoryDefaultBranch.md" -->

## EXAMPLES

### Example 1

```powershell
Update-VSTeamGitRepositoryDefaultBranch -ProjectName MyProject -Name MyRepo -DefaultBranch develop
```

This command sets the default branch for the 'MyRepo' git repository in the 'MyProject' project to 'develop'.

## PARAMETERS

### ProjectName

The name of the project which the target repository is a part of.

```yaml
Type: String
Required: True
Accept pipeline input: true #(ByPropertyName)
```

### Name

The name of the target repository.

```yaml
Type: String
Required: True
Accept pipeline input: true #(ByPropertyName)
```

### DefaultBranch

The new branch name to apply to the target repository.

```yaml
Type: String
Required: True
Accept pipeline input: true #(ByPropertyName)
```

<!-- #include "./params/projectName.md" -->

## INPUTS

## OUTPUTS

## NOTES

<!-- #include "./common/prerequisites.md" -->

## RELATED LINKS
1 change: 1 addition & 0 deletions .docs/synopsis/Update-VSTeamGitRepositoryDefaultBranch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Update the default branch of an existing respository.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 7.8.0
- Added Update-VSTeamGitRepositoryDefaultBranch to allow for changing the default branch of a repository
- Fixed bad link in README.md
- Bumped manifest version
- Cleaned up comments/debug messages

## 7.7.0
Merged [Pull Request](https://github.com/MethodsAndPractices/vsteam/pull/470) from [Joshua Davis](https://github.com/a11smiles) the following:

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
</p>
<p align="center">
<a href="github/CONTRIBUTING.md">Contribute</a> |
<a href="#buildin-gmodule">Building Module</a> |
<a href="#building-module">Building Module</a> |
<a href="#mantainers">Maintainers</a>
</p>

Expand Down
64 changes: 64 additions & 0 deletions Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Sets the default branch for a Git repository in your Azure DevOps or Team Foundation Server account.
#
# Get-VSTeamOption 'git' 'repositories'
# id : 225f7195-f9c7-4d14-ab28-a83f7ff77e1f
# area : git
# resourceName : repositories
# routeTemplate : {project}/_apis/{area}/{resource}/{repositoryId}
# https://docs.microsoft.com/en-us/rest/api/azure/devops/git/repositories/update?view=azure-devops-rest-6.1&tabs=HTTP
function Update-VSTeamGitRepositoryDefaultBranch {
[CmdletBinding(HelpUri = 'https://methodsandpractices.github.io/vsteam-docs/docs/modules/vsteam/commands/Update-VSTeamGitRepositoryDefaultBranch', SupportsShouldProcess = $true)]
param(
[parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string] $Name,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[vsteam_lib.ProjectValidateAttribute($false)]
[ArgumentCompleter([vsteam_lib.ProjectCompleter])]
[string] $ProjectName,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string] $DefaultBranch
)
begin {
if ($DefaultBranch -notlike "*refs/heads*") {
$DefaultBranch = 'refs/heads/{0}' -f $DefaultBranch
}
try {
$Repo = Get-VSTeamGitRepository -Name $Name -ProjectName $ProjectName
} catch {
Write-Warning "A repo named $Name could not be found in the project $ProjectName..."
throw $PSItem.Exception.Message
}
$Refs = Get-VSTeamGitRef -RepositoryID $Repo.Id -ProjectName $ProjectName
$BranchNames = $Refs.Name
if ($DefaultBranch -notin $BranchNames) {
$rawDefaultBranch = $DefaultBranch.replace('refs/heads/', $null)
throw "No branch named $rawDefaultBranch was found..."
}
}
process {
$body = @{
defaultBranch = "$DefaultBranch"
} | ConvertTo-Json -Compress
try {
# Call the REST API
if ($PSCmdlet.ShouldProcess("$ProjectName", "Setting default branch to $DefaultBranch")) {
$Parameters = @{
Method = 'PATCH'
ProjectName = $ProjectName
Area = "git"
Resource = "repositories"
id = $Repo.Id
Body = $body
Version = $(_getApiVersion Git)
}
$resp = _callApi @Parameters

}
} catch {
_handleException $_
}
}
end {
Write-Output $resp
}
}
2 changes: 1 addition & 1 deletion Source/VSTeam.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'VSTeam.psm1'

# Version number of this module.
ModuleVersion = '7.7.0'
ModuleVersion = '7.8.0'

# Supported PSEditions
CompatiblePSEditions = @('Core', 'Desktop')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"value": [
{
"name": "refs/heads/master",
"objectId": "0e129ba8-aef1-41d7-ba08-0febf7da4a1d",
"creator": {
"displayName": "Test User",
"url": "https://spsprodcus3.vssps.visualstudio.com/00000000-0000-0000-0000-000000000000/_apis/Identities/00000000-0000-0000-0000-000000000000",
"_links": {
"avatar": {
"href": "https://dev.azure.com/Test/_apis/GraphProfile/MemberAvatars/redacted"
}
},
"id": "00000000-0000-0000-0000-000000000000",
"uniqueName": "[email protected]",
"imageUrl": "https://dev.azure.com/Test/_api/_common/identityImage?id=00000000-0000-0000-0000-000000000000",
"descriptor": "redacted"
},
"url": "https://dev.azure.com/Test/00000000-0000-0000-0000-000000000000/_apis/git/repositories/00000000-0000-0000-0000-000000000000/refs?filter=heads%2Fmaster"
},
{
"name": "refs/heads/develop",
"objectId": "0e129ba8-aef1-41d7-ba08-0febf7da4a1d",
"creator": {
"displayName": "Test User",
"url": "https://spsprodcus3.vssps.visualstudio.com/00000000-0000-0000-0000-000000000000/_apis/Identities/00000000-0000-0000-0000-000000000000",
"_links": {
"avatar": {
"href": "https://dev.azure.com/Test/_apis/GraphProfile/MemberAvatars/redacted"
}
},
"id": "00000000-0000-0000-0000-000000000000",
"uniqueName": "[email protected]",
"imageUrl": "https://dev.azure.com/Test/_api/_common/identityImage?id=00000000-0000-0000-0000-000000000000",
"descriptor": "redacted"
},
"url": "https://dev.azure.com/Test/00000000-0000-0000-0000-000000000000/_apis/git/repositories/00000000-0000-0000-0000-000000000000/refs?filter=heads%2Fdevelop"
}
],
"count": 1
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Set-StrictMode -Version Latest

Describe "VSTeamGitRepository" {
BeforeAll {
. "$PSScriptRoot\_testInitialize.ps1" $PSCommandPath

# Without adding this call, an exception is thrown stating that Get-VSTeamGitRepository is a part of VSTeam but the module could not be loaded. Because we're not testing this function, but it is used in the function we are testing I created it here to facilitate the tests.
function Get-VSTeamGitRepository {
param(
$Name,
$ProjectName
)
$Repo = _callAPI -Method Get -ProjectName $ProjectName `
-Area "git" `
-Resource "repositories" `
-id $Name `
-Version $(_getApiVersion Git)
$Repo
}

Mock Get-VSTeamGitRef { (Open-SampleFile 'Get-VSTeamGitRef_for_Update-VSTeamGitRepositoryDefaultBranch.json').value } -ParameterFilter {
$ProjectName -like "*Peopletracker*"
}
Mock Invoke-RestMethod { Open-SampleFile 'Get-VSTeamGitRepository.json' }
Mock Invoke-RestMethod { Open-SampleFile 'Get-VSTeamGitRepository-ProjectNamePeopleTracker-NamePeopleTracker.json' } -ParameterFilter {
$Uri -like "*00000000-0000-0000-0000-000000000000*" -or $URI -like "*Peopletracker*"
}

Mock Invoke-RestMethod { throw [System.Net.WebException] } -ParameterFilter {
$Uri -like "*00000000-0000-0000-0000-000000000101*" -or
$Uri -like "*boom*"
}
}

Context 'Update-VSTeamGitRepositoryDefaultBranch' {
Context 'Services' {
BeforeAll {
## Arrange
Mock _getInstance { return 'https://dev.azure.com/Test' }
}

It "by name should update Git repo's default branch" {
## Act
Update-VSTeamGitRepositoryDefaultBranch -Name PeopleTracker -projectname PeopleTracker -DefaultBranch 'master'

## Assert
Should -Invoke Invoke-RestMethod -ParameterFilter {
$Method -eq 'Patch' -and $Uri -eq "https://dev.azure.com/Test/PeopleTracker/_apis/git/repositories/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Git)"
}
}

It 'by id should throw' {
{ Update-VSTeamGitRepositoryDefaultBranch -id 00000000-0000-0000-0000-000000000101 -projectname PeopleTracker -DefaultBranch 'develop' } | Should -Throw
}
It 'should throw if the branch does not exist' {
{ Update-VSTeamGitRepositoryDefaultBranch -id 00000000-0000-0000-0000-000000000101 -projectname PeopleTracker -DefaultBranch 'notarealbranch' } | Should -Throw
}
}
}
}
23 changes: 15 additions & 8 deletions Tests/function/tests/_testInitialize.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,18 @@ Import-Module SHiPS

$baseFolder = "$PSScriptRoot/../../.."
$sampleFiles = "$PSScriptRoot/../../SampleFiles"

Add-Type -Path "$baseFolder/dist/bin/vsteam-lib.dll"

# Changed to only import this type if the dll is missing and exists in the path, since PS throws an exception when trying to import a dll already in memory
$vsTeamDllPath = "$baseFolder/dist/bin/vsteam-lib.dll"
SebastianSchuetze marked this conversation as resolved.
Show resolved Hide resolved
if (Test-Path -Path $vsTeamDllPath) {
try {
Add-Type -Path $vsTeamDllPath -ErrorAction SilentlyContinue
} catch {
if ($PSItem.Exception.Message -ne 'Assembly with same name is already loaded') {
throw $PSItem.Exception.Message
}
}
}

$sut = (Split-Path -Leaf $testPath).Replace(".Tests.", ".")

Expand All @@ -21,8 +31,7 @@ $sut = (Split-Path -Leaf $testPath).Replace(".Tests.", ".")

if ($private.IsPresent) {
. "$baseFolder/Source/Private/$sut"
}
else {
} else {
. "$baseFolder/Source/Public/$sut"
}

Expand Down Expand Up @@ -50,12 +59,10 @@ function Open-SampleFile {

if ($ReturnValue.IsPresent) {
return $(Get-Content "$sampleFiles\$file" -Raw | ConvertFrom-Json).value
}
else {
} else {
if ($index -eq -1) {
return $(Get-Content "$sampleFiles\$file" -Raw | ConvertFrom-Json)
}
else {
} else {
return $(Get-Content "$sampleFiles\$file" -Raw | ConvertFrom-Json).value[$index]
}
}
Expand Down