forked from H-uru/korman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease.ps1
188 lines (167 loc) · 6.07 KB
/
release.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# This file is part of Korman.
#
# Korman is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Korman is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Korman. If not, see <http://www.gnu.org/licenses/>.
param(
[Parameter()]
[string]$GitHub = "https://api.github.com",
[Parameter(Mandatory=$true)]
[string]$Repository,
[Parameter(Mandatory=$true)]
[string]$Token,
[Parameter()]
[string]$UploadDir = "$PSScriptRoot/build/package",
[Parameter()]
[hashtable]$SubDirs = @{}
)
$ErrorActionPreference = "STOP"
$RequestHeaders = @{
"Accept" = "application/vnd.github.v3+json"
"Authorization" = "token $Token"
}
function Invoke-GitHubRequest($Method, $Uri, $Body, $ValidResults) {
$Result = Invoke-WebRequest `
-Uri "$Uri" `
-Method "$Method" `
-Headers $RequestHeaders `
-ContentType "application/json" `
-Body @(ConvertTo-JSON $Body) `
-SkipHttpErrorCheck
Test-GitHubResult `
-Result $Result `
-FailureMessage "GitHub API call failed" `
-ValidResults $ValidResults
try {
$Content = ConvertFrom-JSON $Result.Content
} catch {
$Content = ""
}
return @{
"StatusCode" = $Result.StatusCode
"Content" = $Content
}
}
$ContentTypeLUT = @{
".exe" = "application/x-msdownload"
".sha256" = "text/plain"
".zip" = "application/zip"
}
function Invoke-GitHubUpload($Method, $Uri, $InFile, $ValidResults) {
Write-Host "Uploading $InFile -> $Uri"
# Can't use -InFile per my testing. Gah.
$Body = [System.IO.File]::ReadAllBytes($InFile)
if ($ContentTypeLUT.Contains($(Split-Path -Extension $InFile))) {
$ContentType = $ContentTypeLUT[$(Split-Path -Extension $InFile)]
} else {
$ContentType = "application/octet-stream"
}
try {
$Result = Invoke-WebRequest `
-Uri "$Uri" `
-Method "$Method" `
-Headers $RequestHeaders `
-ContentType $ContentType `
-Body $Body `
-SkipHttpErrorCheck
} catch {
throw "Uploading to GitHub failed. Check for (and delete any) junked files on the release!"
}
Test-GitHubResult `
-Result $Result `
-FailureMessage "GitHub API call failed" `
-ValidResults $ValidResults
}
function Test-GitHubResult($Result, $FailureMessage, $ValidResults) {
if (!($ValidResults.Contains($Result.StatusCode))) {
Write-Host "bad result $Result"
throw "$($FailureMessage): $($Result.StatusCode) $($Result.Content)"
}
}
function Get-KormanRelease() {
Write-Host -ForegroundColor Cyan "Finding a GitHub release for $Tag ($CommitSHA)..."
$ExistingRelease = Invoke-GitHubRequest `
-Uri "$GitHub/repos/$Repository/releases/tags/$Tag" `
-Method GET `
-ValidResults @(200, 404)
if ($ExistingRelease.StatusCode -Eq 404) {
# No matching release, so make a new one.
Write-Host -ForegroundColor Cyan "Creating a new (pre-)release..."
$NewRelease = Invoke-GitHubRequest `
-Uri "$GitHub/repos/$Repository/releases" `
-Method POST `
-Body @{
"name" = "Korman $Tag"
"tag_name" = $Tag
"target_commitish" = $CommitSHA
"prerelease" = $true
} `
-ValidResults @(201)
return $NewRelease.Content
} elseif ($ExistingRelease.StatusCode -Eq 200) {
Write-Host -ForegroundColor Yellow "Existing release found ($($ExistingRelease.Content.name)) - it will be forced to pre-release status and cleared!"
if ($ExistingRelease.Content.prerelease -Ne $false) {
Invoke-GitHubRequest `
-Uri "$GitHub/repos/$Repository/releases/$($ExistingRelease.Content.id)" `
-Method PATCH `
-Body @{ "prerelease" = $true } `
-ValidResults @(200)
}
foreach ($Asset in $ExistingRelease.Content.assets) {
Write-Host -ForegroundColor Red "Deleting release asset $($Asset.name))"
Invoke-GitHubRequest `
-Uri $Asset.url `
-Method DELETE `
-ValidResults @(204)
}
return $ExistingRelease.Content
}
throw "???"
}
function Publish-KormanReleaseAssets($Release) {
Write-Host -ForegroundColor Cyan "Finding assets to upload"
$Assets = @{}
foreach ($Iter in $SubDirs.GetEnumerator()) {
foreach ($Item in (Get-ChildItem -File "$UploadDir/$($Iter.Key)")) {
$Assets.Add("$($Iter.Value)_$($Item.Name)", $Item.FullName);
}
}
foreach ($Item in (Get-ChildItem -File $UploadDir)) {
$Assets.Add($Item.Name, $Item.FullName);
}
# Sigh, stupid GitHub...
Write-Host -ForegroundColor Cyan "Uploading $($Assets.Count) asset(s) to GitHub release $($Release.id)"
$ss = Select-String -InputObject $Release.upload_url "^(https:\/\/.+){"
$UploadURL = $ss.Matches[0].Groups[1]
Write-Host "URL: $UploadURL"
foreach ($Asset in $Assets.GetEnumerator()) {
Invoke-GitHubUpload `
-Uri "$($UploadURL)?name=$($Asset.Key)" `
-Method POST `
-InFile $Asset.Value `
-ValidResults @(201)
}
}
# Try to determine what the current tag is.
Push-Location $PSScriptRoot
try {
$Tag = git describe --tags --exact
if ($LASTEXITCODE -Ne 0) {
Write-Host -ForegroundColor Yellow "This will only work for tagged releases, my friend." | Out-Null
Exit 0
}
$CommitSHA = git rev-parse HEAD
} finally {
Pop-Location
}
$Release = Get-KormanRelease
Publish-KormanReleaseAssets $Release