-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-DevOpsPrivateRepoFile.ps1
More file actions
237 lines (182 loc) · 6.86 KB
/
Get-DevOpsPrivateRepoFile.ps1
File metadata and controls
237 lines (182 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#Requires -Version 7.0
<#
.SYNOPSIS
Retrieves the content of a single file from a private Azure DevOps Git repository.
.DESCRIPTION
Calls the Azure DevOps Git Items REST API to download the text content of one
file from a private repository, authenticating with a Personal Access Token (PAT)
over HTTP Basic auth. The file content is written to the output stream so it can
be captured, piped, or redirected to disk.
Prerequisites:
- An Azure DevOps organisation reachable at https://<account>.visualstudio.com.
- A PAT with at least Code (Read) scope on the target project/repository.
.PARAMETER DevOpsAccountName
The Azure DevOps organisation (account) name. Used to build the base URL
https://<DevOpsAccountName>.visualstudio.com.
.PARAMETER DevOpsTeamProjectName
The team project that contains the target repository.
.PARAMETER FileRepo
The name (or id) of the Git repository to read the file from.
.PARAMETER DevOpsPAT
The Azure DevOps Personal Access Token used to authenticate, supplied as a
SecureString. Requires Code (Read) scope.
.PARAMETER User
The user name portion of the Basic auth pair. Azure DevOps PAT auth ignores the
user name, so this is normally left empty.
.PARAMETER FileRepoBranch
The branch to read the file from. Defaults to 'master'.
.PARAMETER FilePath
The repository-relative path of the file to retrieve.
.PARAMETER ApiVersion
The Azure DevOps REST API version to target. Defaults to '4.1'.
.INPUTS
None. This script does not accept pipeline input.
.OUTPUTS
System.String. The text content of the requested file is written to the output
stream.
.EXAMPLE
$pat = Read-Host -AsSecureString
./Get-DevOpsPrivateRepoFile.ps1 -DevOpsAccountName 'contoso' -DevOpsTeamProjectName 'Platform' -FileRepo 'Platform' -DevOpsPAT $pat -FilePath 'Scripts/PowerShell/Deploy.ps1'
Downloads Deploy.ps1 from the master branch of the Platform repository and writes
its content to the output stream.
.EXAMPLE
./Get-DevOpsPrivateRepoFile.ps1 -DevOpsAccountName 'contoso' -DevOpsTeamProjectName 'Platform' -FileRepo 'Platform' -DevOpsPAT $pat -FilePath 'README.md' -FileRepoBranch 'main' > README.md
Downloads README.md from the main branch and saves it to a local file.
.NOTES
Author: Sebastian Gräf
Repo: https://github.com/segraef/Scripts
#>
#region Parameters
[CmdletBinding()]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DevOpsAccountName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DevOpsTeamProjectName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$FileRepo,
[Parameter(Mandatory)]
[ValidateNotNull()]
[securestring]$DevOpsPAT,
[Parameter()]
[string]$User = '',
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$FileRepoBranch = 'master',
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$FilePath,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ApiVersion = '4.1'
)
#endregion
#region Initialisation
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
Import-Module "$PSScriptRoot/Write-Log.psm1" -Force
#endregion
#region Functions
function Get-DevOpsRepoFileContent {
<#
.SYNOPSIS
Downloads the content of one file from a private Azure DevOps Git repository.
.DESCRIPTION
Builds the Azure DevOps Git Items REST API URI for the requested file and
retrieves its content using PAT-based Basic authentication.
.PARAMETER DevOpsAccountName
The Azure DevOps organisation (account) name.
.PARAMETER DevOpsTeamProjectName
The team project that contains the target repository.
.PARAMETER FileRepo
The Git repository to read the file from.
.PARAMETER DevOpsPAT
The Azure DevOps Personal Access Token, supplied as a SecureString.
.PARAMETER User
The user name portion of the Basic auth pair (normally empty for PAT auth).
.PARAMETER FileRepoBranch
The branch to read the file from.
.PARAMETER FilePath
The repository-relative path of the file to retrieve.
.PARAMETER ApiVersion
The Azure DevOps REST API version to target.
.OUTPUTS
System.String. The text content of the requested file.
.EXAMPLE
Get-DevOpsRepoFileContent -DevOpsAccountName 'contoso' -DevOpsTeamProjectName 'Platform' -FileRepo 'Platform' -DevOpsPAT $pat -FilePath 'README.md'
Returns the content of README.md from the master branch.
#>
[CmdletBinding()]
[OutputType([string])]
param
(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DevOpsAccountName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$DevOpsTeamProjectName,
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$FileRepo,
[Parameter(Mandatory)]
[ValidateNotNull()]
[securestring]$DevOpsPAT,
[Parameter()]
[string]$User = '',
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$FileRepoBranch = 'master',
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[string]$FilePath,
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$ApiVersion = '4.1'
)
$devOpsBaseUrl = "https://$DevOpsAccountName.visualstudio.com"
$plainPat = [System.Net.NetworkCredential]::new('', $DevOpsPAT).Password
try {
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(('{0}:{1}' -f $User, $plainPat)))
$devOpsAuthHeader = @{ Authorization = ('Basic {0}' -f $base64AuthInfo) }
$itemsPath = "/$DevOpsTeamProjectName/_apis/git/repositories/$FileRepo/items"
$queryParts = @(
"path=$FilePath"
'$format=json'
'includeContent=true'
"versionDescriptor.version=$FileRepoBranch"
'versionDescriptor.versionType=branch'
"api-version=$ApiVersion"
)
$uri = $devOpsBaseUrl + $itemsPath + '?' + ($queryParts -join '&')
Write-Log "Requesting '$FilePath' from repository '$FileRepo' (branch '$FileRepoBranch')."
$file = Invoke-RestMethod -Method Get -ContentType 'application/json' -Uri $uri -Headers $devOpsAuthHeader
return $file.content
}
catch {
Write-Log -Message "Failed to retrieve '$FilePath' from repository '$FileRepo'." -ErrorRecord $_
throw
}
finally {
$plainPat = $null
}
}
#endregion
#region Execution
Write-Log "Executing $($MyInvocation.MyCommand.Name)."
$content = Get-DevOpsRepoFileContent `
-DevOpsAccountName $DevOpsAccountName `
-DevOpsTeamProjectName $DevOpsTeamProjectName `
-FileRepo $FileRepo `
-DevOpsPAT $DevOpsPAT `
-User $User `
-FileRepoBranch $FileRepoBranch `
-FilePath $FilePath `
-ApiVersion $ApiVersion
Write-Output $content
Write-Log "Finished $($MyInvocation.MyCommand.Name)."
#endregion