Polish and reformat the code.

This commit is contained in:
Manuele Simi 2020-03-12 11:13:24 -04:00
parent 4af8c62144
commit de2fba08b8
1 changed files with 50 additions and 52 deletions

102
Jenkinsfile vendored
View File

@ -1,5 +1,3 @@
def counts = 0
def projects2artifacts = [:]
def artifacts2projects = [:]
def modules2deps = [:]
@ -13,21 +11,21 @@ pipeline {
}
parameters {
string(name: 'jenkins_project',
defaultValue: '',
description: 'The name of the Jenkins project to analyze.')
defaultValue: '',
description: 'The name of the Jenkins project to analyze.')
}
stages {
stage('walking projects') {
steps {
script {
script {
Jenkins.get().getAllItems(TopLevelItem.class).each { p ->
projects2artifacts[p.name] = []
p.getAllJobs().each { j -> projects2artifacts[p.name] << j.name; artifacts2projects[j.name] = p.name }
projects2artifacts[p.name] = []
p.getAllJobs().each { j -> projects2artifacts[p.name] << j.name; artifacts2projects[j.name] = p.name }
}
println "FOUND ${projects2artifacts.size()} projects"
//projects2artifacts.each { k,v -> println ("PROJECT ${k} BUILDS ${v}") }
}
}
}
}
@ -36,8 +34,8 @@ pipeline {
script {
// get all the maven modules and their dependencies
Jenkins.get().getAllItems(hudson.maven.MavenModule.class).each { m ->
modules2deps[m.name] = []
m.getDependencies().each { d -> modules2deps[m.name] << "${d.groupId}:${d.artifactId}" }
modules2deps[m.name] = []
m.getDependencies().each { d -> modules2deps[m.name] << "${d.groupId}:${d.artifactId}" }
}
println "FOUND ${modules2deps.size()} modules"
//modules2deps.each { k,v -> println ("MODULE ${k} DEPENDS on ${v}") }
@ -46,19 +44,19 @@ pipeline {
}
stage('analyze downstream projects') {
steps {
script {
// println "PROJECT ${inputProject} BUILDS ${projects2artifacts[inputProject]} artifacts"
// first, let's find out what components depends on the project's artifacts (i.e. downstream dependencies)
def downstreamdeps = [:]
report['downstream_modules'] = [:]
report['downstream_projects'] = []
report['downstream_modules']['L1'] = [:]
projects2artifacts[inputProject].each { a -> report = analyzeDependency(modules2deps, artifacts2projects, report, a, 1)}
steps {
script {
// println "PROJECT ${inputProject} BUILDS ${projects2artifacts[inputProject]} artifacts"
// first, let's find out what components depend on the project's artifacts (i.e. downstream dependencies)
def downstreamdeps = [:]
report['downstream_modules'] = [:]
report['downstream_projects'] = []
report['downstream_modules']['L1'] = [:]
projects2artifacts[inputProject].each { a -> report = analyzeDependency(modules2deps, artifacts2projects, report, a, 1) }
}
}
}
}
stage('print report') {
steps {
@ -67,21 +65,21 @@ pipeline {
}
}
}
}
}
}
// look for modules that use this artifact
def findDownstreamDependencies(modules2deps, artifact) {
def downdeps = []
//println "Looking for users of ${artifact}"
modules2deps.each { k,v -> if (v.contains("${artifact}")) downdeps << k }
modules2deps.each { k, v -> if (v.contains("${artifact}")) downdeps << k }
downdeps
}
//build the report of the given dependency, go recursive on its dependencies
def analyzeDependency(modules2deps, artifacts2projects, report, artifact, deep) {
def level = "L${deep}"
report['downstream_modules'][level][artifact] = [ 'dependencies':[], 'projects':[] ]
def analyzeDependency(modules2deps, artifacts2projects, report, artifact, depth) {
GString level = "L${depth}"
report['downstream_modules'][level][artifact] = ['dependencies': [], 'projects': []]
// get all downstream dependencies
report['downstream_modules'][level][artifact]['dependencies'].addAll(findDownstreamDependencies(modules2deps, artifact))
@ -89,17 +87,17 @@ def analyzeDependency(modules2deps, artifacts2projects, report, artifact, deep)
// add the project that builds the artifact
report['downstream_modules'][level][artifact]['projects'] << artifacts2projects[artifact]
report['downstream_projects'] << artifacts2projects[artifact]
report['downstream_projects'] << artifacts2projects[artifact]
// then we look for the projects that build the downstream dependencies and finally we go recursive
if (report['downstream_modules'][level][artifact]['dependencies']) {
def nextDeep = ++deep
def nextDepth = ++depth
report['downstream_modules']["L${nextDeep}"] = [:]
report['downstream_modules'][level][artifact]['dependencies'].each { d ->
report['downstream_modules'][level][artifact]['projects'] << artifacts2projects[d]
report['downstream_projects'] << artifacts2projects[d]
report['downstream_projects'] << artifacts2projects[d]
// go recursive
analyzeDependency(modules2deps, artifacts2projects, report, d, nextDeep)
analyzeDependency(modules2deps, artifacts2projects, report, d, nextDepth)
}
}
report
@ -107,41 +105,41 @@ def analyzeDependency(modules2deps, artifacts2projects, report, artifact, deep)
// print the final report
def printReport(report) {
def text = ''
def indent = '\t'
GString text = ''
String indent = '\t'
text += "Dependency Report for ${report['project']} (jenkins project)"
text += "\n\n"
text += "|--Project Maven Modules\n"
report['downstream_modules'].each { deep, artifacts ->
artifacts.each { name, data ->
text += "${indent}|--Module: ${name}\n"
text += "${indent*2}|--Dependency Level: ${deep}\n"
text += "${indent*2}|--Used by (Maven Modules)\n"
data['dependencies'].each {d ->
text += "${indent*3}|--${d}"
text += "\n"
}
text += "${indent*2}|--Referred by (Jenkins Projects)\n"
data['projects'].each { p ->
text += "${indent*3}|--${p}"
text += "\n"
}
report['downstream_modules'].each { depth, artifacts ->
artifacts.each { name, data ->
text += "${indent}|--Module: ${name}\n"
text += "${indent * 2}|--Dependency Level: ${depth}\n"
text += "${indent * 2}|--Used by (Maven Modules)\n"
data['dependencies'].each { d ->
text += "${indent * 3}|--${d}"
text += "\n"
}
}
text += "${indent * 2}|--Referred by (Jenkins Projects)\n"
data['projects'].each { p ->
text += "${indent * 3}|--${p}"
text += "\n"
}
}
}
text += "\n\n"
text += "|--All Downstream Projects\n"
report['downstream_projects'].unique().sort()
report['downstream_projects'].each { p ->
text += "${indent}|--${p}"
text += "\n"
text += "${indent}|--${p}"
text += "\n"
}
println text
}
// debug job
def printJob(job) {
println("fullname ${job.fullName}")
println("name ${job.name}")
println("display name ${job.displayName}")
job.getAllJobs().each {j -> println ("dep: ${j.name}")}
println("fullname ${job.fullName}")
println("name ${job.name}")
println("display name ${job.displayName}")
job.getAllJobs().each { j -> println("dep: ${j.name}") }
}