Modules/AzureDevOpsDsc.Common/Api/Functions/Private/Helper/ACL/ConvertTo-FormattedToken.ps1
|
<#
.SYNOPSIS Formats the token based on its type. .DESCRIPTION The ConvertTo-FormattedToken function is used to format a token based on its type. It takes a token as input and returns the formatted token string. .PARAMETER Token The token to format. This parameter is mandatory and accepts an array of objects. .EXAMPLE $token = @{ type = 'GitProject' projectId = 'myProject' repositoryId = 'myRepo' } ConvertTo-FormattedToken -Token $token # Output: "repoV2/myProject/myRepo" .NOTES This function assumes that the token type is either 'GitOrganization', 'GitProject', or 'GitRepository'. If the token type is not one of these, the function will not format the token and will return an empty string. #> Function ConvertTo-FormattedToken { [CmdletBinding()] param ( # Define a mandatory parameter named 'Token' of type Object array [Parameter(Mandatory = $true)] [Object[]]$Token ) # Output verbose message indicating the function has started Write-Verbose "[ConvertTo-FormattedToken] Started." # Initialize variable to store formatted token string $string = "" # Determine the type of the token and format accordingly switch ($Token) { # If the token type is 'GitOrganization' {$_.type -eq 'GitOrganization'} { $string = 'repoV2' break } # If the token type is 'GitProject' {$_.type -eq 'GitProject'} { $string = 'repoV2/{0}' -f $Token.projectId break } # If the token type is 'GitRepository' {$_.type -eq 'GitRepository'} { $string = 'repoV2/{0}/{1}' -f $Token.projectId, $Token.RepoId break } # If the token type is 'GitBranch' — each '/'-delimited ref segment is hex/UTF-16LE # encoded separately by ConvertTo-GitRefToken (a branch folder such as 'release/' falls # out of this for free, since it is just the same per-segment encoding one segment short). {$_.type -eq 'GitBranch'} { $string = 'repoV2/{0}/{1}/refs/heads/{2}' -f $Token.projectId, $Token.RepoId, (ConvertTo-GitRefToken -RefName $Token.BranchName) break } # If the token type is 'GitTag' {$_.type -eq 'GitTag'} { $string = 'repoV2/{0}/{1}/refs/tags/{2}' -f $Token.projectId, $Token.RepoId, (ConvertTo-GitRefToken -RefName $Token.TagName) break } # If the token type is 'CSS' {$_.type -eq 'CSS'} { $string = $(($Token.Identifiers | ForEach-Object { "vstfs:///Classification/Node/{0}" -f $_.identifier }) -join ':') break } # If the token type is 'Iteration' {$_.type -eq 'Iteration'} { $string = $(($Token.Identifiers | ForEach-Object { "vstfs:///Classification/Node/{0}" -f $_.identifier }) -join ':') break } # Work item queries — $/{projectId}, plus one folder id per level beneath it {$_.type -eq 'Query'} { $string = '$/{0}' -f $Token.ProjectId if ($Token.Identifiers) { $string += ($Token.Identifiers | ForEach-Object { '/{0}' -f $_.identifier }) -join '' } break } # Project-level permissions {$_.type -eq 'Project'} { $string = '$PROJECT:vstfs:///Classification/TeamProject/{0}' -f $Token.ProjectId break } # Tagging permissions — '/{projectId}' {$_.type -eq 'Tagging'} { $string = '/{0}' -f $Token.ProjectId break } # Analytics permissions — '$/{projectId}' {$_.type -eq 'Analytics'} { $string = '$/{0}' -f $Token.ProjectId break } # AnalyticsViews permissions — '$/Shared/{projectId}' {$_.type -eq 'AnalyticsViews'} { $string = '$/Shared/{0}' -f $Token.ProjectId break } # Process permissions — org-wide root {$_.type -eq 'ProcessRoot'} { $string = '$PROCESS' break } # Process permissions — specific process ($PROCESS:{parentId}:{processId}) {$_.type -eq 'Process'} { $string = '$PROCESS:{0}:{1}' -f $Token.ParentProcessId, $Token.ProcessId break } # Build (Pipeline) permissions {$_.type -eq 'Build'} { $string = if ($Token.PipelineId) { '{0}/{1}' -f $Token.ProjectId, $Token.PipelineId } else { $Token.ProjectId } break } # Build folder permissions — the folder is addressed by path, not by id {$_.type -eq 'BuildFolder'} { $string = '{0}/{1}' -f $Token.ProjectId, $Token.FolderPath break } # ReleaseManagement — project root {$_.type -eq 'ReleaseRoot'} { $string = '{0}' -f $Token.ProjectId break } # ReleaseManagement — folder permissions, addressed by path, not by id {$_.type -eq 'ReleaseFolder'} { $string = '{0}/{1}' -f $Token.ProjectId, $Token.FolderPath break } # ReleaseManagement — definition permissions. The root folder is omitted from the token # rather than written out. {$_.type -eq 'ReleaseDefinition'} { $string = if ($Token.FolderPath) { '{0}/{1}/{2}' -f $Token.ProjectId, $Token.FolderPath, $Token.DefinitionId } else { '{0}/{1}' -f $Token.ProjectId, $Token.DefinitionId } break } # Library (VariableGroup) permissions {$_.type -eq 'Library'} { $string = if ($Token.SecureFileId) { 'Library/Project/{0}/SecureFile/{1}' -f $Token.ProjectId, $Token.SecureFileId } elseif ($Token.VariableGroupId) { 'Library/Project/{0}/VariableGroup/{1}' -f $Token.ProjectId, $Token.VariableGroupId } else { 'Library/Project/{0}' -f $Token.ProjectId } break } # ServiceEndpoints permissions {$_.type -eq 'ServiceEndpoints'} { $string = if ($Token.EndpointId) { 'endpoints/Project/{0}/endpoint/{1}' -f $Token.ProjectId, $Token.EndpointId } else { 'endpoints/Project/{0}' -f $Token.ProjectId } break } # DistributedTask — AgentPool {$_.type -eq 'AgentPool'} { $string = '{0}' -f $Token.PoolId break } # DistributedTask — Environment {$_.type -eq 'Environment'} { $string = if ($Token.EnvironmentId) { 'Environments/{0}/{1}' -f $Token.ProjectId, $Token.EnvironmentId } else { 'Environments/{0}' -f $Token.ProjectId } break } # Identity / Group Permission: token format is {projectId}\{groupOriginId} {$_.type -eq 'Identity'} { $string = '{0}\{1}' -f $Token.projectId, $Token.groupId break } # Generic / passthrough — token stored verbatim {$_.type -eq 'Generic'} { $string = $Token.TokenValue break } } # Output verbose message with the token value Write-Verbose "[ConvertTo-FormattedToken] Token: $string" # Return the formatted token string return $string } |