Skip to content

Commit

Permalink
kie-issues#776: automate PR merge into protected branches (#1194)
Browse files Browse the repository at this point in the history
Co-authored-by: jstastny-cz <jan.stastny@ibm.com>
  • Loading branch information
jstastny-cz and jstastny-cz committed May 13, 2024
1 parent 116bfc3 commit 6c18b6e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
27 changes: 24 additions & 3 deletions dsl/seed/jenkinsfiles/Jenkinsfile.release.prepare
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pipeline {

environment {
CI_EMAIL_TO = credentials("${JENKINS_EMAIL_CREDS_ID}")
PR_BRANCH_HASH = "${util.generateHash(10)}"
}

stages {
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
16 changes: 7 additions & 9 deletions dsl/seed/jenkinsfiles/Jenkinsfile.remove.branches
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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())
}
}
12 changes: 8 additions & 4 deletions dsl/seed/jenkinsfiles/Jenkinsfile.tools.toggle-triggers
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down Expand Up @@ -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())
}
}

0 comments on commit 6c18b6e

Please sign in to comment.