forked from nanoframework/nf-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-units-net.yml
100 lines (76 loc) · 3.37 KB
/
update-units-net.yml
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
# Copyright (c) .NET Foundation and Contributors
# See LICENSE file in the project root for full license information.
steps:
- task: PowerShell@2
displayName: Update UnitsNet
condition: >-
and(
succeeded(),
ne(variables['StartReleaseCandidate'], 'true'),
or(
eq(variables['UPDATE_DEPENDENTS'], 'true'),
startsWith(variables['Build.SourceBranch'], 'refs/tags/v')
)
)
env:
GITHUB_TOKEN: $(GitHubToken)
inputs:
targetType: 'inline'
script: |
"" | Write-Host
"**************************" | Write-Host
"Firing update for UnitsNet" | Write-Host
"" | Write-Host
# working directory is agent temp directory
Write-Debug "cd to $env:Agent_TempDirectory"
cd "$env:Agent_TempDirectory" > $null
git clone https://github.com/angularsen/UnitsNet UnitsNet
cd UnitsNet > $null
git remote add myorigin https://github.com/nfbot/UnitsNet
git config --global gc.auto 0
git config --global user.email "nfbot"
git config --global user.name "[email protected]"
# compute authorization header in format "AUTHORIZATION: Bearer <YOUR-TOKEN>"
$auth = "Bearer $(GitHubToken)"
# new branch name
$newBranch = "nfbot/nano-update/"+[guid]::NewGuid().ToString()
# create branch to perform updates
git checkout master
git checkout -b $newBranch master
nuget restore UnitsNet.NanoFramework/GeneratedCode/UnitsNet.NanoFramework.sln
cd CodeGen > $null
dotnet run --update-nano-framework-dependencies --verbose
# check if anything was changed
$repoStatus = "$(git status --short --porcelain)"
if ($repoStatus -ne "")
{
"Commit changes..." | Write-Host -ForegroundColor White
# commit changed files
git add -A 2>&1
git commit -m "Update .NET nanoFramework NuGets" -m "Automated update" > $null
"Pushing changes..." | Write-Host -ForegroundColor White
# push to nfbot repo
git -c http.extraheader="AUTHORIZATION: $auth" push --porcelain --set-upstream myorigin "$newBranch"
# submit PR
$prRequestBody = @{title="Update .NET nanoFramework NuGets";body="Automated update from @nfbot.";head="nfbot:$newBranch";base="master"} | ConvertTo-Json
# start PR
"Creating PR..." | Write-Host -ForegroundColor White
$githubApiEndpoint = "https://api.github.com/repos/angularsen/UnitsNet/pulls"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
try
{
$result = Invoke-RestMethod -Method Post -UserAgent [Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer -Uri $githubApiEndpoint -Header @{"Authorization"="$auth"} -ContentType "application/json" -Body $prRequestBody
'Submitting PR with updates...' | Write-Host -ForegroundColor White -NoNewline
'OK' | Write-Host -ForegroundColor Green
}
catch
{
"Failed" | Write-Host -ForegroundColor Red
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd()
"Error submitting PR: $responseBody" | Write-Host -ForegroundColor Red
}
}