-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAdd-FilesToRepo.ps1
More file actions
129 lines (115 loc) · 3.58 KB
/
Add-FilesToRepo.ps1
File metadata and controls
129 lines (115 loc) · 3.58 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
function Add-FilesToRepo {
<#
.SYNOPSIS
Upload path to a repo in Azure DevOps.
.DESCRIPTION
Upload path to a repo in Azure DevOps.
.EXAMPLE
$params = @{
CollectionUri = "https://dev.azure.com/contoso"
Name = "Repo 1"
ProjectName = "Project 1"
}
New-AzDoRepo @params
This example creates a new Azure DevOps repo with splatting parameters
.EXAMPLE
$env:SYSTEM_ACCESSTOKEN = '***'
'test', 'test2' | New-AzDoRepo -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1"
This example creates a new Azure DevOps repo for each in pipeline
.OUTPUTS
[PSCustomObject]@{
CollectionUri = $CollectionUri
ProjectName = $ProjectName
RepoName = $res.name
RepoId = $res.id
RepoURL = $res.url
WebUrl = $res.webUrl
HttpsUrl = $res.remoteUrl
SshUrl = $res.sshUrl
}
.NOTES
#>
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
param (
# Collection Uri of the organization
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$CollectionUri,
# Name of the repository to add files to
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$RepoName,
# Name of the project containing the repository where files need to be added
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[string]
$ProjectName,
# Name of the project where the new repository has to be created
[Parameter(Mandatory)]
[string]
$Path
)
begin {
Write-Verbose "Starting function: Add-FilesToRepo"
}
process {
$changes = @()
$files = Get-ChildItem -Path $Path -Recurse -File
foreach ($file in $files) {
if (($file.Extension -in '.png', '.svg, .jpg', '.jpeg')) {
$fileContent = Get-Content -Path $file.FullName -AsByteStream
$content = [convert]::ToBase64String($fileContent)
$contentType = "base64encoded"
} else {
$content = Get-Content -Path $file.FullName -Raw
$contentType = "rawtext"
}
if ($null -eq $content) {
Write-Warning "File $($file.FullName) is empty, skipping"
continue
}
$filePath = ($file.fullName).replace("$($path)", '')
$changes += [PSCustomObject]@{
changeType = "add"
item = @{
path = $filePath.Replace('\', '/')
}
newContent = @{
content = $content
contentType = $contentType
}
}
}
$Body = @{
refUpdates = @(
@{
name = "refs/heads/main"
oldObjectId = "0000000000000000000000000000000000000000"
}
)
commits = @(
@{
comment = "Initial commit"
changes = $changes
}
)
}
$params = @{
uri = "$CollectionUri/$ProjectName/_apis/git/repositories/$RepoName/pushes"
version = "7.1-preview.2"
Method = 'POST'
}
if ($PSCmdlet.ShouldProcess($RepoName, "Add path named: $($PSStyle.Bold)$($file.name)$($PSStyle.Reset)")) {
try {
$body | Invoke-AzDoRestMethod @params
} catch {
if ($_ -match 'TF401028') {
Write-Warning "Repo is already initialized, skip uploading files"
} else {
Write-AzDoError -message $_
}
}
} else {
Write-Verbose "Calling Invoke-AzDoRestMethod with $($params| ConvertTo-Json -Depth 10)"
}
}
}