[DO-103] Add eva link with pr (#13)

Reviewed-on: https://git.avroid.tech/DevOps/jenkins-pipelines/pulls/13
Reviewed-by: Denis Patrakeev <denis.patrakeev@avroid.tech>
Reviewed-by: Andrey Danin <andrey.danin@avroid.tech>
Co-authored-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
Co-committed-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
This commit is contained in:
aleksandr.vodyanov
2024-01-10 17:15:16 +03:00
committed by Aleksandr Vodyanov
parent 4a54b6de6b
commit 5d6e266419
3 changed files with 153 additions and 60 deletions

View File

@@ -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"
}
}
}
}
}

View File

@@ -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
}
}
}
}