Skip to content

Commit

Permalink
fix(auto-pr): Use standalone git for non-pullrequests operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash258 committed Feb 3, 2021
1 parent ac012d0 commit 9638964
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions bin/auto-pr.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ param(
[Switch] $SkipCheckver
)

'Helpers', 'manifest', 'json' | ForEach-Object {
'Helpers', 'manifest', 'Git', 'json' | ForEach-Object {
. (Join-Path $PSScriptRoot "..\lib\$_.ps1")
}

Expand All @@ -65,6 +65,7 @@ if (($RepositoryRoot.BaseName -eq 'bucket') -and (!(Join-Path $RepositoryRoot '.
} else {
$RepositoryRoot = $RepositoryRoot.FullName
}
$RepositoryRoot.TrimEnd('/').TrimEnd('\')
$repoContext = "-C ""$RepositoryRoot"""

if ($Help -or (!$Push -and !$Request)) {
Expand All @@ -83,17 +84,25 @@ Optional options:
exit 3
}

if (!(Get-Command -Name 'hub' -CommandType 'Application' -ErrorAction 'SilentlyContinue')) {
if ($Request -and !(Get-Command -Name 'hub' -CommandType 'Application' -ErrorAction 'SilentlyContinue')) {
Stop-ScoopExecution -Message 'hub is required! Please refer to ''https://hub.github.com/'' to find out how to get hub for your platform.'
}

function execute($cmd) {
Write-Host $cmd -ForegroundColor Green
$output = Invoke-Expression $cmd
function _gitWrapper {
param([String] $Command, [String[]] $Argument, [String] $Repository, [Switch] $GH, [Switch] $Proxy)

if ($LASTEXITCODE -gt 0) { Stop-ScoopExecution -Message "^^^ Error! See above ^^^ (last command: $cmd)" }
process {
$utility = if ($GH) { 'gh' } else { 'git' }
$mes = "$utility -C ""$Repository"" $Command $($Argument -join ' ')"
Write-UserMessage -Message $mes -Color 'Green'

return $output
$output = Invoke-GitCmd -Repository $Repository -Command $Command -Argument $Argument -Proxy:$Proxy
if ($LASTEXITCODE -gt 0) {
Stop-ScoopExecution -Message "^^^ See above ^^^ (last command: $mes)"
}

return $output
}
}

# json object, application name, upstream repository, relative path to manifest file
Expand Down Expand Up @@ -148,13 +157,15 @@ a new version of [$app]($homepage) is available.
}
}

$splat = @{ 'Repository' = $RepositoryRoot }

Write-UserMessage 'Updating ...' -ForegroundColor 'DarkCyan'
if ($Push) {
execute "hub $repoContext pull origin master"
execute "hub $repoContext checkout master"
_gitWrapper @splat -Command 'pull' -Argument 'origin', 'master' -Proxy
_gitWrapper @splat -Command 'checkout' -Argument 'master'
} else {
execute "hub $repoContext pull upstream master"
execute "hub $repoContext push origin master"
_gitWrapper @splat -Command 'pull' -Argument 'upstream', 'master' -Proxy
_gitWrapper @splat -Command 'push' -Argument 'origin', 'master' -Proxy
}

if (!$SkipCheckver) {
Expand All @@ -167,7 +178,8 @@ if (!$SkipCheckver) {
}
}

foreach ($changedFile in hub -C "$RepositoryRoot" diff --name-only | Where-Object { $_ -like 'bucket/*' }) {
foreach ($changedFile in _gitWrapper @splat -Command 'diff' -Argument '--name-only' | Where-Object { $_ -like 'bucket/*' }) {
# foreach ($changedFile in Invoke-GitCmd @splat -Command 'diff' -Argument '--name-only' | Where-Object { $_ -like 'bucket/*' }) {
$gci = Get-Item "$RepositoryRoot\$changedFile"
$applicationName = $gci.BaseName
if ($gci.Extension -notmatch "\.($ALLOWED_MANIFEST_EXTENSION_REGEX)") {
Expand All @@ -192,6 +204,7 @@ foreach ($changedFile in hub -C "$RepositoryRoot" diff --name-only | Where-Objec

if ($Push) {
Write-UserMessage "Creating update $applicationName ($version) ..." -ForegroundColor 'DarkCyan'
continue

execute "hub $repoContext add $changedFile"
# Detect if file was staged, because it's not when only LF or CRLF have changed
Expand All @@ -208,6 +221,8 @@ foreach ($changedFile in hub -C "$RepositoryRoot" diff --name-only | Where-Objec
}
}

exit 258

if ($Push) {
Write-UserMessage 'Pushing updates ...' -ForegroundColor 'DarkCyan'
execute "hub $repoContext push origin master"
Expand All @@ -216,7 +231,7 @@ if ($Push) {
execute "hub $repoContext checkout --force master"
}

execute "hub $repoContext reset --hard"
_gitWrapper @splat -Command 'reset' -Argument '--hard'

if ($problems -gt 0) { $exitCode = 10 + $problems }
exit $exitCode

0 comments on commit 9638964

Please sign in to comment.