Skip to content

Commit 6f53ec8

Browse files
authored
fix: refine hash fixing workflow (#75)
Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
1 parent 162fa9f commit 6f53ec8

1 file changed

Lines changed: 61 additions & 26 deletions

File tree

src/Action/Issue.psm1

Lines changed: 61 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
Join-Path $PSScriptRoot '..\Helpers.psm1' | Import-Module
22
Join-Path $PSScriptRoot 'Issue' | Get-ChildItem -Filter '*.psm1' | Select-Object -ExpandProperty Fullname | Import-Module
33

4+
function Invoke-Git {
5+
param(
6+
[Parameter(Mandatory = $true)]
7+
[string[]] $GitArgs
8+
)
9+
10+
& git @GitArgs
11+
if ($LASTEXITCODE -ne 0) {
12+
throw "git $($GitArgs -join ' ') failed with exit code $LASTEXITCODE"
13+
}
14+
}
15+
416
function Test-Hash {
517
param (
618
[Parameter(Mandatory = $true)]
@@ -88,38 +100,61 @@ function Test-Hash {
88100
Invoke-GithubRequest "repos/$REPOSITORY/pulls/$prID" -Method Patch -Body @{ 'body' = (@("- Closes #$IssueID", $pr.body) -join "`r`n") }
89101
Add-Label -ID $IssueID -Label 'duplicate'
90102
} else {
91-
# Check if default branch is protected
92-
if (((Invoke-GithubRequest "repos/$REPOSITORY/branches/$masterBranch").Content | ConvertFrom-Json).protected) {
93-
Write-Log 'PR - Create new branch and post PR'
94-
95-
$branch = "$manifestNameAsInBucket-hash-fix-$(Get-Random -Maximum 258258258)"
96-
97-
Write-Log 'Branch' $branch
103+
Write-Log 'Git Status:'
104+
Invoke-Git -GitArgs @('status', '--porcelain')
98105

99-
git checkout -B $branch
100-
# TODO: There is some problem
106+
Invoke-Git -GitArgs @('add', $gci.FullName)
107+
Invoke-Git -GitArgs @('commit', '-m', "$titleToBePosted (Closes #$IssueID)")
101108

102-
Write-Log 'Git Status' @(git status --porcelain)
109+
# Try direct push
110+
try {
111+
Write-Log 'Commiting fix directly'
112+
Invoke-Git -GitArgs @('push')
113+
} catch {
114+
Write-Log 'Direct push failed. Probably protected branch. Will try to create PR instead.'
103115

104-
git add $gci.FullName
105-
git commit -m $titleToBePosted
106-
git push origin $branch
116+
$branch = "$manifestNameAsInBucket-hash-fix-$(Get-Random -Maximum 258258258)"
117+
Write-Log 'Branch' $branch
107118

108-
# Create new PR
109-
Invoke-GithubRequest -Query "repos/$REPOSITORY/pulls" -Method Post -Body @{
110-
'title' = $titleToBePosted
111-
'base' = $masterBranch
112-
'head' = $branch
113-
'body' = "- Closes #$IssueID"
119+
Invoke-Git -GitArgs @('checkout', '-B', $branch)
120+
# Amend commit with new message
121+
Invoke-Git -GitArgs @('commit', '--amend', '-m', "$titleToBePosted")
122+
123+
# Try create branch and PR
124+
try {
125+
Write-Log 'Creating branch'
126+
Invoke-Git -GitArgs @('push', 'origin', $branch)
127+
} catch {
128+
Write-Log 'Create branch failed. Please check workflow permissions.'
129+
Add-Comment -ID $IssueID -AppendLogLink -Message @(
130+
'Hash mismatch confirmed, but the bot could not publish the fix currently.'
131+
)
132+
return
114133
}
115-
} else {
116-
Write-Log 'Push - Fix hash and push the commit'
117-
118-
Write-Log 'Git Status' @(git status --porcelain)
119134

120-
git add $gci.FullName
121-
git commit -m "$titleToBePosted (Closes #$IssueID)"
122-
git push
135+
try {
136+
Write-Log 'Creating PR'
137+
138+
# Create new PR
139+
Invoke-GithubRequest -Query "repos/$REPOSITORY/pulls" -Method Post -Body @{
140+
'title' = $titleToBePosted
141+
'base' = $masterBranch
142+
'head' = $branch
143+
'body' = "- Closes #$IssueID"
144+
}
145+
} catch {
146+
Write-Log 'Create PR failed. Please check workflow permissions.'
147+
# Try to delete branch if PR creation failed
148+
try {
149+
Invoke-Git -GitArgs @('push', 'origin', '--delete', $branch)
150+
} catch {
151+
Write-Log 'Failed to delete branch. Please check workflow permissions.'
152+
}
153+
Add-Comment -ID $IssueID -AppendLogLink -Message @(
154+
'Hash mismatch confirmed, but the bot could not publish the fix currently.'
155+
)
156+
return
157+
}
123158
}
124159
}
125160
Add-Comment -ID $IssueID -Message $message -AppendLogLink

0 commit comments

Comments
 (0)