-
Notifications
You must be signed in to change notification settings - Fork 155
Added Update-VSTeamGitRepositoryDefaultBranch #475
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
Merged
SebastianSchuetze
merged 9 commits into
MethodsAndPractices:trunk
from
rbleattler:trunk
Aug 24, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4593943
- Added Update-VSTeamGitRepositoryDefaultBranch to allow for changing…
8c7f2a3
Clean up comments, and fixed/updated urls
ad000a4
Added logic to only try to import vstem-lib.dll if exists and is not …
8ce46b7
Added fake Get-VSTeamGitRepository call (since pester doesn't recogni…
bdec189
Merge pull request #1 from MethodsAndPractices/trunk
rbleattler 6d554fb
- Fixed default branch name check.
a8ae443
- Added mock for Get-VSTeamGitRef
e4a2803
- Added sample file for gitref call in Update-VSTeamGitRepositoryDefa…
3f3aebc
removed some line in changelog
SebastianSchuetze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Update the default branch of an existing respository. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
Tests/SampleFiles/Get-VSTeamGitRef_for_Update-VSTeamGitRepositoryDefaultBranch.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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": "test@test.com", | ||
| "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": "test@test.com", | ||
| "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 | ||
| } |
60 changes: 60 additions & 0 deletions
60
Tests/function/tests/Update-VSTeamGitRepositoryDefaultBranch.Tests.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.