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
|
#
# Copyright (c) .NET Foundation and contributors. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#
# This script updates the dotnet/versions repository based on a set of packages. It directly
# commits the changes using GitHub APIs.
param(
[Parameter(Mandatory=$true)][string]$gitHubUser,
[Parameter(Mandatory=$true)][string]$gitHubEmail,
[Parameter(Mandatory=$true)][string]$gitHubAuthToken,
[Parameter(Mandatory=$true)][string]$versionsRepoOwner,
[Parameter(Mandatory=$true)][string]$versionsRepo,
[Parameter(Mandatory=$true)][string]$versionsRepoPath,
# A pattern matching all packages in the set that the versions repository should be set to.
[Parameter(Mandatory=$true)][string]$nupkgPath,
# Print out the new file contents, but don't change the versions repository.
[switch]$dryRun)
function ConvertPathTo-Package([string]$path)
{
# Find the package ID and version using a regex. This matches the semantic version
# and assumes everything to the left is the id or a path to the package directory.
$matched = $path -match '^(.*\\)?(.*?)\.(([0-9]+\.)?[0-9]+\.[0-9]+(-([A-z0-9-]+))?)\.(symbols\.)?nupkg$'
if ($matched)
{
$packageInfo = @{
Path = $path
Name = $matches[2]
Version = $matches[3]
Prerelease = $matches[6]
}
$packageInfo.NameVersion = "$($packageInfo.Name) $($packageInfo.Version)"
return $packageInfo
}
else
{
throw "Couldn't find name and version from path $path."
}
}
# Updates a GitHub file with the specified file contents
function Update-GitHub-File(
[string]$user = $gitHubUser,
[string]$email = $gitHubEmail,
[string]$authToken = $gitHubAuthToken,
[string]$owner = $versionsRepoOwner,
[string]$repo = $versionsRepo,
[string]$path,
[string]$newFileContent,
[string]$commitMessage)
{
function message([string]$message)
{
Write-Host -ForegroundColor Green "*** $message ***"
}
$headers = @{
'Accept' = 'application/vnd.github.v3+json'
'Authorization' = "token $authToken"
}
$fileUrl = "https://api.github.com/repos/$owner/$repo/contents/$path"
message "Getting the `"sha`" of the current contents of file '$owner/$repo/$path'"
$currentFile = Invoke-WebRequest $fileUrl -UseBasicParsing -Headers $headers
$currentSha = (ConvertFrom-Json $currentFile.Content).sha
message "Got `"sha`" value of '$currentSha'"
message "Request to update file '$owner/$repo/$path' contents to:"
Write-Host $newFileContent
if ($dryRun)
{
message 'Not sending request: dry run.'
return
}
# Base64 encode the string
function ToBase64([string]$value)
{
return [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($value))
}
$updateFileBody =
@"
{
"message": "$commitMessage",
"committer": {
"name": "$user",
"email": "$email"
},
"content": "$(ToBase64 $newFileContent)",
"sha": "$currentSha"
}
"@
message 'Sending request...'
$putResponse = Invoke-WebRequest $fileUrl -UseBasicParsing -Method PUT -Body $updateFileBody -Headers $headers
if ($putResponse.StatusCode -ge 200 -and $putResponse.StatusCode -lt 300)
{
message 'Successfully updated the file'
}
}
# Store result of Get-ChildItem before piping to ConvertPathTo-Package. When directly piping, exceptions are ignored.
$packagePaths = Get-ChildItem $nupkgPath
$packages = $packagePaths | %{ ConvertPathTo-Package $_ }
$prereleaseVersion = ''
foreach ($package in $packages)
{
if ($package.Prerelease)
{
$prereleaseVersion = $package.Prerelease
break
}
}
if (!$prereleaseVersion)
{
throw "Could not find a Prerelease version in '$newPackages'"
}
$versionFilePath = "$versionsRepoPath/Latest.txt"
$versionFileContent = "$prereleaseVersion`n"
Update-GitHub-File `
-path $versionFilePath `
-newFileContent $versionFileContent `
-commitMessage "Update '$versionFilePath' with $prereleaseVersion"
$packageInfoFilePath = "$versionsRepoPath/Latest_Packages.txt"
$packageInfoFileContent = ($packages | %{ $_.NameVersion } | Sort-Object) -join "`r`n"
Update-GitHub-File `
-path $packageInfoFilePath `
-newFileContent $packageInfoFileContent `
-commitMessage "Adding package info to '$packageInfoFilePath' for $prereleaseVersion"
|