From 4593943573ec7e8bae2a0274ae1be1ccbc9bab54 Mon Sep 17 00:00:00 2001 From: Bleattler Date: Tue, 16 Aug 2022 11:15:51 -0400 Subject: [PATCH 1/4] - Added Update-VSTeamGitRepositoryDefaultBranch to allow for changing the default branch of a repository. - Fixed bad link in README.md --- ...Update-VSTeamGitRepositoryDefaultBranch.md | 67 +++++++++++++++++++ ...Update-VSTeamGitRepositoryDefaultBranch.md | 1 + CHANGELOG.md | 3 + README.md | 2 +- ...pdate-VSTeamGitRepositoryDefaultBranch.ps1 | 61 +++++++++++++++++ ...VSTeamGitRepositoryDefaultBranch.Tests.ps1 | 62 +++++++++++++++++ 6 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 .docs/Update-VSTeamGitRepositoryDefaultBranch.md create mode 100644 .docs/synopsis/Update-VSTeamGitRepositoryDefaultBranch.md create mode 100644 Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1 create mode 100644 Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1 diff --git a/.docs/Update-VSTeamGitRepositoryDefaultBranch.md b/.docs/Update-VSTeamGitRepositoryDefaultBranch.md new file mode 100644 index 00000000..bdbaf645 --- /dev/null +++ b/.docs/Update-VSTeamGitRepositoryDefaultBranch.md @@ -0,0 +1,67 @@ + + +# Update-VSTeamGitRepositoryDefaultBranch + +## SYNOPSIS + + + +## SYNTAX + +## DESCRIPTION + + + +## 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) +``` + + + +## INPUTS + +## OUTPUTS + +## NOTES + + + +## RELATED LINKS diff --git a/.docs/synopsis/Update-VSTeamGitRepositoryDefaultBranch.md b/.docs/synopsis/Update-VSTeamGitRepositoryDefaultBranch.md new file mode 100644 index 00000000..014c537a --- /dev/null +++ b/.docs/synopsis/Update-VSTeamGitRepositoryDefaultBranch.md @@ -0,0 +1 @@ +Update the default branch of an existing respository. \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 80a0419a..fa46bd28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +- Added Update-VSTeamGitRepositoryDefaultBranch to allow for changing the default branch of a repository. +- Fixed bad link in README.md + ## 7.7.0 Merged [Pull Request](https://github.com/MethodsAndPractices/vsteam/pull/470) from [Joshua Davis](https://github.com/a11smiles) the following: diff --git a/README.md b/README.md index fb916859..49bfd72d 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@

Contribute | - Building Module | + Building Module | Maintainers

diff --git a/Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1 b/Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1 new file mode 100644 index 00000000..3980867d --- /dev/null +++ b/Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1 @@ -0,0 +1,61 @@ +# 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} +# http://bit.ly/Add-VSTeamGitRepository +function Update-VSTeamGitRepositoryDefaultBranch { + [CmdletBinding()] + 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 { + try { + # $Repo = Get-VSTeamGitRepository -Name $Name -ProjectName $ProjectName + $Repo = _callAPI -Method Get -ProjectName $ProjectName ` + -Area "git" ` + -Resource "repositories" ` + -id $Name ` + -Version $(_getApiVersion Git) + } catch { + Write-Warning "A repo named $Name could not be found in the project $ProjectName..." + throw $PSItem.Exception.Message + } + if ($DefaultBranch -notlike "*refs/head*") { + $DefaultBranch = 'refs/head/{0}' -f $DefaultBranch + } + } + process { + + + $body = @{ + # name = $Name + defaultBranch = "refs/head/$DefaultBranch" + } + try { + # Call the REST API + $resp = _callAPI -Method PATCH -ProjectName $ProjectName ` + -Area "git" ` + -Resource "repositories" ` + -id $Repo.Id ` + -Body $body ` + -Version $(_getApiVersion Git) + # Storing the object before you return it cleaned up the pipeline. + # When I just write the object from the constructor each property + # seemed to be written + # $repo = [vsteam_lib.GitRepository]::new($resp, $ProjectName) + Write-Output $resp + } catch { + _handleException $_ + } + } +} \ No newline at end of file diff --git a/Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1 b/Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1 new file mode 100644 index 00000000..ab67f7ee --- /dev/null +++ b/Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1 @@ -0,0 +1,62 @@ +Set-StrictMode -Version Latest + +Describe "VSTeamGitRepository" { + BeforeAll { + . "$PSScriptRoot\_testInitialize.ps1" $PSCommandPath + . "$PSScriptRoot\..\..\..\Source\Private\common.ps1" + + 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 'develop' + + ## Assert + Should -Invoke Invoke-RestMethod -ParameterFilter { + Write-Debug "Method : $Method" + Write-Debug "Uri : $Uri" + $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 + } + } + + # Context 'Server' { + # BeforeAll { + # ## Arrange + # Mock _getInstance { return 'https://localhost:8080/tfs' } + # } + + # It "by name should update Git repo's default branch" { + # ## Act + # Update-VSTeamGitRepositoryDefaultBranch -Name PeopleTracker -projectname PeopleTracker -DefaultBranch 'develop' + + # ## Assert + # Should -Invoke Invoke-RestMethod -ParameterFilter { + # Write-Debug "Method : $Method" + # Write-Debug "Uri : $Uri" + # $Method -eq 'Patch' -and $Uri -eq "https://localhost:8080/tfs/PeopleTracker/PeopleTracker/_apis/git/repositories/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Git)" + # } + # } + # } + } +} \ No newline at end of file From 8c7f2a38d9c3b14b069c5eb48edfafd884484674 Mon Sep 17 00:00:00 2001 From: Bleattler Date: Thu, 18 Aug 2022 09:42:51 -0400 Subject: [PATCH 2/4] Clean up comments, and fixed/updated urls --- ...pdate-VSTeamGitRepositoryDefaultBranch.ps1 | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1 b/Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1 index 3980867d..71ea182b 100644 --- a/Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1 +++ b/Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1 @@ -5,9 +5,9 @@ # area : git # resourceName : repositories # routeTemplate : {project}/_apis/{area}/{resource}/{repositoryId} -# http://bit.ly/Add-VSTeamGitRepository +# 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()] + [CmdletBinding(HelpUri = 'https://methodsandpractices.github.io/vsteam-docs/docs/modules/vsteam/commands/Update-VSTeamGitRepositoryDefaultBranch')] param( [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)] [string] $Name, @@ -20,12 +20,7 @@ function Update-VSTeamGitRepositoryDefaultBranch { ) begin { try { - # $Repo = Get-VSTeamGitRepository -Name $Name -ProjectName $ProjectName - $Repo = _callAPI -Method Get -ProjectName $ProjectName ` - -Area "git" ` - -Resource "repositories" ` - -id $Name ` - -Version $(_getApiVersion Git) + $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 @@ -35,24 +30,17 @@ function Update-VSTeamGitRepositoryDefaultBranch { } } process { - - $body = @{ - # name = $Name defaultBranch = "refs/head/$DefaultBranch" } try { - # Call the REST API + # Call the REST API $resp = _callAPI -Method PATCH -ProjectName $ProjectName ` -Area "git" ` -Resource "repositories" ` -id $Repo.Id ` -Body $body ` -Version $(_getApiVersion Git) - # Storing the object before you return it cleaned up the pipeline. - # When I just write the object from the constructor each property - # seemed to be written - # $repo = [vsteam_lib.GitRepository]::new($resp, $ProjectName) Write-Output $resp } catch { _handleException $_ From ad000a41e07c72a77e6817dfbfa599b159a9be03 Mon Sep 17 00:00:00 2001 From: Bleattler Date: Thu, 18 Aug 2022 09:44:22 -0400 Subject: [PATCH 3/4] Added logic to only try to import vstem-lib.dll if exists and is not imported. --- Tests/function/tests/_testInitialize.ps1 | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Tests/function/tests/_testInitialize.ps1 b/Tests/function/tests/_testInitialize.ps1 index a222ff68..c0876152 100644 --- a/Tests/function/tests/_testInitialize.ps1 +++ b/Tests/function/tests/_testInitialize.ps1 @@ -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" +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.", ".") @@ -21,8 +31,7 @@ $sut = (Split-Path -Leaf $testPath).Replace(".Tests.", ".") if ($private.IsPresent) { . "$baseFolder/Source/Private/$sut" -} -else { +} else { . "$baseFolder/Source/Public/$sut" } @@ -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] } } From 8ce46b73b92ddda82bfd64eb85f53acf3f3ca062 Mon Sep 17 00:00:00 2001 From: Bleattler Date: Thu, 18 Aug 2022 09:49:36 -0400 Subject: [PATCH 4/4] Added fake Get-VSTeamGitRepository call (since pester doesn't recognize it as something when mocked) Removed call to dot source common.ps1 Removed debug messages Cleaned up comments Bumped Version Updated Changelog --- CHANGELOG.md | 5 ++- Source/VSTeam.psd1 | 2 +- ...VSTeamGitRepositoryDefaultBranch.Tests.ps1 | 38 ++++++++----------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa46bd28..0a43ef7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,10 @@ # Changelog -- Added Update-VSTeamGitRepositoryDefaultBranch to allow for changing the default branch of a repository. +## 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: diff --git a/Source/VSTeam.psd1 b/Source/VSTeam.psd1 index 3256c538..f7744f8d 100644 --- a/Source/VSTeam.psd1 +++ b/Source/VSTeam.psd1 @@ -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') diff --git a/Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1 b/Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1 index ab67f7ee..768c69b7 100644 --- a/Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1 +++ b/Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1 @@ -3,8 +3,21 @@ Set-StrictMode -Version Latest Describe "VSTeamGitRepository" { BeforeAll { . "$PSScriptRoot\_testInitialize.ps1" $PSCommandPath - . "$PSScriptRoot\..\..\..\Source\Private\common.ps1" - + + # 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 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*" @@ -29,8 +42,6 @@ Describe "VSTeamGitRepository" { ## Assert Should -Invoke Invoke-RestMethod -ParameterFilter { - Write-Debug "Method : $Method" - Write-Debug "Uri : $Uri" $Method -eq 'Patch' -and $Uri -eq "https://dev.azure.com/Test/PeopleTracker/_apis/git/repositories/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Git)" } } @@ -39,24 +50,5 @@ Describe "VSTeamGitRepository" { { Update-VSTeamGitRepositoryDefaultBranch -id 00000000-0000-0000-0000-000000000101 -projectname PeopleTracker -DefaultBranch 'develop' } | Should -Throw } } - - # Context 'Server' { - # BeforeAll { - # ## Arrange - # Mock _getInstance { return 'https://localhost:8080/tfs' } - # } - - # It "by name should update Git repo's default branch" { - # ## Act - # Update-VSTeamGitRepositoryDefaultBranch -Name PeopleTracker -projectname PeopleTracker -DefaultBranch 'develop' - - # ## Assert - # Should -Invoke Invoke-RestMethod -ParameterFilter { - # Write-Debug "Method : $Method" - # Write-Debug "Uri : $Uri" - # $Method -eq 'Patch' -and $Uri -eq "https://localhost:8080/tfs/PeopleTracker/PeopleTracker/_apis/git/repositories/00000000-0000-0000-0000-000000000000?api-version=$(_getApiVersion Git)" - # } - # } - # } } } \ No newline at end of file