Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kie-issues#776: automate PR merge into protected branches #626

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}"
}
Loading