From 6c18b6ef76e413fb6df1dbb7d39ca090e929a7d8 Mon Sep 17 00:00:00 2001 From: Jan Stastny Date: Mon, 13 May 2024 10:48:10 +0200 Subject: [PATCH] kie-issues#776: automate PR merge into protected branches (#1194) Co-authored-by: jstastny-cz --- .../jenkinsfiles/Jenkinsfile.release.prepare | 27 ++++++++++++++++--- .../jenkinsfiles/Jenkinsfile.remove.branches | 16 +++++------ .../Jenkinsfile.tools.toggle-triggers | 12 ++++++--- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/dsl/seed/jenkinsfiles/Jenkinsfile.release.prepare b/dsl/seed/jenkinsfiles/Jenkinsfile.release.prepare index 3a4ad4bbc..1dcdc0cda 100644 --- a/dsl/seed/jenkinsfiles/Jenkinsfile.release.prepare +++ b/dsl/seed/jenkinsfiles/Jenkinsfile.release.prepare @@ -14,6 +14,7 @@ pipeline { environment { CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}") + PR_BRANCH_HASH = "${util.generateHash(10)}" } stages { @@ -124,10 +125,19 @@ pipeline { writeYaml file: seedConfigFile, data: seedConfig, overwrite: true if (githubscm.isThereAnyChanges()) { - sh 'git diff' - githubscm.commitChanges("Add release branch ${getEcosystemReleaseBranch()} after cut-off") - githubscm.pushObject('origin', env.SEED_CONFIG_FILE_GIT_BRANCH, env.SEED_CONFIG_FILE_GIT_AUTHOR_PUSH_CREDS_ID) + String prBranch = getPRBranch(getEcosystemReleaseBranch()) + githubscm.createBranch(prBranch) + sh 'git diff' + String commitMsg = "Add release branch ${getEcosystemReleaseBranch()} after cut-off" + githubscm.setUserConfigFromCreds(env.SEED_CONFIG_FILE_GIT_AUTHOR_PUSH_CREDS_ID) + githubscm.commitChanges(commitMsg) + githubscm.pushObject('origin', prBranch, env.SEED_CONFIG_FILE_GIT_AUTHOR_PUSH_CREDS_ID) + // Create PR + def prBody = "Generated by ${BUILD_URL}" + // raise a PR by another user to allow approval by the robot user which is a committer + String prLink = githubscm.createPR(commitMsg, prBody, env.SEED_CONFIG_FILE_GIT_BRANCH, env.SEED_CONFIG_FILE_GIT_AUTHOR_CREDS_ID) + approveAndMergePR(prLink) sendNotification("Main config has been updated with release branch ${getEcosystemReleaseBranch()}") } else { println '[WARN] No changes to commit' @@ -248,6 +258,17 @@ void getOrCreateGitBranch(String branch, String credentialsId) { } } +String getPRBranch(String newConfigEntry) { + return "configure-branch-${newConfigEntry}-${env.PR_BRANCH_HASH}" +} + +void approveAndMergePR(String prLink) { + if (prLink?.trim()) { + githubscm.approvePR(prLink, env.SEED_CONFIG_FILE_GIT_AUTHOR_PUSH_CREDS_ID) + githubscm.mergePR(prLink, env.SEED_CONFIG_FILE_GIT_AUTHOR_PUSH_CREDS_ID) + } +} + void sendNotification(String body) { echo 'Send Notification' echo body diff --git a/dsl/seed/jenkinsfiles/Jenkinsfile.remove.branches b/dsl/seed/jenkinsfiles/Jenkinsfile.remove.branches index 37fb1dd67..e489022f8 100644 --- a/dsl/seed/jenkinsfiles/Jenkinsfile.remove.branches +++ b/dsl/seed/jenkinsfiles/Jenkinsfile.remove.branches @@ -70,9 +70,7 @@ pipeline { String targetBranch = "${getBuildBranch()}" if (githubscm.isThereAnyChanges()) { String prLink = commitAndCreatePR(commitMsg, localBranch, targetBranch) - sh "git checkout ${targetBranch}" - mergeAndPush(prLink, targetBranch) - githubscm.removeRemoteBranch('origin', localBranch, getGitAuthorPushCredsId()) + approveAndMergePR(prLink) } else { println '[WARN] no changes to commit' } @@ -106,15 +104,15 @@ String getBuildBranch() { return env.GIT_BRANCH_TO_BUILD } -void getRemoveBranch(){ +void getRemoveBranch() { return params.BRANCH_TO_REMOVE } -void getGitAuthorCredsId(){ +void getGitAuthorCredsId() { return env.GIT_AUTHOR_CREDENTIALS_ID } -void getGitAuthorPushCredsId(){ +void getGitAuthorPushCredsId() { return env.GIT_AUTHOR_PUSH_CREDENTIALS_ID } @@ -140,9 +138,9 @@ String commitAndCreatePR(String commitMsg, String localBranch, String targetBran return githubscm.createPR(commitMsg, prBody, targetBranch, getGitAuthorCredsId()) } -void mergeAndPush(String prLink, String targetBranch) { +void approveAndMergePR(String prLink) { if (prLink?.trim()) { - githubscm.mergePR(prLink, getGitAuthorCredsId()) - githubscm.pushObject('origin', targetBranch, getGitAuthorPushCredsId()) + githubscm.approvePR(prLink, getGitAuthorPushCredsId()) + githubscm.mergePR(prLink, getGitAuthorPushCredsId()) } } diff --git a/dsl/seed/jenkinsfiles/Jenkinsfile.tools.toggle-triggers b/dsl/seed/jenkinsfiles/Jenkinsfile.tools.toggle-triggers index 578af03a8..7048ab022 100644 --- a/dsl/seed/jenkinsfiles/Jenkinsfile.tools.toggle-triggers +++ b/dsl/seed/jenkinsfiles/Jenkinsfile.tools.toggle-triggers @@ -64,10 +64,7 @@ pipeline { String prLink = githubscm.createPR(commitMsg, prBody, getSeedConfigBranch(), getSeedConfigAuthorCredsId()) // Merge automatically PR - sh "git checkout ${getSeedConfigBranch()}" - githubscm.mergePR(prLink, getSeedConfigAuthorCredsId()) - githubscm.pushObject('origin', getSeedConfigBranch(), getSeedConfigAuthorPushCredsId()) - githubscm.removeRemoteBranch('origin', getPRBranch(), getSeedConfigAuthorPushCredsId()) + approveAndMergePR(prLink) } else { println '[WARN] no changes to commit' } @@ -133,3 +130,10 @@ void sendNotification(String body) { void sendErrorNotification() { sendNotification("Job #${BUILD_NUMBER} was: **${currentBuild.currentResult}**\nPlease look here: ${BUILD_URL}") } + +void approveAndMergePR(String prLink) { + if (prLink?.trim()) { + githubscm.approvePR(prLink, getSeedConfigAuthorPushCredsId()) + githubscm.mergePR(prLink, getSeedConfigAuthorPushCredsId()) + } +}