Merge pull request #5807 from Maoni0/obj_addr
[platform/upstream/coreclr.git] / UpdatePublishedVersions.ps1
1 #
2 # Copyright (c) .NET Foundation and contributors. All rights reserved.
3 # Licensed under the MIT license. See LICENSE file in the project root for full license information.
4 #
5
6 # This script updates the dotnet/versions repository based on a set of packages. It directly
7 # commits the changes using GitHub APIs.
8
9 param(
10     [Parameter(Mandatory=$true)][string]$gitHubUser,
11     [Parameter(Mandatory=$true)][string]$gitHubEmail,
12     [Parameter(Mandatory=$true)][string]$gitHubAuthToken,
13     [Parameter(Mandatory=$true)][string]$versionsRepoOwner,
14     [Parameter(Mandatory=$true)][string]$versionsRepo,
15     [Parameter(Mandatory=$true)][string]$versionsRepoPath,
16     # A pattern matching all packages in the set that the versions repository should be set to.
17     [Parameter(Mandatory=$true)][string]$nupkgPath,
18     # Print out the new file contents, but don't change the versions repository.
19     [switch]$dryRun)
20
21 function ConvertPathTo-Package([string]$path)
22 {
23     # Find the package ID and version using a regex. This matches the semantic version
24     # and assumes everything to the left is the id or a path to the package directory.
25     $matched = $path -match '^(.*\\)?(.*?)\.(([0-9]+\.)?[0-9]+\.[0-9]+(-([A-z0-9-]+))?)\.(symbols\.)?nupkg$'
26     if ($matched)
27     {
28         $packageInfo = @{
29             Path = $path
30             Name = $matches[2]
31             Version = $matches[3]
32             Prerelease = $matches[6]
33         }
34         $packageInfo.NameVersion = "$($packageInfo.Name) $($packageInfo.Version)"
35         return $packageInfo
36     }
37     else
38     {
39         throw "Couldn't find name and version from path $path."
40     }
41 }
42
43 # Updates a GitHub file with the specified file contents
44 function Update-GitHub-File(
45     [string]$user = $gitHubUser,
46     [string]$email = $gitHubEmail,
47     [string]$authToken = $gitHubAuthToken,
48     [string]$owner = $versionsRepoOwner,
49     [string]$repo = $versionsRepo,
50     [string]$path,
51     [string]$newFileContent,
52     [string]$commitMessage)
53 {
54     function message([string]$message)
55     {
56         Write-Host -ForegroundColor Green "*** $message ***"
57     }
58
59     $headers = @{
60         'Accept' = 'application/vnd.github.v3+json'
61         'Authorization' = "token $authToken"
62     }
63
64     $fileUrl = "https://api.github.com/repos/$owner/$repo/contents/$path"
65
66     message "Getting the `"sha`" of the current contents of file '$owner/$repo/$path'"
67
68     $currentFile = Invoke-WebRequest $fileUrl -UseBasicParsing -Headers $headers
69     $currentSha = (ConvertFrom-Json $currentFile.Content).sha
70
71     message "Got `"sha`" value of '$currentSha'"
72
73     message "Request to update file '$owner/$repo/$path' contents to:"
74     Write-Host $newFileContent
75
76     if ($dryRun)
77     {
78         message 'Not sending request: dry run.'
79         return
80     }
81
82     # Base64 encode the string
83     function ToBase64([string]$value)
84     {
85        return [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($value))
86     }
87
88     $updateFileBody = 
89 @"
90 {
91   "message": "$commitMessage",
92   "committer": {
93     "name": "$user",
94     "email": "$email"
95   },
96   "content": "$(ToBase64 $newFileContent)",
97   "sha": "$currentSha"
98 }
99 "@
100
101     message 'Sending request...'
102     $putResponse = Invoke-WebRequest $fileUrl -UseBasicParsing -Method PUT -Body $updateFileBody -Headers $headers
103
104     if ($putResponse.StatusCode -ge 200 -and $putResponse.StatusCode -lt 300)
105     {
106         message 'Successfully updated the file'
107     }
108 }
109
110 # Store result of Get-ChildItem before piping to ConvertPathTo-Package. When directly piping, exceptions are ignored.
111 $packagePaths = Get-ChildItem $nupkgPath
112 $packages = $packagePaths | %{ ConvertPathTo-Package $_ }
113
114 $prereleaseVersion = ''
115 foreach ($package in $packages)
116 {
117     if ($package.Prerelease)
118     {
119         $prereleaseVersion = $package.Prerelease
120         break
121     }
122 }
123
124 if (!$prereleaseVersion)
125 {
126     throw "Could not find a Prerelease version in '$newPackages'"
127 }
128
129 $versionFilePath = "$versionsRepoPath/Latest.txt"
130 $versionFileContent = "$prereleaseVersion`n"
131
132 Update-GitHub-File `
133     -path $versionFilePath `
134     -newFileContent $versionFileContent `
135     -commitMessage "Update '$versionFilePath' with $prereleaseVersion" 
136
137 $packageInfoFilePath = "$versionsRepoPath/Latest_Packages.txt"
138 $packageInfoFileContent = ($packages | %{ $_.NameVersion } | Sort-Object) -join "`r`n"
139
140 Update-GitHub-File `
141     -path $packageInfoFilePath `
142     -newFileContent $packageInfoFileContent `
143     -commitMessage "Adding package info to '$packageInfoFilePath' for $prereleaseVersion"
144