From 73baa394f1fe4bc9be78d6b855a844014b9829be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Mattm=C3=BCller?= Date: Tue, 5 Sep 2023 14:16:39 +0200 Subject: [PATCH] jobs/updateSrcRev: added creating a PR, set clean build as default MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Implemented creating a pull request using REST API after pushing the changes (new source revisions) to a nightly branch. Additionally set the default value of the clean build parameter to true. This is necessary to have per default a proper operation of this job Signed-off-by: Marc Mattmüller --- jobs/Jenkinsfile_UpdateSrcRevisions | 63 +++++++++++++++++++++++++---- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/jobs/Jenkinsfile_UpdateSrcRevisions b/jobs/Jenkinsfile_UpdateSrcRevisions index 0edf327..380601e 100644 --- a/jobs/Jenkinsfile_UpdateSrcRevisions +++ b/jobs/Jenkinsfile_UpdateSrcRevisions @@ -9,7 +9,7 @@ pipeline { parameters { string(name: 'BUILD_BRANCH', defaultValue: 'main', description: 'Enter the branch of the NWL to build (default = main), will skip deployment if not main') - booleanParam(name: 'CLEAN_BUILD', defaultValue: false, description: 'do a clean build, i.e. remove the yocto directory and start from scratch') + booleanParam(name: 'CLEAN_BUILD', defaultValue: true, description: 'do a clean build, i.e. remove the yocto directory and start from scratch') booleanParam(name: 'DRY_RUN', defaultValue: true, description: 'do a dry run, i.e. without committing and pushing') } @@ -91,14 +91,57 @@ def updateTheSourceRevisions(commonHelpers, repoDir, theBranch) { } } //--------------------------------------------------------------------------------------------------------------------- +def setupNewBranchAndGetGitPushPostfix(theNewBranch) { + if(sh(returnStatus: true, script: "git checkout ${theNewBranch}") == 0) { + println "----------------------------------------------------\n\ + NOTE: Branch ${theNewBranch} already available \n\ + and potentially as well a pull/merge request.\n\ + --> handle this branch and request first\n----------------------------------------------------" + error("Available branch ${theNewBranch} detected --> handle this first") + } + + sh(script: "git checkout -b ${theNewBranch}") +} +//--------------------------------------------------------------------------------------------------------------------- +def createPR(theBranch) { + def prReviewer = "bard" + def prProject = "NM-NSP" + def prRepo = "netmodule-wireless-linux" + + def bitbucketRestApiUrl = "https://bitbucket.gad.local/rest/api/1.0" + def prTitle = "Nightly: Src Rev Update" + def prDescription = "CI is ppdating the source revisions to HEAD" + def prRepository = "\"repository\": {\"slug\": \"${prRepo}\", \"name\": null, \"project\": {\"key\": \"${prProject}\"}}" + def prDataJson = "{\"title\": \"${prTitle}\", \"description\": \"${prDescription}\",\ + \"state\": \"OPEN\", \"open\": true, \"closed\": false,\ + \"fromRef\": {\"id\": \"refs/heads/${theBranch}\", ${prRepository}},\ + \"toRef\": {\"id\": \"refs/heads/main\", ${prRepository}},\ + \"locked\": false,\ + \"reviewers\": [{\"user\": {\"name\": \"${prReviewer}\"}}], \"links\": {\"self\": [null]}}" + def prUrl = "${bitbucketRestApiUrl}/projects/${prProject}/repos/${prRepo}/pull-requests" + def authHeader = "Authorization: Bearer MDI1NjczNzAyMTI3OrbDBQestfmsi/Iys5qvKP4Tgulp" + def acceptHeader = "Accept: application/json" + def contentHeader = "Content-Type: application/json" + + def prResponse = sh(returnStdout: true, + script: "curl --request POST --url '${prUrl}' \ + --header '${authHeader}' \ + --header '${acceptHeader}' \ + --header '${contentHeader}' \ + --data '${prDataJson}'").toString() + if(prResponse.contains("error")) { + error("Failed creating PR/MR:\n${prResponse}") + } +} +//--------------------------------------------------------------------------------------------------------------------- def commitAndPushTheChanges(commonHelpers, repoDir) { def commitMsg = "srcrev: updated source revisions by Jenkins Job" + def nightlyBranch = "nightly" dir(repoDir) { - if(sh(returnStdout: true, script: "git status | grep \"modified:\" | grep -v coreos | wc -l").toInteger() != 0) { - def itemFindCmd = "git status | grep \"modified:\" | grep -v coreos | cut -d':' -f2 | sed -e 's/^[ ]*//' | cut -d' ' -f1" - def changedItems = sh(returnStdout: true, script: "${itemFindCmd}") - sh(label: "Stage tracked and changed git files", returnStdout: true, script: "git add ${changedItems}") + if(sh(returnStdout: true, script: "git status | grep \"modified:\" | wc -l").toInteger() != 0) { + def changedItems = sh(returnStdout: true, script: "git status") + sh(label: "Stage tracked and changed git files", returnStdout: true, script: "git add \\*") if(params.DRY_RUN) { println "DRY RUN: commit message = '${commitMsg}'\n changes = ${changedItems}" @@ -106,13 +149,19 @@ def commitAndPushTheChanges(commonHelpers, repoDir) { } else { def theCredentials = commonHelpers.getGitCredentialID() + setupNewBranchAndGetGitPushPostfix("${nightlyBranch}") + println "Commit and Push changes; message = '${commitMsg}'\nchanges = ${changedItems}" sshagent (credentials: [theCredentials]) { - println "Commit and Push changes; message = '${commitMsg}'\nchanges = ${changedItems}" + // ToDo: replace the user.email and user.name once a CI user is setup in the + // active directory. sh(label: "Commit and push changes", returnStdout: true, script: """ + git config --global user.email "marc.mattmueller@netmodule.com" + git config --global user.name "Marc Mattmüller" git commit -m "${commitMsg}" - git push + git push -u origin ${theNewBranch} """) } + createPR("${nightlyBranch}") } } }