forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-pr.ps1
172 lines (151 loc) · 5.61 KB
/
auto-pr.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
# Usage: .\bin\auto-pr.ps1 [options]
# Summary: Updates manifests and pushes them or creates pull-requests
# Help: Updates manifests and pushes them to directly the master branch or creates pull-requests for upstream
#
# Options:
# -p, --push push updates directly to 'origin master'
# -r, --request create pull-requests on 'upstream master' for each update
# -u, --upstream <upstream> upstream repository with target branch
# only used if -r is set (default: lukesampson/scoop:master)
param(
[String]$upstream = "lukesampson/scoop:master",
[String]$dir,
[Switch]$push = $false,
[Switch]$request = $false,
[Switch]$help = $false,
[string[]]$specialSnowflakes
)
if(!$dir) { $dir = "$psscriptroot\..\bucket" }
$dir = resolve-path $dir
. "$psscriptroot\..\lib\manifest.ps1"
. "$psscriptroot\..\lib\json.ps1"
. "$psscriptroot\..\lib\unix.ps1"
if(is_unix) {
if (!(which hub)) {
Write-Host -f yellow "Please install hub ('brew install hub' or visit: https://hub.github.com/)"
exit 1
}
} else {
if (!(scoop which hub)) {
Write-Host -f yellow "Please install hub 'scoop install hub'"
exit 1
}
}
if ((!$push -and !$request) -or $help) {
Write-Host ""
Write-Host "Usage: auto-pr.ps1 [OPTION]"
Write-Host ""
Write-Host "Mandatory options:"
Write-Host " -p, -push push updates directly to 'origin master'"
Write-Host " -r, -request create pull-requests on 'upstream master' for each update"
Write-Host ""
Write-Host "Optional options:"
Write-Host " -u, -upstream upstream repository with target branch"
Write-Host " only used if -r is set (default: lukesampson/scoop:master)"
Write-Host " -h, -help"
Write-Host ""
exit 0
}
if(!($upstream -match "^(.*)\/(.*):(.*)$")) {
Write-Host -f DarkRed "Upstream must have this format: <user>/<repo>:<branch>"
exit 1
}
function execute($cmd) {
Write-Host -f Green $cmd
$output = iex $cmd
if($LASTEXITCODE -gt 0) {
Write-Host -f Red "^^^ Error! See above ^^^ (last command: $cmd)"
exit 1
}
return $output
}
function pull_requests($json, [String]$app, [String]$upstream, [String]$manifest)
{
$version = $json.version
$homepage = $json.homepage
$branch = "manifest/$app-$version"
execute "hub checkout master"
Write-Host -f Green "hub rev-parse --verify $branch"
hub rev-parse --verify $branch
if($LASTEXITCODE -eq 0) {
Write-Host -f Yellow "Skipping update $app ($version) ..."
return
}
Write-Host -f DarkCyan "Creating update $app ($version) ..."
execute "hub checkout -b $branch"
execute "hub add $manifest"
execute "hub commit -m 'Update $app to version $version'"
Write-Host -f DarkCyan "Pushing update $app ($version) ..."
execute "hub push origin $branch"
if($LASTEXITCODE -gt 0) {
Write-Host -f DarkRed "Push failed! (hub push origin $branch)"
execute "hub reset"
return
}
Start-Sleep 1
Write-Host -f DarkCyan "Pull-Request update $app ($version) ..."
Write-Host -f green "hub pull-request -m '<msg>' -b '$upstream' -h '$branch'"
$msg = "Update $app to version $version`n`n"
$msg += "Hello lovely humans,`n"
$msg += "a new version of [$app]($homepage) is available.`n"
$msg += "<table>"
$msg += "<tr><th align=left>State</th><td>Update :rocket:</td></tr>"
$msg += "<tr><th align=left>New version</td><td>$version</td></tr>"
$msg += "</table>"
hub pull-request -m "$msg" -b '$upstream' -h '$branch'
if($LASTEXITCODE -gt 0) {
Write-Host -f DarkRed "Pull Request failed! (hub pull-request -m 'Update $app to version $version' -b '$upstream' -h '$branch')"
execute "hub reset"
exit 1
}
}
Write-Host -f DarkCyan "Updating ..."
if($push -eq $true) {
execute("hub pull origin master")
execute "hub checkout master"
} else {
execute("hub pull upstream master")
execute("hub push origin master")
}
. "$psscriptroot\checkver.ps1" * -update -dir $dir
if($specialSnowflakes) {
write-host -f DarkCyan "Forcing update on our special snowflakes: $($specialSnowflakes -join ',')"
$specialSnowflakes | % {
. "$psscriptroot\checkver.ps1" $_ -update -forceUpdate -dir $dir
}
}
hub diff --name-only | % {
$manifest = $_
if(!$manifest.EndsWith(".json")) {
return
}
$app = ([System.IO.Path]::GetFileNameWithoutExtension($manifest))
$json = parse_json $manifest
if(!$json.version) {
Write-Host -f Red "Invalid manifest: $manifest ..."
return
}
$version = $json.version
if($push -eq $true) {
Write-Host -f DarkCyan "Creating update $app ($version) ..."
execute "hub add $manifest"
# detect if file was staged, because it's not when only LF or CRLF have changed
$status = iex "hub status --porcelain -uno"
$status = $status | select-object -first 1
if($status -and $status.StartsWith('M ') -and $status.EndsWith("$app.json")) {
execute "hub commit -m 'Update $app to version $version'"
} else {
Write-Host -f Yellow "Skipping $app because only LF/CRLF changes were detected ..."
}
} else {
pull_requests $json $app $upstream $manifest
}
}
if($push -eq $true) {
Write-Host -f DarkCyan "Pushing updates ..."
execute "hub push origin master"
} else {
Write-Host -f DarkCyan "Returning to master branch and removing unstaged files ..."
execute "hub checkout -f master"
}
execute "hub reset"