-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
update-dependencies.ps1
157 lines (125 loc) · 4.03 KB
/
update-dependencies.ps1
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
# Update one project packages
function UpdatePackages {
param (
$project
)
$currentDirectoy = Get-Location
$return = $false
$dir = Split-Path $project
Write-Host 'Set-Location' $dir
Set-Location $dir
# Get outdated packages
$packageLineList = dotnet list package --outdated
foreach ($line in $packageLineList) {
Write-Host $line
$match = $line -match '>\s(\S*)\s*\S*\s*\S*\s*(\S*)'
if (!$match) {
# the line doesn't contain a package information, continue
continue
}
Write-Host $match
$packageName = $Matches.1
$version = $Matches.2
if ($version -eq "Not") {
# latest version not found
$preReleasePackageLineList = dotnet list package --outdated --include-prerelease
foreach ($preReleaseLine in $preReleasePackageLineList) {
Write-Host $preReleaseLine
$preReleaseMatch = $preReleaseLine -match '>\s(\S*)\s*\S*\s*\S*\s*(\S*)'
if (!$preReleaseMatch) {
# the line doesn't contain a package information, continue
continue
}
if ($packageName -ne $Matches.1) {
continue
}
$latestVersion = $Matches.2
if ($latestVersion -match '-rc') {
$version = $latestVersion
Write-Host "Release candidate version found for $packageName : $version"
}
break
}
}
if ($version -eq "Not") {
continue
}
# update an outdated package
$added = dotnet add package $packageName --version $version
if ($LASTEXITCODE -ne 0) {
# error while updating the package
Write-Error "dotnet add package $packageName --version $version exit with code $LASTEXITCODE"
Write-Host $added
break
}
Write-Host 'package' $Matches.1 'version' $Matches.2 'updated'
$return = $true
}
Set-Location $currentDirectoy
return $return
}
# get branches names
$dest = "master"
if (Test-Path env:DEST_BRANCH) {
$dest = $env:DEST_BRANCH
}
$src = "fix/dependencies"
if (Test-Path env:SRC_BRANCH) {
$src = $env:SRC_BRANCH
}
Write-Host "src:$src dest: $dest"
# Restore dependencies
dotnet restore
# Get all project list in the solution
$projectList = dotnet sln list
$updated = $false
$exit = 0;
foreach ($path in $projectList) {
if ($path -eq "Project(s)" -or $path -eq "----------") {
# The line doesn't contain a path, continue
continue
}
# Update project dependencies
$projectUpdated = UpdatePackages -project $path
if ($LASTEXITCODE -ne 0) {
#The update fail, exit
$exit = $LASTEXITCODE
}
$updated = $updated -or $projectUpdated
}
if ($exit -ne 0) {
exit $exit
}
if (!$updated) {
# No packages to update found, exit
Write-Host "nothing to update"
exit 0
}
# Try build the solution with new packages
Write-Host "dotnet build -c Release"
dotnet build -c Release
# commit changes
Write-Host "git config user.name github-actions"
git config user.name github-actions
Write-Host "git config user.email [email protected]"
git config user.email [email protected]
Write-Host "git add ."
git add .
Write-Host "git commit -m ""fix: update packages"""
git commit -m "fix: update packages"
Write-Host "git push"
try {
git push
}
catch {
}
# Create a pull request
$authorization = "Bearer $env:GITHUB_TOKEN"
$createPrUrl = "https://api.github.com/repos/$env:GITHUB_REPOSITORY/pulls"
$headers = @{
Authorization = $authorization
Accept = "application/vnd.github.v3+json"
}
$payload = "{ ""title"": ""update packages"", ""head"": ""$src"", ""base"": ""$dest"" }"
Write-Host "Invoke-WebRequest -Uri $createPrUrl -Body $payload"
Invoke-WebRequest -Uri $createPrUrl -Headers $headers -Method "POST" -Body $payload