Add to model.xml several members we wish to expose in the FX (#6310)
[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 # Make a nicely formatted string of the dir props version elements. Short names, joined by commas.
26 $DirPropsVersionNames = ($DirPropsVersionElements | %{ $_ -replace 'ExpectedPrerelease', '' }) -join ', '
27
28 # Updates the dir.props file with the latest build number
29 function UpdateValidDependencyVersionsFile
30 {
31     if (!$LatestVersion)
32     {
33         Write-Error "Unable to find latest dependency version at $VersionFileUrl ($DirPropsVersionNames)"
34         return $false
35     }
36
37     $DirPropsPath = "$PSScriptRoot\dir.props"
38     
39     $DirPropsContent = Get-Content $DirPropsPath | % {
40         $line = $_
41         $DirPropsVersionElements | % {
42             $line = $line -replace `
43                 "<$_>.*</$_>", `
44                 "<$_>$LatestVersion</$_>"
45         }
46         $line
47     }
48     Set-Content $DirPropsPath $DirPropsContent
49
50     return $true
51 }
52
53 # Creates a Pull Request for the updated version numbers
54 function CreatePullRequest
55 {
56     $GitStatus = git status --porcelain
57     if ([string]::IsNullOrWhiteSpace($GitStatus))
58     {
59         Write-Warning "Dependencies are currently up to date"
60         return $true
61     }
62     
63     $CommitMessage = "Updating $DirPropsVersionNames dependencies to $LatestVersion"
64
65     $env:GIT_COMMITTER_NAME = $GitHubUser
66     $env:GIT_COMMITTER_EMAIL = $GitHubEmail
67     git commit -a -m "$CommitMessage" --author "$GitHubUser <$GitHubEmail>" | Out-Host
68
69     $RemoteUrl = "github.com/$GitHubOriginOwner/$GitHubProject.git"
70     $RemoteBranchName = "UpdateDependencies$([DateTime]::UtcNow.ToString('yyyyMMddhhmmss'))"
71     $RefSpec = "HEAD:refs/heads/$RemoteBranchName"
72
73     Write-Host "git push https://$RemoteUrl $RefSpec"
74     # pipe this to null so the password secret isn't in the logs
75     git push "https://$($GitHubUser):$GitHubPassword@$RemoteUrl" $RefSpec 2>&1 | Out-Null
76
77     if ($GitHubPullRequestNotifications)
78     {
79         $PRNotifications = $GitHubPullRequestNotifications.Split(';', [StringSplitOptions]::RemoveEmptyEntries) -join ' @'
80         $PRBody = "/cc @$PRNotifications"
81     }
82     else
83     {
84         $PRBody = ''
85     }
86
87     $CreatePRBody = @"
88     {
89         "title": "$CommitMessage",
90         "body": "$PRBody",
91         "head": "$($GitHubOriginOwner):$RemoteBranchName",
92         "base": "$GitHubUpstreamBranch"
93     }
94 "@
95
96     $CreatePRHeaders = @{'Accept'='application/vnd.github.v3+json'; 'Authorization'="token $GitHubPassword"}
97
98     try
99     {
100         Invoke-WebRequest https://api.github.com/repos/$GitHubUpstreamOwner/$GitHubProject/pulls -UseBasicParsing -Method Post -Body $CreatePRBody -Headers $CreatePRHeaders
101     }
102     catch
103     {
104         Write-Error $_.ToString()
105         return $false
106     }
107
108     return $true
109 }
110
111 if (!(UpdateValidDependencyVersionsFile))
112 {
113     Exit -1
114 }
115
116 if (!(CreatePullRequest))
117 {
118     Exit -1
119 }
120
121 Write-Host -ForegroundColor Green "Successfully updated dependencies from the latest build numbers"
122
123 exit $LastExitCode