Created
June 25, 2024 12:53
-
-
Save mu88/bdbe207c1f5ca9b4d05ed8597eefcaa3 to your computer and use it in GitHub Desktop.
Bitbucket - Add tasks for fixup commits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Clear-Host | |
# Unlock PowerShell SecretStore with dedicated secret | |
$password = Import-CliXml -Path "C:\secretStore.xml" | |
Unlock-SecretStore -Password $password | |
$baseUrl = "https://bitbucket.company.com/rest/api/latest" | |
$username = Get-Secret -Name Bitbucket_FixupTasks_Username -AsPlainText | |
$password = Get-Secret -Name Bitbucket_FixupTasks_Token -AsPlainText | |
$commitPrefix = "fixup!" | |
$newTaskName = "squash/rebase" | |
# Create the basic authentication header | |
$base64Auth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$($username):$password")) | |
$headers = @{ | |
Authorization = "Basic $base64Auth" | |
Accept = "application/json" | |
"Content-Type" = "application/json" | |
} | |
# Get all open pull requests | |
$pullRequests = Invoke-RestMethod -Uri "$baseUrl/dashboard/pull-requests?role=AUTHOR&state=OPEN" -Headers $headers | |
if ($pullRequests.values.Count -eq 0) { | |
Write-Host "No open pull requests found" | |
return | |
} | |
foreach ($pr in $pullRequests.values) { | |
$prId = $pr.id | |
$prTitle = $pr.title | |
$prUrl = $pr.links.self[0].href | |
$commits = Invoke-RestMethod -Uri "$prUrl/commits" -Headers $headers | |
foreach ($commit in $commits.values) { | |
$commitMessage = $commit.message | |
# Check if the commit message starts with the specified prefix | |
if ($commitMessage -like "$commitPrefix*") { | |
# Check if the pull request already contains a dedicated task | |
$existingTasks = Invoke-RestMethod -Uri "$prUrl/blocker-comments" -Headers $headers | |
$fixupsTaskExists = $existingTasks.values | Where-Object { $_.text -eq $newTaskName } | |
if (-not $fixupsTaskExists) { | |
$newTask = @{ | |
text = $newTaskName | |
state = "OPEN" | |
} | |
$jsonBody = $newTask | ConvertTo-Json | |
Invoke-RestMethod -Uri "$prUrl/blocker-comments" -Method Post -Headers $headers -Body $jsonBody | |
Write-Host "Created task '$newTaskName' for PR $($prId): $prTitle" | |
} | |
else { | |
Write-Host "Task '$newTaskName' already exists for PR $($prId): $prTitle" | |
} | |
$foundFixup = $true | |
break | |
} | |
} | |
if (-not $foundFixup) { | |
Write-Host "No fixup commits found for PR $($prId): $prTitle" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment