From 9cacc2ed6067831a6de2722370b4a4f7b7764fbc Mon Sep 17 00:00:00 2001 From: jstastny-cz Date: Fri, 10 May 2024 11:23:44 +0200 Subject: [PATCH] kie-issues#776: automate PR merge into protected branches --- .ci/jenkins/Jenkinsfile.post-release | 2 +- .ci/jenkins/Jenkinsfile.setup-branch | 29 +++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/.ci/jenkins/Jenkinsfile.post-release b/.ci/jenkins/Jenkinsfile.post-release index f0217268d..ae16bdf77 100644 --- a/.ci/jenkins/Jenkinsfile.post-release +++ b/.ci/jenkins/Jenkinsfile.post-release @@ -78,7 +78,7 @@ pipeline { } // Tag repository - githubscm.tagLocalAndRemoteRepository('origin', getKogitoVersion(), getGitAuthorCredsId(), env.BUILD_TAG, true) + githubscm.tagLocalAndRemoteRepository('origin', getKogitoVersion(), getGitAuthorPushCredsId(), env.BUILD_TAG, true) } } } diff --git a/.ci/jenkins/Jenkinsfile.setup-branch b/.ci/jenkins/Jenkinsfile.setup-branch index 4560937ce..87c317be5 100644 --- a/.ci/jenkins/Jenkinsfile.setup-branch +++ b/.ci/jenkins/Jenkinsfile.setup-branch @@ -32,7 +32,8 @@ pipeline { } environment { - CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}") + CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}")\ + PR_BRANCH_HASH = "${util.generateHash(10)}" } stages { @@ -107,16 +108,19 @@ pipeline { if (!antoraConfig.content.sources[0].branches.find { it == getBuildBranch() }) { antoraConfig.content.sources[0].branches.add(getBuildBranch()) } - } // Add changed files, commit, open and merge PR if (githubscm.isThereAnyChanges()) { + String branchName = getPRBranchName() + githubScm.createBranch(branchName) githubscm.setUserConfigFromCreds(getGitAuthorPushCredsId()) githubscm.commitChanges("Add branch ${getBuildBranch()} for generation") - githubscm.pushObject('origin', 'main', getGitAuthorPushCredsId()) + String prLink = commitAndCreatePR("Add branch ${getBuildBranch()} for generation", branchName,"main") + approveAndMergePR(prLink) } else { echo 'No changes to push.' } + } } } } @@ -171,3 +175,22 @@ void updateYaml(String filePath, Closure updateClosure) { boolean isMainBranch() { return env.IS_MAIN_BRANCH ? env.IS_MAIN_BRANCH.toBoolean() : false } + +void approveAndMergePR(String prLink) { + if (prLink?.trim()) { + githubscm.approvePR(prLink, getGitAuthorPushCredsId()) + githubscm.mergePR(prLink, getGitAuthorPushCredsId()) + } +} + +String commitAndCreatePR(String commitMsg, String localBranch, String targetBranch) { + def prBody = "Generated by build ${BUILD_TAG}: ${BUILD_URL}" + githubscm.setUserConfigFromCreds(getGitAuthorPushCredsId()) + githubscm.commitChanges(commitMsg) + githubscm.pushObject('origin', localBranch, getGitAuthorPushCredsId()) + return githubscm.createPR(commitMsg, prBody, targetBranch, getGitAuthorCredsId()) +} + +String getPRBranchName() { + return "${getKogitoVersion().toLowerCase()}-${env.PR_BRANCH_HASH}" +}