This is an automated email from the ASF dual-hosted git repository.

jstastnycz pushed a commit to branch main
in repository 
https://gitbox.apache.org/repos/asf/incubator-kie-kogito-pipelines.git


The following commit(s) were added to refs/heads/main by this push:
     new 6c18b6ef kie-issues#776: automate PR merge into protected branches 
(#1194)
6c18b6ef is described below

commit 6c18b6ef76e413fb6df1dbb7d39ca090e929a7d8
Author: Jan Stastny <[email protected]>
AuthorDate: Mon May 13 10:48:10 2024 +0200

    kie-issues#776: automate PR merge into protected branches (#1194)
    
    Co-authored-by: jstastny-cz <[email protected]>
---
 dsl/seed/jenkinsfiles/Jenkinsfile.release.prepare  | 27 +++++++++++++++++++---
 dsl/seed/jenkinsfiles/Jenkinsfile.remove.branches  | 16 ++++++-------
 .../jenkinsfiles/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 3a4ad4bb..1dcdc0cd 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 37fb1dd6..e489022f 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 578af03a..7048ab02 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())
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to