summaryrefslogtreecommitdiff
path: root/UpdateDependencies.ps1
blob: 51f24c5c144b4535557eab03b5281878efd06fc5 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
#
# 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 dir.props with the current version of CoreCLR
# dependencies, and then creates a Pull Request for the change.

param(
    [Parameter(Mandatory=$true)][string]$GitHubUser,
    [Parameter(Mandatory=$true)][string]$GitHubEmail,
    [Parameter(Mandatory=$true)][string]$GitHubPassword,
    [Parameter(Mandatory=$true)][string]$VersionFileUrl,
    [string[]]$DirPropsVersionElements = 'CoreClrExpectedPrerelease',
    [string]$GitHubUpstreamOwner='dotnet', 
    [string]$GitHubOriginOwner=$GitHubUser,
    [string]$GitHubProject='coreclr',
    [string]$GitHubUpstreamBranch='master',
    # a semi-colon delimited list of GitHub users to notify on the PR
    [string]$GitHubPullRequestNotifications='')

$LatestVersion = Invoke-WebRequest $VersionFileUrl -UseBasicParsing
$LatestVersion = $LatestVersion.ToString().Trim()

if ($DirPropsVersionElements -contains 'CoreClrExpectedPrerelease')
{
    # Also get list of all package versions, relative to the given prerelease version url.
    $LatestPackagesListUrl = $VersionFileUrl -Replace 'Latest.txt', 'Latest_Packages.txt'
    $LatestPackagesList = Invoke-WebRequest $LatestPackagesListUrl -UseBasicParsing
    $LatestCoreCLRPackage = $LatestPackagesList -split "`n" | ?{ $_.StartsWith('Microsoft.NETCore.Runtime.CoreCLR') }
    $LatestCoreCLRVersion = ($LatestCoreCLRPackage -split ' ')[1].Trim()
}


# Make a nicely formatted string of the dir props version elements. Short names, joined by commas.
$DirPropsVersionNames = ($DirPropsVersionElements | %{ $_ -replace 'ExpectedPrerelease', '' }) -join ', '

# Updates the dir.props file with the latest build number
function UpdateValidDependencyVersionsFile
{
    if (!$LatestVersion)
    {
        Write-Error "Unable to find latest dependency version at $VersionFileUrl ($DirPropsVersionNames)"
        return $false
    }

    $DirPropsPaths = @("$PSScriptRoot\dir.props", "$PSScriptRoot\tests\dir.props")

    $DirPropsPaths | %{
       $DirPropsContent = Get-Content $_ | %{
            $line = $_

            $DirPropsVersionElements | %{
                $line = $line -replace `
                    "<$_>.*</$_>", `
                    "<$_>$LatestVersion</$_>"
            }

            if ($LatestCoreCLRVersion)
            {
                $line = $line -replace `
                    "<CoreClrPackageVersion>.*<", `
                    "<CoreClrPackageVersion>$LatestCoreCLRVersion<"
            }

            $line
       }
       Set-Content $_ $DirPropsContent
    }

    return $true
}

# Updates all the project.json files with out of date version numbers
function RunUpdatePackageDependencyVersions
{
    cmd /c $PSScriptRoot\build-test.cmd updateinvalidpackages | Out-Host

    return $LASTEXITCODE -eq 0
}

# Creates a Pull Request for the updated version numbers
function CreatePullRequest
{
    $GitStatus = git status --porcelain
    if ([string]::IsNullOrWhiteSpace($GitStatus))
    {
        Write-Warning "Dependencies are currently up to date"
        return $true
    }
    
    $CommitMessage = "Updating $DirPropsVersionNames dependencies to $LatestVersion"

    $env:GIT_COMMITTER_NAME = $GitHubUser
    $env:GIT_COMMITTER_EMAIL = $GitHubEmail
    git commit -a -m "$CommitMessage" --author "$GitHubUser <$GitHubEmail>" | Out-Host

    $RemoteUrl = "github.com/$GitHubOriginOwner/$GitHubProject.git"
    $RemoteBranchName = "UpdateDependencies$([DateTime]::UtcNow.ToString('yyyyMMddhhmmss'))"
    $RefSpec = "HEAD:refs/heads/$RemoteBranchName"

    Write-Host "git push https://$RemoteUrl $RefSpec"
    # pipe this to null so the password secret isn't in the logs
    git push "https://$($GitHubUser):$GitHubPassword@$RemoteUrl" $RefSpec 2>&1 | Out-Null

    if ($GitHubPullRequestNotifications)
    {
        $PRNotifications = $GitHubPullRequestNotifications.Split(';', [StringSplitOptions]::RemoveEmptyEntries) -join ' @'
        $PRBody = "/cc @$PRNotifications"
    }
    else
    {
        $PRBody = ''
    }

    $CreatePRBody = @"
    {
        "title": "$CommitMessage",
        "body": "$PRBody",
        "head": "$($GitHubOriginOwner):$RemoteBranchName",
        "base": "$GitHubUpstreamBranch"
    }
"@

    $CreatePRHeaders = @{'Accept'='application/vnd.github.v3+json'; 'Authorization'="token $GitHubPassword"}

    try
    {
        Invoke-WebRequest https://api.github.com/repos/$GitHubUpstreamOwner/$GitHubProject/pulls -UseBasicParsing -Method Post -Body $CreatePRBody -Headers $CreatePRHeaders
    }
    catch
    {
        Write-Error $_.ToString()
        return $false
    }

    return $true
}

if (!(UpdateValidDependencyVersionsFile))
{
    Exit -1
}

if (!(RunUpdatePackageDependencyVersions))
{
    Exit -1
}

if (!(CreatePullRequest))
{
    Exit -1
}

Write-Host -ForegroundColor Green "Successfully updated dependencies from the latest build numbers"

exit $LastExitCode