Merge pull request #6297 from CarolEidt/MorphGenTreeRefactors
[platform/upstream/coreclr.git] / UpdateDependencies.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 dir.props with the current version of CoreCLR
7 # dependencies, and then creates a Pull Request for the change.
8
9 param(
10     [Parameter(Mandatory=$true)][string]$GitHubUser,
11     [Parameter(Mandatory=$true)][string]$GitHubEmail,
12     [Parameter(Mandatory=$true)][string]$GitHubPassword,
13     [Parameter(Mandatory=$true)][string]$VersionFileUrl,
14     [string[]]$DirPropsVersionElements = 'CoreClrExpectedPrerelease',
15     [string]$GitHubUpstreamOwner='dotnet', 
16     [string]$GitHubOriginOwner=$GitHubUser,
17     [string]$GitHubProject='coreclr',
18     [string]$GitHubUpstreamBranch='master',
19     # a semi-colon delimited list of GitHub users to notify on the PR
20     [string]$GitHubPullRequestNotifications='')
21
22 $LatestVersion = Invoke-WebRequest $VersionFileUrl -UseBasicParsing
23 $LatestVersion = $LatestVersion.ToString().Trim()
24
25 if ($DirPropsVersionElements -contains 'CoreClrExpectedPrerelease')
26 {
27     # Also get list of all package versions, relative to the given prerelease version url.
28     $LatestPackagesListUrl = $VersionFileUrl -Replace 'Latest.txt', 'Latest_Packages.txt'
29     $LatestPackagesList = Invoke-WebRequest $LatestPackagesListUrl -UseBasicParsing
30     $LatestCoreCLRPackage = $LatestPackagesList -split "`n" | ?{ $_.StartsWith('Microsoft.NETCore.Runtime.CoreCLR') }
31     $LatestCoreCLRVersion = ($LatestCoreCLRPackage -split ' ')[1].Trim()
32 }
33
34
35 # Make a nicely formatted string of the dir props version elements. Short names, joined by commas.
36 $DirPropsVersionNames = ($DirPropsVersionElements | %{ $_ -replace 'ExpectedPrerelease', '' }) -join ', '
37
38 # Updates the dir.props file with the latest build number
39 function UpdateValidDependencyVersionsFile
40 {
41     if (!$LatestVersion)
42     {
43         Write-Error "Unable to find latest dependency version at $VersionFileUrl ($DirPropsVersionNames)"
44         return $false
45     }
46
47     $DirPropsPaths = @("$PSScriptRoot\dir.props", "$PSScriptRoot\tests\dir.props")
48
49     $DirPropsPaths | %{
50        $DirPropsContent = Get-Content $_ | %{
51             $line = $_
52
53             $DirPropsVersionElements | %{
54                 $line = $line -replace `
55                     "<$_>.*</$_>", `
56                     "<$_>$LatestVersion</$_>"
57             }
58
59             if ($LatestCoreCLRVersion)
60             {
61                 $line = $line -replace `
62                     "<CoreClrPackageVersion>.*<", `
63                     "<CoreClrPackageVersion>$LatestCoreCLRVersion<"
64             }
65
66             $line
67        }
68        Set-Content $_ $DirPropsContent
69     }
70
71     return $true
72 }
73
74 # Updates all the project.json files with out of date version numbers
75 function RunUpdatePackageDependencyVersions
76 {
77     cmd /c $PSScriptRoot\tests\buildtest.cmd updateinvalidpackages | Out-Host
78
79     return $LASTEXITCODE -eq 0
80 }
81
82 # Creates a Pull Request for the updated version numbers
83 function CreatePullRequest
84 {
85     $GitStatus = git status --porcelain
86     if ([string]::IsNullOrWhiteSpace($GitStatus))
87     {
88         Write-Warning "Dependencies are currently up to date"
89         return $true
90     }
91     
92     $CommitMessage = "Updating $DirPropsVersionNames dependencies to $LatestVersion"
93
94     $env:GIT_COMMITTER_NAME = $GitHubUser
95     $env:GIT_COMMITTER_EMAIL = $GitHubEmail
96     git commit -a -m "$CommitMessage" --author "$GitHubUser <$GitHubEmail>" | Out-Host
97
98     $RemoteUrl = "github.com/$GitHubOriginOwner/$GitHubProject.git"
99     $RemoteBranchName = "UpdateDependencies$([DateTime]::UtcNow.ToString('yyyyMMddhhmmss'))"
100     $RefSpec = "HEAD:refs/heads/$RemoteBranchName"
101
102     Write-Host "git push https://$RemoteUrl $RefSpec"
103     # pipe this to null so the password secret isn't in the logs
104     git push "https://$($GitHubUser):$GitHubPassword@$RemoteUrl" $RefSpec 2>&1 | Out-Null
105
106     if ($GitHubPullRequestNotifications)
107     {
108         $PRNotifications = $GitHubPullRequestNotifications.Split(';', [StringSplitOptions]::RemoveEmptyEntries) -join ' @'
109         $PRBody = "/cc @$PRNotifications"
110     }
111     else
112     {
113         $PRBody = ''
114     }
115
116     $CreatePRBody = @"
117     {
118         "title": "$CommitMessage",
119         "body": "$PRBody",
120         "head": "$($GitHubOriginOwner):$RemoteBranchName",
121         "base": "$GitHubUpstreamBranch"
122     }
123 "@
124
125     $CreatePRHeaders = @{'Accept'='application/vnd.github.v3+json'; 'Authorization'="token $GitHubPassword"}
126
127     try
128     {
129         Invoke-WebRequest https://api.github.com/repos/$GitHubUpstreamOwner/$GitHubProject/pulls -UseBasicParsing -Method Post -Body $CreatePRBody -Headers $CreatePRHeaders
130     }
131     catch
132     {
133         Write-Error $_.ToString()
134         return $false
135     }
136
137     return $true
138 }
139
140 if (!(UpdateValidDependencyVersionsFile))
141 {
142     Exit -1
143 }
144
145 if (!(RunUpdatePackageDependencyVersions))
146 {
147     Exit -1
148 }
149
150 if (!(CreatePullRequest))
151 {
152     Exit -1
153 }
154
155 Write-Host -ForegroundColor Green "Successfully updated dependencies from the latest build numbers"
156
157 exit $LastExitCode