blob: 4513f2d96c8911283947a05f84146fe3315bcb1a [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/**
Kirill Mashchenko43001092018-12-25 05:28:53 +040011 * Returns a list of groups which user belongs
12 * @param username String
13 * @return list of groups [String]
Jakub Josefd44b6972018-01-23 17:55:57 +010014 */
Kirill Mashchenko43001092018-12-25 05:28:53 +040015def userGroups(username) {
16 res = []
17 def authorities = Jenkins.instance.securityRealm.loadUserByUsername(username).getAuthorities()
18 authorities.each {
19 res.add(it.toString())
Jakub Josefd44b6972018-01-23 17:55:57 +010020 }
Kirill Mashchenko43001092018-12-25 05:28:53 +040021 return res
22}
23
24/**
25 * Check if user belongs to group
26 * @param username String
27 * @param group String
28 * @return boolean result
29 */
30def userInGroup(username, group) {
31 def authorities = userGroups(username)
32 return authorities.any{it==group}
33}
34
35/**
36 * Check if user belongs to at least one of given groups
37 * @param username String
38 * @param groups [String]
39 * @return boolean result
40 */
41def userInGroups(username, groups) {
42 return groups.any{userInGroup(username, it)}
43}
44
45/**
46 * Returns current username from build
47 * @return username String
48 */
49def currentUsername() {
50 username = ''
51 wrap([$class: 'BuildUser']) {
Maxim Rasskazov2b7c3be2019-06-21 14:50:02 +040052 username = env.BUILD_USER_ID ?: 'jenkins'
Kirill Mashchenko43001092018-12-25 05:28:53 +040053 }
54 if (username) {
55 return username
56 } else {
57 throw new Exception('cant get current username')
58 }
59}
60
61/**
62 * Check if current user belongs to at least one of given groups
63 * @param groups [String]
64 * @return boolean result
65 */
66def currentUserInGroups(groups) {
67 username = currentUsername()
68 return userInGroups(username, groups)
69}
70
71/**
72 * Check if current user belongs to group
73 * @param group String
74 * @return boolean result
75 */
76def currentUserInGroup(group) {
77 username = currentUsername()
78 return userInGroup(username, group)
Jakub Josefd44b6972018-01-23 17:55:57 +010079}
80
81/**
Jakub Josef4edd7432017-05-10 17:58:56 +020082 * Get Jenkins job running builds
83 * @param jobName job name
84 * @return list of running builds
85 */
Jakub Joseffbe8c7c2017-05-11 13:35:11 +020086@NonCPS
Jakub Josef4edd7432017-05-10 17:58:56 +020087def getJobRunningBuilds(jobName){
Kirill Mashchenko43001092018-12-25 05:28:53 +040088 def job = Jenkins.instance.items.find{it -> it.name.equals(jobName)}
89 if(job){
90 return job.builds.findAll{build -> build.isBuilding()}
91 }
92 return []
Jakub Josef93f08e22017-06-05 19:14:53 +020093}
94
95@NonCPS
96def getRunningBuilds(job){
Kirill Mashchenko43001092018-12-25 05:28:53 +040097 return job.builds.findAll{build -> build.isBuilding()}
Jakub Josef93f08e22017-06-05 19:14:53 +020098}
99
100@NonCPS
101def killStuckBuilds(maxSeconds, job){
Kirill Mashchenko43001092018-12-25 05:28:53 +0400102 def common = new com.mirantis.mk.Common()
103 def result = true
104 def runningBuilds = getRunningBuilds(job)
105 def jobName = job.name
106 for(int j=0; j < runningBuilds.size(); j++){
107 int durationInSeconds = (System.currentTimeMillis() - runningBuilds[j].getTimeInMillis())/1000.0
108 if(durationInSeconds > maxSeconds){
109 result = false
110 def buildId = runningBuilds[j].id
111 common.infoMsg("Aborting ${jobName}-${buildId} which is running for ${durationInSeconds}s")
112 try{
113 runningBuilds[j].finish(hudson.model.Result.ABORTED, new java.io.IOException("Aborting build by long running jobs killer"));
114 result = true
115 }catch(e){
116 common.errorMsg("Error occured during aborting build: Exception: ${e}")
117 }
118 }
Jakub Josef93f08e22017-06-05 19:14:53 +0200119 }
Kirill Mashchenko43001092018-12-25 05:28:53 +0400120 return result
Jakub Josefd44b6972018-01-23 17:55:57 +0100121}
Richard Felkl838892f2018-06-12 17:58:20 +0200122
123/**
124 * Get Jenkins job object
125 * @param jobName job name
126 * @return job object that matches jobName
127 */
Denis Egorenkob1a40b62019-01-11 18:04:57 +0400128def getJobByName(jobName, regexp=false){
Richard Felkl838892f2018-06-12 17:58:20 +0200129 for(item in Hudson.instance.items) {
Denis Egorenkob1a40b62019-01-11 18:04:57 +0400130 if (regexp && item.name ==~ jobName || item.name == jobName) {
Richard Felkl838892f2018-06-12 17:58:20 +0200131 return item
132 }
133 }
134}
135
136/**
137 * Get Jenkins job parameters
138 * @param jobName job name
139 * @return HashMap with parameter names as keys and their values as values
140 */
141def getJobParameters(jobName){
142 def job = getJobByName(jobName)
143 def prop = job.getProperty(ParametersDefinitionProperty.class)
144 def params = new java.util.HashMap<String,String>()
145 if(prop != null) {
146 for(param in prop.getParameterDefinitions()) {
147 params.put(param.name, param.defaultValue)
148 }
149 }
150 return params
151}
Denis Egorenko46ff1382018-12-14 15:46:36 +0400152
153/**
154 * Get list of causes actions for given build
155 *
156 * @param build Job build object (like, currentBuild.rawBuild)
157 * @return list of causes actions for given build
158 */
159@NonCPS
160def getBuildCauseActions(build) {
Denis Egorenko270c5302019-01-10 20:53:01 +0400161 for(action in build.actions) {
162 if (action instanceof hudson.model.CauseAction) {
163 return action.causes
164 }
Denis Egorenko46ff1382018-12-14 15:46:36 +0400165 }
Denis Egorenko270c5302019-01-10 20:53:01 +0400166 return []
Denis Egorenko46ff1382018-12-14 15:46:36 +0400167}
168
169/**
170 * Get list of builds, triggered by Gerrit with given build
171 * @param build Job build object (like, currentBuild.rawBuild)
172 * @return list of builds with names and numbers
173 */
174@NonCPS
175def getGerritBuildContext(build) {
176 def causes = getBuildCauseActions(build)
Denis Egorenko270c5302019-01-10 20:53:01 +0400177 for(cause in causes) {
178 if (cause instanceof com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause) {
179 return cause.context.getOtherBuilds()
180 }
Denis Egorenko46ff1382018-12-14 15:46:36 +0400181 }
Denis Egorenko270c5302019-01-10 20:53:01 +0400182 return []
Denis Egorenko46ff1382018-12-14 15:46:36 +0400183}
184
185/**
186 * Wait for other jobs
187 * @param config config parameter:
188 * builds - List of job build objects, which should be checked
189 * checkBuilds - List of job names or regexps, which should be used to check provided builds list
190 * regexp - Wheither to use regexp or simple string matching
191 */
Denis Egorenko270c5302019-01-10 20:53:01 +0400192@NonCPS
Denis Egorenko46ff1382018-12-14 15:46:36 +0400193def waitForOtherBuilds(LinkedHashMap config){
Denis Egorenko270c5302019-01-10 20:53:01 +0400194 def context = config.get('context', 'gerrit')
195 def builds = []
196 if (context == 'gerrit') {
197 builds = getGerritBuildContext(currentBuild.rawBuild)
198 } else if (context == 'custom') {
199 builds = config.get('builds')
200 }
Denis Egorenko46ff1382018-12-14 15:46:36 +0400201 def checkBuilds = config.get('checkBuilds')
202 def regexp = config.get('regexp', false)
Denis Egorenko270c5302019-01-10 20:53:01 +0400203
Denis Egorenko46ff1382018-12-14 15:46:36 +0400204 def waitForBuilds = builds.findAll { build ->
205 def jobName = build.fullDisplayName.tokenize(' ')[0]
206 if (regexp) {
207 checkBuilds.find { jobName ==~ it }
208 } else {
209 jobName in checkBuilds
210 }
211 }
Denis Egorenko270c5302019-01-10 20:53:01 +0400212
213 def buildsMap = []
Denis Egorenko46ff1382018-12-14 15:46:36 +0400214 if (waitForBuilds) {
215 def waiting = true
Denis Egorenko270c5302019-01-10 20:53:01 +0400216 print "\u001B[36mWaiting for next jobs: ${waitForBuilds}\u001B[0m"
Denis Egorenko46ff1382018-12-14 15:46:36 +0400217 while(waiting) {
218 waiting = false
219 waitForBuilds.each { job ->
220 if (job.inProgress) {
221 waiting = true
Denis Egorenko270c5302019-01-10 20:53:01 +0400222 } else {
223 buildInfo = [
224 'jobName': job.fullDisplayName.tokenize(' ')[0],
225 'jobNumber': job.number,
226 ]
227 buildsMap.add(buildInfo)
Denis Egorenko46ff1382018-12-14 15:46:36 +0400228 }
229 }
230 }
231 }
Denis Egorenko270c5302019-01-10 20:53:01 +0400232 return buildsMap
Denis Egorenko46ff1382018-12-14 15:46:36 +0400233}