Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
67 changes: 67 additions & 0 deletions .docs/Update-VSTeamGitRepositoryDefaultBranch.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff line change
@@ -0,0 +1 @@
Update the default branch of an existing respository.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff 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
49 changes: 49 additions & 0 deletions Source/Public/Update-VSTeamGitRepositoryDefaultBranch.ps1
Original file line numberDiff line numberDiff line change
@@ -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 $_
}
}
}
2 changes: 1 addition & 1 deletion Source/VSTeam.psd1
Original file line numberDiff line numberDiff 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 numberDiff line numberDiff line change
@@ -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
}
}
}
}
23 changes: 15 additions & 8 deletions Tests/function/tests/_testInitialize.ps1
Original file line numberDiff line numberDiff 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"
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 DownExpand 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