Skip to content

Instantly share code, notes, and snippets.

@igoravl
Last active February 9, 2023 06:05
Show Gist options
  • Save igoravl/702659092e51bb90f116f8f8b4650cc9 to your computer and use it in GitHub Desktop.
Save igoravl/702659092e51bb90f116f8f8b4650cc9 to your computer and use it in GitHub Desktop.
Modifies the settings of a Git repository in Azure DevOps Services, using cmdlets from TfsCmdlets
#requires -Modules TfsCmdlets
<#
.SYNOPSIS
Modifies the settings of a Git repository.
#>
Function Set-TfsGitRepositorySetting {
[CmdletBinding(SupportsShouldProcess = $true)]
Param (
# Specifies the name of the setting to modify.
[Parameter(Mandatory = $true, Position = 0)]
[ValidateSet('ForksEnabled', 'WitMentionsEnabled', 'WitResolutionMentionsEnabled', 'WitTransitionsSticky', 'RepoCreatedBranchesManagePermissionsEnabled', 'StrictVoteMode')]
[string]$Setting,
# Specifies the value of the setting.
[Parameter(Mandatory = $true, Position = 1)]
[bool]$Value,
# Specifies the target Git repository. Valid values are the name of the repository, its ID (a GUID), or a Microsoft.TeamFoundation.SourceControl.WebApi.GitRepository object obtained by e.g. a call to Get-TfsGitRepository. When omitted, defaults to the team project name (i.e. the default repository).
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[object]$Repository,
# Specifies the name of the Team Project, its ID (a GUID), or a Microsoft.TeamFoundation.Core.WebApi.TeamProject object to connect to. When omitted, it defaults to the connection set by Connect-TfsTeamProject (if any). For more details, see the Get-TfsTeamProject cmdlet.
[Parameter()]
[object]$Project,
# Specifies the URL to the Team Project Collection or Azure DevOps Organization to connect to, a TfsTeamProjectCollection object (Windows PowerShell only), or a VssConnection object. You can also connect to an Azure DevOps Services organizations by simply providing its name instead of the full URL. For more details, see the Get-TfsTeamProjectCollection cmdlet. When omitted, it defaults to the connection set by Connect-TfsTeamProjectCollection (if any).
[Parameter()]
[object]$Collection
)
Process {
# Normalizes the repository object
$Repository = Get-TfsGitRepository -Repository $Repository
# Extracts the repository id and the project name
$repoId = $Repository.id
$project = $Repository.TeamProject
# Issues a call to the web page to retrieve the cookies and the request verification token
$resp = (Invoke-TfsRestApi "$project/_settings/repositories?repo=$repoId" -AsTask).Result
# Extracts the page contents
$pageData = $resp.Content.ReadAsStringAsync().Result
# Extracts the cookies from the response headers and create a request header
$cookies = (($resp.Headers | Where-Object key -eq 'Set-Cookie').Value -join '; ').Replace('; path=/; secure; HttpOnly', '')
# Extracts the request verification token from the page contents
$token = (Select-String -InputObject $pageData 'name="__RequestVerificationToken" value="(.+?)"').Matches.Groups[1].Value
# Creates the body of the request
$body = "repositoryId=$repoId&option=%7B%22key%22%3A%22${Setting}%22%2C%22value%22%3A$($Value.ToString().ToLowerInvariant())%7D&__RequestVerificationToken=$token"
if (-not $PSCmdlet.ShouldProcess("Repository $project/$($Repository.Name)", "Set $Setting to $Value")) {
return
}
# Issues the request to update the repository option
Invoke-TfsRestApi "$project/_api/_versioncontrol/UpdateRepositoryOption?__v=5&repositoryId=$repoId" `
-Body $body -Method POST -RequestContentType "application/x-www-form-urlencoded" -AdditionalHeaders @{ cookie = $cookies } | Out-Null
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment