blob: 8b35aca2e21a417e4e1a2614f1fda7a369b584f6 [file] [log] [blame]
Jakub Josef4edd7432017-05-10 17:58:56 +02001package com.mirantis.mk
Jakub Josef93f08e22017-06-05 19:14:53 +02002import com.cloudbees.groovy.cps.NonCPS
Jakub Josef4edd7432017-05-10 17:58:56 +02003
4/**
5 *
6 * Jenkins common functions
7 *
8 */
9
10/**
Jakub Josefd44b6972018-01-23 17:55:57 +010011 * Tests if current user belongs to given group
12 * @param groupName name of the group you want to verify user presence
13 * @return boolean result
14 */
15def currentUserInGroup(groupName){
Jakub Josef052e9632018-02-14 14:10:51 +010016 return currentUserInGroups([groupName])
17}
18/**
19 * Tests if current user belongs to at least one of given groups
20 * @param groups list of group names you want to verify user presence
21 * @return boolean result
22 */
23def currentUserInGroups(groups){
Jakub Josefd44b6972018-01-23 17:55:57 +010024 def hasAccess = false
25 wrap([$class: 'BuildUser']) {
26 def authorities = Jenkins.instance.securityRealm.loadUserByUsername(BUILD_USER).getAuthorities()
27 for(int i=0;i < authorities.size();i++){
Jakub Josef052e9632018-02-14 14:10:51 +010028 if(groups.contains(authorities[i])){
Jakub Josefd44b6972018-01-23 17:55:57 +010029 hasAccess=true
30 break
31 }
32 }
33 }
34 return hasAccess
35}
36
37/**
Jakub Josef4edd7432017-05-10 17:58:56 +020038 * Get Jenkins job running builds
39 * @param jobName job name
40 * @return list of running builds
41 */
Jakub Joseffbe8c7c2017-05-11 13:35:11 +020042@NonCPS
Jakub Josef4edd7432017-05-10 17:58:56 +020043def getJobRunningBuilds(jobName){
Jakub Joseffbe8c7c2017-05-11 13:35:11 +020044 def job = Jenkins.instance.items.find{it -> it.name.equals(jobName)}
45 if(job){
46 return job.builds.findAll{build -> build.isBuilding()}
47 }
48 return []
Jakub Josef93f08e22017-06-05 19:14:53 +020049}
50
51@NonCPS
52def getRunningBuilds(job){
53 return job.builds.findAll{build -> build.isBuilding()}
54}
55
56@NonCPS
57def killStuckBuilds(maxSeconds, job){
58 def common = new com.mirantis.mk.Common()
59 def result = true
60 def runningBuilds = getRunningBuilds(job)
61 def jobName = job.name
62 for(int j=0; j < runningBuilds.size(); j++){
63 int durationInSeconds = (System.currentTimeMillis() - runningBuilds[j].getTimeInMillis())/1000.0
64 if(durationInSeconds > maxSeconds){
65 result = false
66 def buildId = runningBuilds[j].id
67 common.infoMsg("Aborting ${jobName}-${buildId} which is running for ${durationInSeconds}s")
68 try{
69 runningBuilds[j].finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build by long running jobs killer"));
70 result = true
71 }catch(e){
72 common.errorMsg("Error occured during aborting build: Exception: ${e}")
73 }
74 }
75 }
76 return result
Jakub Josefd44b6972018-01-23 17:55:57 +010077}
Richard Felkl838892f2018-06-12 17:58:20 +020078
79/**
80 * Get Jenkins job object
81 * @param jobName job name
82 * @return job object that matches jobName
83 */
84def getJobByName(jobName){
85 for(item in Hudson.instance.items) {
86 if(item.name == jobName){
87 return item
88 }
89 }
90}
91
92/**
93 * Get Jenkins job parameters
94 * @param jobName job name
95 * @return HashMap with parameter names as keys and their values as values
96 */
97def getJobParameters(jobName){
98 def job = getJobByName(jobName)
99 def prop = job.getProperty(ParametersDefinitionProperty.class)
100 def params = new java.util.HashMap<String,String>()
101 if(prop != null) {
102 for(param in prop.getParameterDefinitions()) {
103 params.put(param.name, param.defaultValue)
104 }
105 }
106 return params
107}
Denis Egorenkob5208e42018-12-14 15:46:36 +0400108
109/**
110 * Get list of causes actions for given build
111 *
112 * @param build Job build object (like, currentBuild.rawBuild)
113 * @return list of causes actions for given build
114 */
115@NonCPS
116def getBuildCauseActions(build) {
117 def causeAction = build.actions.find { it -> it instanceof hudson.model.CauseAction }
118 if(causeAction) {
119 return causeAction.causes
120 } else {
121 return []
122 }
123}
124
125/**
126 * Get list of builds, triggered by Gerrit with given build
127 * @param build Job build object (like, currentBuild.rawBuild)
128 * @return list of builds with names and numbers
129 */
130@NonCPS
131def getGerritBuildContext(build) {
132 def causes = getBuildCauseActions(build)
133 if (causes) {
134 def gerritTriggerCause = causes.find { cause ->
135 cause instanceof com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause
136 }
137 return gerritTriggerCause.context.getOtherBuilds()
138 } else {
139 return []
140 }
141}
142
143/**
144 * Wait for other jobs
145 * @param config config parameter:
146 * builds - List of job build objects, which should be checked
147 * checkBuilds - List of job names or regexps, which should be used to check provided builds list
148 * regexp - Wheither to use regexp or simple string matching
149 */
150def waitForOtherBuilds(LinkedHashMap config){
151 def common = new com.mirantis.mk.Common()
152 def builds = config.get('builds')
153 def checkBuilds = config.get('checkBuilds')
154 def regexp = config.get('regexp', false)
155 def waitForBuilds = builds.findAll { build ->
156 def jobName = build.fullDisplayName.tokenize(' ')[0]
157 if (regexp) {
158 checkBuilds.find { jobName ==~ it }
159 } else {
160 jobName in checkBuilds
161 }
162 }
163 if (waitForBuilds) {
164 def waiting = true
165 common.infoMsg("Waiting for next jobs: ${waitForBuilds}")
166 while(waiting) {
167 waiting = false
168 waitForBuilds.each { job ->
169 if (job.inProgress) {
170 waiting = true
171 }
172 }
173 }
174 }
175}