From c3195915abc4ae23e72b08ba58fed2d2b079d6dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20=C4=8C=C3=A1bera?= Date: Mon, 14 Jan 2019 16:06:26 +0100 Subject: [PATCH] auto-pr.ps1: Fix table syntax, allow help without hub, format (#2933) * Use Markdown format in commit message * Table produced was wrong formatted due to syntax mismatch * ![wrong syntax](https://i.imgur.com/CgoXlvo.png) * Allow -Help to be called without hub installed * Add Synopsis * Respect PascalCase in Parameters * Add default values for parameters and ValidateScript --- bin/auto-pr.ps1 | 213 +++++++++++++++++++++++++++--------------------- 1 file changed, 120 insertions(+), 93 deletions(-) diff --git a/bin/auto-pr.ps1 b/bin/auto-pr.ps1 index a91d28c16..d20507317 100644 --- a/bin/auto-pr.ps1 +++ b/bin/auto-pr.ps1 @@ -1,169 +1,196 @@ -# 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 repository with target branch -# only used if -r is set (default: lukesampson/scoop:master) - +<# +.SYNOPSIS + Updates manifests and pushes them or creates pull-requests. +.DESCRIPTION + Updates manifests and pushes them directly to the master branch or creates pull-requests for upstream. +.PARAMETER Upstream + Upstream repository with the target branch. + Must be in format '/:' +.PARAMETER Dir + The directory where to search for manifests. +.PARAMETER Push + Push updates directly to 'origin master'. +.PARAMETER Request + Create pull-requests on 'upstream master' for each update. +.PARAMETER Help + Print help to console. +.PARAMETER SpecialSnowflakes + An array of manifests, which should be updated all the time. (-ForceUpdate parameter to checkver) +.EXAMPLE + PS REPODIR > .\bin\auto-pr.ps1 'someUsername/repository:branch' -Request +.EXAMPLE + PS REPODIR > .\bin\auto-pr.ps1 -Push + Update all manifests inside 'bucket/' directory. +#> param( - [String]$upstream = "lukesampson/scoop:master", - [String]$dir, - [Switch]$push = $false, - [Switch]$request = $false, - [Switch]$help = $false, - [string[]]$specialSnowflakes + [ValidateScript( { + if (!($_ -match '^(.*)\/(.*):(.*)$')) { + throw 'Upstream must be in this format: /:' + } + $true + })] + [String] $Upstream = 'lukesampson/scoop:master', + [ValidateScript( { + if (!(Test-Path $_ -Type Container)) { + throw "$_ is not a directory!" + } + $true + })] + [String] $Dir = "$PSScriptRoot\..\bucket", + [Switch] $Push, + [Switch] $Request, + [Switch] $Help, + [string[]] $SpecialSnowflakes ) -if(!$dir) { $dir = "$psscriptroot\..\bucket" } -$dir = resolve-path $dir +. "$PSScriptRoot\..\lib\manifest.ps1" +. "$PSScriptRoot\..\lib\json.ps1" +. "$PSScriptRoot\..\lib\unix.ps1" -. "$psscriptroot\..\lib\manifest.ps1" -. "$psscriptroot\..\lib\json.ps1" -. "$psscriptroot\..\lib\unix.ps1" +$Dir = Resolve-Path $Dir -if(is_unix) { +if ((!$Push -and !$Request) -or $Help) { + Write-Host @' +Usage: auto-pr.ps1 [OPTION] + +Mandatory options: + -p, -push push updates directly to 'origin master' + -r, -request create pull-requests on 'upstream master' for each update + +Optional options: + -u, -upstream upstream repository with target branch + only used if -r is set (default: lukesampson/scoop:master) + -h, -help +'@ + exit 0 +} + +if (is_unix) { if (!(which hub)) { - Write-Host -f yellow "Please install hub ('brew install hub' or visit: https://hub.github.com/)" + Write-Host "Please install hub ('brew install hub' or visit: https://hub.github.com/)" -ForegroundColor Yellow exit 1 } } else { if (!(scoop which hub)) { - Write-Host -f yellow "Please install hub 'scoop install hub'" + Write-Host "Please install hub 'scoop install hub'" -ForegroundColor Yellow 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 "^(.*)\/(.*):(.*)$")) { - abort "Upstream must have this format: /:" -} - function execute($cmd) { - Write-Host -f Green $cmd + Write-Host $cmd -ForegroundColor Green $output = Invoke-Expression $cmd - if($LASTEXITCODE -gt 0) { + if ($LASTEXITCODE -gt 0) { abort "^^^ Error! See above ^^^ (last command: $cmd)" } + return $output } -function pull_requests($json, [String]$app, [String]$upstream, [String]$manifest) -{ +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" + execute 'hub checkout master' + Write-Host "hub rev-parse --verify $branch" -ForegroundColor Green hub rev-parse --verify $branch - if($LASTEXITCODE -eq 0) { - Write-Host -f Yellow "Skipping update $app ($version) ..." + if ($LASTEXITCODE -eq 0) { + Write-Host "Skipping update $app ($version) ..." -ForegroundColor Yellow return } - Write-Host -f DarkCyan "Creating update $app ($version) ..." + Write-Host "Creating update $app ($version) ..." -ForegroundColor DarkCyan execute "hub checkout -b $branch" execute "hub add $manifest" execute "hub commit -m '${app}: Update to version $version'" - Write-Host -f DarkCyan "Pushing update $app ($version) ..." + Write-Host "Pushing update $app ($version) ..." -ForegroundColor DarkCyan execute "hub push origin $branch" - if($LASTEXITCODE -gt 0) { + if ($LASTEXITCODE -gt 0) { error "Push failed! (hub push origin $branch)" - execute "hub reset" + execute 'hub reset' return } + Start-Sleep 1 - Write-Host -f DarkCyan "Pull-Request update $app ($version) ..." - Write-Host -f green "hub pull-request -m '' -b '$upstream' -h '$branch'" - $msg = "${app}: Update to version $version`n`n" - $msg += "Hello lovely humans,`n" - $msg += "a new version of [$app]($homepage) is available.`n" - $msg += "" - $msg += "" - $msg += "" - $msg += "
StateUpdate :rocket:
New version$version
" + Write-Host "Pull-Request update $app ($version) ..." -ForegroundColor DarkCyan + Write-Host "hub pull-request -m '' -b '$upstream' -h '$branch'" -ForegroundColor Green + + $msg = @" +$app`: Update to version $version + +Hello lovely humans, +a new version of [$app]($homepage) is available. + +| State | Update :rocket: | +| :---------- | :-------------- | +| New version | $version | +"@ + hub pull-request -m "$msg" -b '$upstream' -h '$branch' - if($LASTEXITCODE -gt 0) { - execute "hub reset" + if ($LASTEXITCODE -gt 0) { + execute 'hub reset' abort "Pull Request failed! (hub pull-request -m '${app}: Update to version $version' -b '$upstream' -h '$branch')" } } -Write-Host -f DarkCyan "Updating ..." -if($push -eq $true) { - execute("hub pull origin master") - execute "hub checkout master" +Write-Host 'Updating ...' -ForegroundColor DarkCyan +if ($Push) { + execute 'hub pull origin master' + execute 'hub checkout master' } else { - execute("hub pull upstream master") - execute("hub push origin master") + 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 -split ',' | ForEach-Object { - . "$psscriptroot\checkver.ps1" $_ -update -forceUpdate -dir $dir +. "$PSScriptRoot\checkver.ps1" -Dir $Dir -Update +if ($SpecialSnowflakes) { + Write-Host "Forcing update on our special snowflakes: $($SpecialSnowflakes -join ',')" -ForegroundColor DarkCyan + $SpecialSnowflakes -split ',' | ForEach-Object { + . "$PSScriptRoot\checkver.ps1" $_ -Dir $Dir -ForceUpdate } } hub diff --name-only | ForEach-Object { $manifest = $_ - if(!$manifest.EndsWith(".json")) { + if (!$manifest.EndsWith('.json')) { return } $app = ([System.IO.Path]::GetFileNameWithoutExtension($manifest)) $json = parse_json $manifest - if(!$json.version) { + if (!$json.version) { error "Invalid manifest: $manifest ..." return } $version = $json.version - if($push -eq $true) { - Write-Host -f DarkCyan "Creating update $app ($version) ..." + if ($Push) { + Write-Host "Creating update $app ($version) ..." -ForegroundColor DarkCyan execute "hub add $manifest" # detect if file was staged, because it's not when only LF or CRLF have changed - $status = Invoke-Expression "hub status --porcelain -uno" - $status = $status | select-object -first 1 - if($status -and $status.StartsWith('M ') -and $status.EndsWith("$app.json")) { + $status = Invoke-Expression '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 '${app}: Update to version $version'" } else { - Write-Host -f Yellow "Skipping $app because only LF/CRLF changes were detected ..." + Write-Host "Skipping $app because only LF/CRLF changes were detected ..." -ForegroundColor Yellow } } else { - pull_requests $json $app $upstream $manifest + pull_requests $json $app $Upstream $manifest } } -if($push -eq $true) { - Write-Host -f DarkCyan "Pushing updates ..." - execute "hub push origin master" +if ($Push) { + Write-Host 'Pushing updates ...' -ForegroundColor DarkCyan + execute 'hub push origin master' } else { - Write-Host -f DarkCyan "Returning to master branch and removing unstaged files ..." - execute "hub checkout -f master" + Write-Host 'Returning to master branch and removing unstaged files ...' -ForegroundColor DarkCyan + execute 'hub checkout -f master' } -execute "hub reset" +execute 'hub reset'