Skip to content

Instantly share code, notes, and snippets.

@jetersen
Last active July 29, 2022 09:02
Show Gist options
  • Save jetersen/b67a85f66bf8a34cca6b41d887dbd9e5 to your computer and use it in GitHub Desktop.
Save jetersen/b67a85f66bf8a34cca6b41d887dbd9e5 to your computer and use it in GitHub Desktop.
Bulk Pull Requests Update of GitHub Action workflows

Bulk Pull Requests Update of GitHub Action workflows

This is a bulk update script that attempts to upgrade files to use reusable workflow to simplify the work for the plugin maintainers. See this Pull Request search for a successful run: https://github.com/pulls?q=is%3Apr+is%3Aopen+author%3A%40me+archived%3Afalse+sort%3Aupdated-desc+chore%3A+use+jenkins+infra+maven+cd+reusable+workflow

This uses multi-gitter although currently it does not support signed commits and currently I have an open PR that fixes repository listing for repositories without push permissions: lindell/multi-gitter#262

Atm using my PR and multi-gitter print "pwsh $PWD/maven-cd-reusable-workflow.ps1" --config multi-gitter-config.yaml

$repository = "$ENV:REPOSITORY"
$ghUser = "jetersen"
$filePath = Get-ChildItem .github/workflows -Recurse -ErrorAction Ignore | Select-String "uses: jenkins-infra/jenkins-maven-cd-action@" | Select-Object -ExpandProperty Path -First 1
if ([string]::IsNullOrEmpty($filePath) -eq $true) {
$reusableWorkflowFound = Get-ChildItem .github/workflows -Recurse -ErrorAction Ignore | Select-String "uses: jenkins-infra/github-reusable-workflows/.github/workflows/maven-cd.yml@v1" | Select-Object -ExpandProperty Path -First 1
if ([string]::IsNullOrEmpty($reusableWorkflowFound) -eq $false) {
Write-Host "$repository already uses the reusable workflow"
} else {
Write-Host "No jenkins-infra/jenkins-maven-cd-action found, skipping $repository"
}
exit 0
}
& git config remote.origin.gh-resolved base
$checkPullRequest = & gh pr list --author $ghUser --limit 1 --search "use jenkins infra maven cd reusable workflow" --state all
if ($checkPullRequest | Select-String $prTitle) {
Write-Host "Pull request already exists for $repository"
exit 0
}
$noteString = "# Note: additional setup is required, see https://www.jenkins.io/redirect/continuous-delivery-of-plugins"
$prTitle = "chore: use jenkins infra maven cd reusable workflow"
$prBody = @"
🤖 Beep boop!
This is an automatic pull request that updates your GitHub action used for continous delivery workflow for this repository.
Switching to using a reusable workflow created to simplify maintance of this file in your repository.
In case of questions, please ping `@jetersen`.
Additional informations:
[You can find the workflow here](https://github.com/jenkins-infra/github-reusable-workflows/blob/main/.github/workflows/maven-cd.yml)
[Full explaination](https://github.com/jenkinsci/configurationslicing-plugin/pull/117#issuecomment-1156234429)
"@
$desiredFileContent = @(
'# Note: additional setup is required, see https://www.jenkins.io/redirect/continuous-delivery-of-plugins'
''
'name: cd'
'on:'
' workflow_dispatch:'
' check_run:'
' types:'
' - completed'
''
'jobs:'
' maven-cd:'
' uses: jenkins-infra/github-reusable-workflows/.github/workflows/maven-cd.yml@v1'
' secrets:'
' MAVEN_USERNAME: ${{ secrets.MAVEN_USERNAME }}'
' MAVEN_TOKEN: ${{ secrets.MAVEN_TOKEN }}'
''
)
[string] $desiredFileContentString = $desiredFileContent | Out-String
$desiredFileLength = $desiredFileContent.Length - 1
[int] $desiredJobsIndex = $desiredFileContent | Select-String "jobs:" | Select-Object -First 1 -ExpandProperty LineNumber
$desiredJobs = $desiredFileContent[$desiredJobsIndex..$desiredFileLength]
$content = Get-Content "$filePath"
[string] $contentString = $content | Out-String
if ($contentString -eq $desiredFileContentString) {
Write-Host "Content already up to date for $repository"
exit 0
}
$checkWorkflowValidationSteps = & yq '.jobs.validate.steps' "$filePath"
$checkWorkflowValidationSize = $checkWorkflowValidationSteps | wc -l
$checkWorkflowReleaseSteps = & yq '.jobs.release.steps' "$filePath"
$checkWorkflowReleaseSize = $checkWorkflowReleaseSteps | wc -l
if ($checkWorkflowValidationSize -eq 1 -and $checkWorkflowReleaseSize -eq 1) {
Write-Host "Workflow $filePath is not valid in $repository"
exit 0
}
if (($checkWorkflowValidationSize -ne 21 -or $checkWorkflowReleaseSize -ne 15)) {
Write-Host "Workflow $filePath is not valid in $repository"
exit 0
}
if ($content[0] -ne $noteString) {
if ($content[0] -eq "name: cd") {
$content = @($noteString, "") + $content
} elseif ($content[0] -ilike "# Note:*") {
$content[0] = $noteString
} else {
Write-Host "$filePath :" $content[0]
}
}
[int] $contentJobsIndex = $content | Select-String "jobs:" | Select-Object -First 1 -ExpandProperty LineNumber
if ($contentJobsIndex -eq 0) {
continue
}
$contentJobsIndex -= 1
[string[]] $contentNoJobs = $content[0..$contentJobsIndex]
[string[]] $content = $contentNoJobs + $desiredJobs
Set-Content "$filePath" $content
$checkGitStatus = & git status --porcelain
if ([string]::IsNullOrEmpty($checkGitStatus)) {
Write-Host "No changes to commit for $repository"
exit 0
}
& gh repo fork --org "jetersen-cloud" --remote-name fork --remote
& git fetch --all
$branch = "chore/maven-cd-reusable-workflow"
& git checkout .
$checkBranchExist = & git show-ref $branch
if ([string]::IsNullOrEmpty("$checkBranchExist")) {
& git checkout -b $branch
} else {
$checkBranchUsed = & git rev-parse --abbrev-ref HEAD
if ("$checkBranchUsed" -ne "chore/maven-cd-reusable-workflow") {
& git checkout $branch
}
}
Set-Content "$filePath" $content
$checkGitStatus = & git status --porcelain
if ([string]::IsNullOrEmpty($checkGitStatus) -eq $false) {
& git add .
& git commit -S -s -m $prTitle
& git push -u fork $branch
} else {
Write-Host "No changes to commit for $repository"
}
# # create pull request
$checkPullRequest = & gh pr list --author $ghUser --limit 1 --search "use jenkins infra maven cd reusable workflow" --state all
if ($checkPullRequest | Select-String $prTitle) {
Write-Host "Pull request already exists for $repository"
exit 0
}
$pr = & gh pr create --title $prTitle --body $prBody
if ($pr -ilike "https://github.com*") {
Write-Host "Pull request created for $repository"
Start-Sleep -Seconds 5
} else {
Write-Host "Pull request creation failed for $repository"
}
### See https://github.com/lindell/multi-gitter#config-file for all options
# The maximum number of concurrent runs.
concurrent: 1
fork: true
fork-owner: jetersen-cloud
log-file: '-'
# The formating of the logs. Available values: text, json, json-pretty.
log-format: text
# The level of logging that should be made. Available values: trace, debug, info, error.
log-level: debug
# repo:
# - jenkinsci/opslevel-plugin
# # The name of a GitHub organization. All repositories in that organization will be used.
org:
- jenkinsci
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment