Skip to content

Commit ff8766e

Browse files
authored
Add a new GitHub Action: ".NET version upgrader" (#245)
* Initial bits * Added a new GitHub Action, as a composite, that joins .NET Version Sweeper and the .NET Upgrade Assistant
1 parent eccdc82 commit ff8766e

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: '.NET version upgrader'
2+
description: 'A GitHub Action that relies on the .NET version sweeper to upgrade projects to the latest .NET version.'
3+
branding:
4+
icon: 'git-pull-request'
5+
color: 'purple'
6+
inputs:
7+
support:
8+
description: "The support level to target (STS, LTS, or Preview)."
9+
required: false
10+
default: "STS"
11+
runs:
12+
using: "composite"
13+
steps:
14+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
15+
- uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9
16+
17+
# Start the .NET version sweeper, scan projects/slns for non-LTS (or STS) versions
18+
- name: .NET version sweeper
19+
id: dotnet-version-sweeper
20+
uses: dotnet/versionsweeper@main
21+
env:
22+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23+
with:
24+
owner: ${{ github.repository_owner }}
25+
name: ${{ github.repository }}
26+
branch: ${{ github.ref }}
27+
28+
# Call the upgrade projects script, passing in the list of projects to upgrade
29+
- id: upgrade-projects
30+
if: steps.dotnet-version-sweeper.outputs.has-remaining-work == 'true'
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
33+
DOTNET_UPGRADEASSISTANT_TELEMETRY_OPTOUT: '1' # opt-out of upgrade-assistant telemetry
34+
shell: pwsh
35+
run: |
36+
$support = '${{ inputs.support }}'
37+
$projects = ${{ steps.dotnet-version-sweeper.outputs.upgrade-projects }}
38+
./upgrade-projects.ps1 $support $projects
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
$support = $args[0] # STS, LTS, or Preview
2+
$projectsJson = $args[1] # JSON string of projects to upgrade
3+
4+
Write-Host "Upgrading projects to $support versions"
5+
Write-Host ""
6+
7+
# Convert JSON string to array of strings
8+
$projects = ConvertFrom-Json $projectsJson
9+
10+
Write-Host "Parsed $($projects.Length) projects to upgrade"
11+
Write-Host ""
12+
13+
# Install .NET Upgrade Assistant global tool
14+
dotnet tool install --global upgrade-assistant
15+
16+
# Get the git repository directory
17+
$gitRepoDir = (git rev-parse --show-toplevel)
18+
19+
# Create an array of projects that to track failure attempts
20+
$failedProjects = @()
21+
22+
# Iterate all upgrade projects
23+
foreach ($projectDir in $projects) {
24+
Write-Host "Attempting to upgrade project: $projectDir"
25+
Write-Host ""
26+
27+
# Remove the git repository directory from the project directory
28+
$relativePath = $projectDir.Replace($gitRepoDir, "")
29+
30+
# Remove invalid characters from the branch name, then remove leading hyphen
31+
$branchName = $relativePath -replace '[^A-Za-z0-9._-]', '-'
32+
$branchName = $branchName -replace '^[-]+', ''
33+
34+
# Format the branch name
35+
$branch = "auto-upgrade/$branchName"
36+
37+
# Create a new branch
38+
git checkout -b $branch
39+
40+
# Normalize the project directory
41+
$projectDir = [IO.Path]::GetFullPath($projectDir)
42+
43+
# Capture all output from the upgrade-assistant command
44+
$output = upgrade-assistant upgrade "$projectDir" `
45+
--non-interactive `
46+
--operation Inplace `
47+
-t $support `
48+
2>&1
49+
50+
# Check if the exit code is 0 (success), or if there is output from a failure
51+
if ($LASTEXITCODE -eq 0 -and -not ($output -match "Exception")) {
52+
53+
Write-Host $output
54+
Write-Host ""
55+
56+
# Check for changes, report the exit code
57+
git diff --exit-code
58+
59+
# If there aren't any, delete the
60+
# branch and continue to the next project
61+
if ($LASTEXITCODE -ne 0) {
62+
$failedProjects += $projectDir
63+
64+
Write-Host "No changes detected for project: $projectDir"
65+
Write-Host ""
66+
67+
# Delete the branch if there aren't any changes
68+
git checkout main
69+
git branch -D $branch
70+
Write-Host ""
71+
72+
continue
73+
}
74+
75+
# Commit the changes
76+
git add .
77+
git commit -m ".NET Version Sweeper: Upgraded $projectDir"
78+
79+
# Push the branch to the repository
80+
git push origin $branch
81+
82+
# Format the pull request message
83+
$pullRequestMessage = "⚡ This is an automated pull request (powered by the
84+
[.NET Versioon Sweeper](https://github.com/dotnet/versionsweeper) and
85+
the [.NET Upgrade Assistant](https://github.com/dotnet/upgrade-assistant)])
86+
to _upgrade_ the **target framework moniker (TFM)** for the '$projectDir'
87+
project to the $support version. For more information, see
88+
[.NET Support Policy](https://dotnet.microsoft.com/platform/support/policy)."
89+
90+
# Create a pull request
91+
gh pr create `
92+
--base main `
93+
--head $branch `
94+
--title "[$support] Upgrade $projectDir" `
95+
--body $pullRequestMessage
96+
}
97+
else {
98+
$failedProjects += $projectDir
99+
100+
Write-Host "Unable to upgrade project: $projectDir"
101+
Write-Host ""
102+
103+
# Write the output as a warning
104+
Write-Warning -Message ([String]::Join("`n", $output))
105+
Write-Host ""
106+
107+
# Delete the branch if the upgrade fails
108+
git checkout main
109+
git branch -D $branch
110+
Write-Host ""
111+
}
112+
}
113+
114+
# If there are any failed projects, log them
115+
if ($failedProjects.Length -gt 0) {
116+
Write-Host "Failed to upgrade $($failedProjects.Length) projects:"
117+
Write-Host ""
118+
119+
foreach ($failedProject in $failedProjects) {
120+
Write-Host $failedProject
121+
}
122+
}

0 commit comments

Comments
 (0)