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..0a43ef7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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: 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..71ea182b --- /dev/null +++ b/Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1 @@ -0,0 +1,49 @@ +# 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')] + 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 + } 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 = @{ + 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) + Write-Output $resp + } catch { + _handleException $_ + } + } +} \ No newline at end of file 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 new file mode 100644 index 00000000..768c69b7 --- /dev/null +++ b/Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1 @@ -0,0 +1,54 @@ +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 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 { + $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 + } + } + } +} \ No newline at end of file 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] } }