Skip to content

Commit

Permalink
kie-issues#776: automate PR merge into protected branches
Browse files Browse the repository at this point in the history
  • Loading branch information
jstastny-cz committed May 10, 2024
1 parent 70426a5 commit 9cacc2e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .ci/jenkins/Jenkinsfile.post-release
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
29 changes: 26 additions & 3 deletions .ci/jenkins/Jenkinsfile.setup-branch
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.'
}
}
}
}
}
Expand Down Expand Up @@ -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}"
}

0 comments on commit 9cacc2e

Please sign in to comment.