diff --git a/jobs-dsl/jobs/gitea_events/eva_pr_link_webhook.groovy b/jobs-dsl/jobs/gitea_events/eva_pr_link_webhook.groovy new file mode 100644 index 0000000..488097a --- /dev/null +++ b/jobs-dsl/jobs/gitea_events/eva_pr_link_webhook.groovy @@ -0,0 +1,24 @@ +pipelineJob('gitea-events/eva-pr-link-webhook') { + parameters { + string { + description('Data from webhook JSON') + name('DATA_JSON') + defaultValue('') + } + } + + definition { + cpsScm { + scm { + git { + remote { + url("${JENKINS_GIT_REPOSITORY_URL}/DevOps/jenkins-pipelines.git") + credentials("${JENKINS_GIT_CREDENTIALS_HTTP}") + } + branch('master') + } + } + scriptPath('pipelines/gitea-events/eva-pr-link-webhook.groovy') + } + } +} diff --git a/pipelines/gitea-events/eva-pr-link-webhook.groovy b/pipelines/gitea-events/eva-pr-link-webhook.groovy new file mode 100644 index 0000000..ad38255 --- /dev/null +++ b/pipelines/gitea-events/eva-pr-link-webhook.groovy @@ -0,0 +1,60 @@ +@Library('shared-lib') _ + +import tech.avroid.api.Eva + +properties([ + buildDiscarder(logRotator(artifactNumToKeepStr: '10', + numToKeepStr: '10')), + parameters([ + string(name: 'DATA_JSON', defaultValue: '') + ]) +]) + +podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"), + yaml: """ + apiVersion: v1 + kind: Pod + spec: + containers: + - name: alpine + image: ${env.JENKINS_DOCKER_REGISTRY}/docker-hub-proxy/library/alpine:3.18.5 + command: + - sleep + args: + - 99d + resources: + limits: + cpu: 100m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi +""" +) { + node(POD_LABEL) { + stage('Add PR link to Eva task') { + Map dataJSON = readJSON text: params.DATA_JSON + String prLink = dataJSON.pull_request.url + String taskPattern = '[A-Z]+(-[A-Z]+)?-[0-9]+' + // Eva Api adds name with Spaces, but returnes 500 response code + String prName = dataJSON.pull_request.title.replace(' ','_') + List prTasks = prName.findAll(taskPattern) + Eva eva = new Eva(this, env.JENKINS_EVA_URL, env.JENKINS_EVA_CREDENTIALS) + + if (prTasks.isEmpty()) { + prTasks = dataJSON.pull_request.body.findAll(taskPattern) + } + + prTasks.each { String taskCode -> + String taskId = eva.getTaskId(taskCode) + + if (taskId) { + eva.createLink(taskId, prName, prLink) + println "Eva task $taskCode linked with PR $prLink" + } else { + println "Eva task $taskCode doesn't exist" + } + } + } + } +} diff --git a/pipelines/gitea-events/jobs-runner.groovy b/pipelines/gitea-events/jobs-runner.groovy index c8bb725..d11fb1a 100644 --- a/pipelines/gitea-events/jobs-runner.groovy +++ b/pipelines/gitea-events/jobs-runner.groovy @@ -20,67 +20,76 @@ Map projects = [ ] ] -String getBranch(Map fullJSON) { - String refHead = 'refs/heads/' - String refTags = 'refs/tags/' - - if (fullJSON.ref.contains(refHead)) { - return fullJSON.ref.split(refHead).last() - } - if (fullJSON.ref.contains(refTags)) { - return fullJSON.ref.split(refTags).last() - } - return fullJSON.ref -} - podTemplate(workspaceVolume: hostPathWorkspaceVolume(hostPath: "/data"), - yaml: ''' - apiVersion: v1 - kind: Pod - spec: - containers: - - name: linux - image: harbor.avroid.tech/devops/base-build-image:1.0 - command: - - sleep - args: - - 99d - resources: - limits: - cpu: 300m - memory: 256Mi - requests: - cpu: 300m - memory: 256Mi - imagePullSecrets: - - name: harbor-registry-secret -''' + yaml: """ + apiVersion: v1 + kind: Pod + spec: + containers: + - name: linux + image: ${env.JENKINS_DOCKER_REGISTRY}/docker-hub-proxy/library/alpine:3.18.5 + command: + - sleep + args: + - 99d + resources: + limits: + cpu: 100m + memory: 64Mi + requests: + cpu: 100m + memory: 64Mi +""" ) { - node(POD_LABEL) { - stage('Run job') { - try { - Map fullJSON = readJSON text: jsonEvent - String branch = getBranch(fullJSON) - String projectName = fullJSON.repository.full_name + node(POD_LABEL) { + stage('Run job') { + try { + Map fullJSON = readJSON text: jsonEvent + String projectName = fullJSON.repository.full_name + currentBuild.description = "Repo: ${fullJSON.repository.full_name}" + currentBuild.displayName = "#${env.BUILD_ID} Skip" - println fullJSON //debug - currentBuild.description = "Repo: ${fullJSON.repository.full_name} ${branch}" - currentBuild.displayName = "#${env.BUILD_ID} Skip" - projects[projectName].each { project -> - currentBuild.displayName = "#${env.BUILD_ID} Run for branches" - project.jobs.each { job -> - build job: job.job, - parameters: [ - string(name: 'PROJECT_NAME', value: projectName), - string(name: 'BRANCH_NAME', value: branch), - string(name: 'DATA_JSON', value: fullJSON.toString()) - ], - wait: false - } - } - } catch (groovy.lang.MissingPropertyException e) { - println 'This job should run through gitea webhook:\n' + e - } - } - } + //info for Debug + //println(fullJSON) + if (fullJSON.ref) { + String branch = '' + currentBuild.description = "Repo: ${fullJSON.repository.full_name} $branch" + String refHead = 'refs/heads/' + String refTags = 'refs/tags/' + + if (fullJSON.ref.contains(refHead)) { + branch = fullJSON.ref.split(refHead).last() + } else { + branch = fullJSON.ref.split(refTags).last() + } + + // Check project in map and start to work with it + projects[projectName].each { Map project -> + currentBuild.displayName = "#${env.BUILD_ID} Run for branches" + project.jobs.each { Map job -> + build job: job.job, + parameters: [ + string(name: 'PROJECT_NAME', value: projectName), + string(name: 'BRANCH_NAME', value: branch), + string(name: 'DATA_JSON', value: fullJSON.toString()) + ], + wait: false + } + } + } + + // Start tasks for all repositories by PR + if (fullJSON.action == 'opened') { + currentBuild.displayName = "#${env.BUILD_ID} run on PR" + build job: 'gitea-events/eva-pr-link-webhook', + parameters: [ + string(name: 'DATA_JSON', value: fullJSON.toString()) + ], + wait: false + } + } catch (groovy.lang.MissingPropertyException e) { + println 'This job should run through gitea webhook:\n' + e + } + } + } }