|
| 1 | +function Get-ADORepository |
| 2 | +{ |
| 3 | + <# |
| 4 | + .Synopsis |
| 5 | + Gets repositories from Azure DevOps |
| 6 | + .Description |
| 7 | + Gets the repositories from Azure DevOps. |
| 8 | +
|
| 9 | + By default, this will return the project's git repositories. |
| 10 | +
|
| 11 | + Azure DevOps repositories can have more than one type of SourceProvider. |
| 12 | +
|
| 13 | + To list the Source Providers, use -SourceProvider |
| 14 | +
|
| 15 | + We can get repositories for a given -ProviderName |
| 16 | + .Example |
| 17 | + Get-ADORepository -Organization StartAutomating -Project PSDevOps |
| 18 | + .Link |
| 19 | + Remove-ADORepository |
| 20 | + .Link |
| 21 | + https://docs.microsoft.com/en-us/rest/api/azure/devops/build/source%20providers/list%20repositories?view=azure-devops-rest-5.1 |
| 22 | + #> |
| 23 | + [CmdletBinding(DefaultParameterSetName='git/repositories')] |
| 24 | + param( |
| 25 | + # The Organization |
| 26 | + [Parameter(Mandatory,ValueFromPipelineByPropertyName)] |
| 27 | + [Alias('Org')] |
| 28 | + [string] |
| 29 | + $Organization, |
| 30 | + |
| 31 | + # The Project |
| 32 | + [Parameter(Mandatory,ValueFromPipelineByPropertyName)] |
| 33 | + [string] |
| 34 | + $Project, |
| 35 | + |
| 36 | + # The Repository ID |
| 37 | + [Parameter(Mandatory,ParameterSetName='git/repositories/{repositoryId}',ValueFromPipelineByPropertyName)] |
| 38 | + [string] |
| 39 | + $RepositoryID, |
| 40 | + |
| 41 | + # If set, will list repository source providers |
| 42 | + [Parameter(Mandatory,ParameterSetName='sourceproviders',ValueFromPipelineByPropertyName)] |
| 43 | + [Alias('SourceProviders')] |
| 44 | + [switch] |
| 45 | + $SourceProvider, |
| 46 | + |
| 47 | + # The name of the Source Provider. This will get all repositories associated with the project. |
| 48 | + # If the -ProviderName is not TFVC or TFGit, an -EndpointID is also required |
| 49 | + [Parameter(Mandatory,ParameterSetName='sourceproviders/{ProviderName}/repositories',ValueFromPipelineByPropertyName)] |
| 50 | + [Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] |
| 51 | + [Alias('EndpointType')] |
| 52 | + [string] |
| 53 | + $ProviderName, |
| 54 | + |
| 55 | + # The name of the Source Provider. This will get all repositories associated with the project. |
| 56 | + # If the -ProviderName is not TFVC or TFGit, an -EndpointID is also required |
| 57 | + [Parameter(ParameterSetName='sourceproviders/{ProviderName}/repositories',ValueFromPipelineByPropertyName)] |
| 58 | + [Parameter(ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] |
| 59 | + [string] |
| 60 | + $EndpointID, |
| 61 | + |
| 62 | + # The name of the repository |
| 63 | + [Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] |
| 64 | + [string] |
| 65 | + $RepositoryName, |
| 66 | + |
| 67 | + # The path within the repository. |
| 68 | + # To use this parameter, -ProviderName is also required, and -EndpointID will be required if the -ProviderName is not TFVC or TFGit |
| 69 | + [Parameter(Mandatory,ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] |
| 70 | + [string] |
| 71 | + $Path, |
| 72 | + |
| 73 | + # The commit or branch. By default, Master. |
| 74 | + [Parameter(ParameterSetName='sourceProviders/{ProviderName}/filecontents',ValueFromPipelineByPropertyName)] |
| 75 | + [string] |
| 76 | + $CommitOrBranch = 'master', |
| 77 | + |
| 78 | + # If set, will include the parent repository |
| 79 | + [Parameter(ParameterSetName='git/repositories/{repositoryId}',ValueFromPipelineByPropertyName)] |
| 80 | + [switch] |
| 81 | + $IncludeParent, |
| 82 | + |
| 83 | + # If set, will get repositories from the recycle bin |
| 84 | + [Parameter(Mandatory,ParameterSetName='git/recycleBin/repositories',ValueFromPipelineByPropertyName)] |
| 85 | + [Alias('RecycleBin')] |
| 86 | + [switch] |
| 87 | + $Recycled, |
| 88 | + |
| 89 | + # The server. By default https://dev.azure.com/. |
| 90 | + # To use against TFS, provide the tfs server URL (e.g. http://tfsserver:8080/tfs). |
| 91 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 92 | + [uri] |
| 93 | + $Server = "https://dev.azure.com/", |
| 94 | + |
| 95 | + # The api version. By default, 5.1-preview. |
| 96 | + # If targeting TFS, this will need to change to match your server version. |
| 97 | + # See: https://docs.microsoft.com/en-us/azure/devops/integrate/concepts/rest-api-versioning?view=azure-devops |
| 98 | + [string] |
| 99 | + $ApiVersion = "5.1-preview", |
| 100 | + |
| 101 | + # A Personal Access Token |
| 102 | + [Alias('PAT')] |
| 103 | + [string] |
| 104 | + $PersonalAccessToken, |
| 105 | + |
| 106 | + # Specifies a user account that has permission to send the request. The default is the current user. |
| 107 | + # Type a user name, such as User01 or Domain01\User01, or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. |
| 108 | + [pscredential] |
| 109 | + [Management.Automation.CredentialAttribute()] |
| 110 | + $Credential, |
| 111 | + |
| 112 | + # Indicates that the cmdlet uses the credentials of the current user to send the web request. |
| 113 | + [Alias('UseDefaultCredential')] |
| 114 | + [switch] |
| 115 | + $UseDefaultCredentials, |
| 116 | + |
| 117 | + # Specifies that the cmdlet uses a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server. |
| 118 | + [uri] |
| 119 | + $Proxy, |
| 120 | + |
| 121 | + # Specifies a user account that has permission to use the proxy server that is specified by the Proxy parameter. The default is the current user. |
| 122 | + # Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated by the Get-Credential cmdlet. |
| 123 | + # This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command. |
| 124 | + [pscredential] |
| 125 | + [Management.Automation.CredentialAttribute()] |
| 126 | + $ProxyCredential, |
| 127 | + |
| 128 | + # Indicates that the cmdlet uses the credentials of the current user to access the proxy server that is specified by the Proxy parameter. |
| 129 | + # This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command. |
| 130 | + [switch] |
| 131 | + $ProxyUseDefaultCredentials |
| 132 | + ) |
| 133 | + |
| 134 | + begin { |
| 135 | + #region Copy Invoke-ADORestAPI parameters |
| 136 | + $invokeParams = & $getInvokeParameters $PSBoundParameters |
| 137 | + #endregion Copy Invoke-ADORestAPI parameters |
| 138 | + } |
| 139 | + |
| 140 | + process { |
| 141 | + $uri = # The URI is comprised of: |
| 142 | + @( |
| 143 | + "$server".TrimEnd('/') # the Server (minus any trailing slashes), |
| 144 | + $Organization # the Organization, |
| 145 | + $Project # the Project, |
| 146 | + '_apis' # the API Root ('_apis'), |
| 147 | + (. $ReplaceRouteParameter $PSCmdlet.ParameterSetName) |
| 148 | + # and any parameterized URLs in this parameter set. |
| 149 | + ) -as [string[]] -ne '' -join '/' |
| 150 | + $uri += '?' # The URI has a query string containing: |
| 151 | + $uri += @( |
| 152 | + if ($IncludeParent) { # includeParent=True (if it was set) |
| 153 | + "includeParent=True" |
| 154 | + } |
| 155 | + |
| 156 | + if ($EndpointID) { |
| 157 | + "serviceEndpointId=$EndpointID" |
| 158 | + } |
| 159 | + |
| 160 | + if ($repositoryName) { |
| 161 | + "repository=$repositoryName" |
| 162 | + "commitOrBranch=$CommitOrBranch" |
| 163 | + } |
| 164 | + |
| 165 | + if ($path) { |
| 166 | + "path=$path" |
| 167 | + } |
| 168 | + |
| 169 | + if ($ApiVersion) { # and the apiVersion |
| 170 | + "api-version=$ApiVersion" |
| 171 | + } |
| 172 | + ) -join '&' |
| 173 | + |
| 174 | + $InvokeParams.Property = |
| 175 | + @{ |
| 176 | + # Because we want to pipeline properly, also return the -Organization and -Project as properties. |
| 177 | + Organization = $Organization |
| 178 | + Project = $Project |
| 179 | + } |
| 180 | + |
| 181 | + $subTypeName = |
| 182 | + if ($psCmdlet.ParameterSetName -eq 'git/recycleBin/repositories') { |
| 183 | + '.Recycled' |
| 184 | + } |
| 185 | + elseif ($psCmdlet.ParameterSetName -eq 'sourceProviders') { |
| 186 | + '.SourceProvider' |
| 187 | + } |
| 188 | + elseif ($psCmdlet.ParameterSetName -eq 'sourceproviders/{ProviderName}/repositories') { |
| 189 | + ".$ProviderName.Repository" |
| 190 | + $invokeParams.ExpandProperty = 'repositories' |
| 191 | + $invokeParams.Property.EndpointID = $EndpointID |
| 192 | + $invokeParams.Property.ProviderName = $ProviderName |
| 193 | + } |
| 194 | + else { |
| 195 | + '' |
| 196 | + } |
| 197 | + |
| 198 | + # Invoke the ADO Rest API. |
| 199 | + Invoke-ADORestAPI @invokeParams -Uri $uri -PSTypeName @( |
| 200 | + # Because we want to format the output, decorate the object with the following typenames: |
| 201 | + "$Organization.$Project.Repository$subTypeName" # * $Organization.$Project.Repository$SubTypeName |
| 202 | + "$Organization.Repository$subTypeName" # * $Organization.Repository$SubTypeName |
| 203 | + "StartAutomating.PSDevOps.Repository$subTypeName" # * PSDevOps.Repository$SubTypeName |
| 204 | + ) |
| 205 | + } |
| 206 | +} |
0 commit comments