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.
6 # This script updates the dotnet/versions repository based on a set of packages. It directly
7 # commits the changes using GitHub APIs.
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.
21 function ConvertPathTo-Package([string]$path)
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$'
32 Prerelease = $matches[6]
34 $packageInfo.NameVersion = "$($packageInfo.Name) $($packageInfo.Version)"
39 throw "Couldn't find name and version from path $path."
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,
51 [string]$newFileContent,
52 [string]$commitMessage)
54 function message([string]$message)
56 Write-Host -ForegroundColor Green "*** $message ***"
60 'Accept' = 'application/vnd.github.v3+json'
61 'Authorization' = "token $authToken"
64 $fileUrl = "https://api.github.com/repos/$owner/$repo/contents/$path"
66 message "Getting the `"sha`" of the current contents of file '$owner/$repo/$path'"
68 $currentFile = Invoke-WebRequest $fileUrl -UseBasicParsing -Headers $headers
69 $currentSha = (ConvertFrom-Json $currentFile.Content).sha
71 message "Got `"sha`" value of '$currentSha'"
73 message "Request to update file '$owner/$repo/$path' contents to:"
74 Write-Host $newFileContent
78 message 'Not sending request: dry run.'
82 # Base64 encode the string
83 function ToBase64([string]$value)
85 return [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($value))
91 "message": "$commitMessage",
96 "content": "$(ToBase64 $newFileContent)",
101 message 'Sending request...'
102 $putResponse = Invoke-WebRequest $fileUrl -UseBasicParsing -Method PUT -Body $updateFileBody -Headers $headers
104 if ($putResponse.StatusCode -ge 200 -and $putResponse.StatusCode -lt 300)
106 message 'Successfully updated the file'
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 $_ }
114 $prereleaseVersion = ''
115 foreach ($package in $packages)
117 if ($package.Prerelease)
119 $prereleaseVersion = $package.Prerelease
124 if (!$prereleaseVersion)
126 throw "Could not find a Prerelease version in '$newPackages'"
129 $versionFilePath = "$versionsRepoPath/Latest.txt"
130 $versionFileContent = "$prereleaseVersion`n"
133 -path $versionFilePath `
134 -newFileContent $versionFileContent `
135 -commitMessage "Update '$versionFilePath' with $prereleaseVersion"
137 $packageInfoFilePath = "$versionsRepoPath/Latest_Packages.txt"
138 $packageInfoFileContent = ($packages | %{ $_.NameVersion } | Sort-Object) -join "`r`n"
141 -path $packageInfoFilePath `
142 -newFileContent $packageInfoFileContent `
143 -commitMessage "Adding package info to '$packageInfoFilePath' for $prereleaseVersion"