Skip to content

Instantly share code, notes, and snippets.

@olivergondza
Created September 8, 2017 08:15
Show Gist options
  • Save olivergondza/ffb54b9660584aba578a6d9fc6b88f83 to your computer and use it in GitHub Desktop.
Save olivergondza/ffb54b9660584aba578a6d9fc6b88f83 to your computer and use it in GitHub Desktop.
Get recent jobs with SCM link
import hudson.scm.*
def MAX_AGE = 1000 * 60 * 60 * 24 * 7
def notOlderThan = new Date(System.currentTimeMillis() - MAX_AGE)
Jenkins.instance.getAllItems(hudson.model.Job.class).grep { it instanceof TopLevelItem }.each {
lb = it.lastBuild
if (lb == null) return // not build
if (notOlderThan.after(lb.time)) return // too old
scms = harvestSCMs(lb.parent)
urls = scms.collect { harvestUrls(it) }.flatten()
if (urls.empty) return // no scm
println it.fullName
println "\tstarted=${lb.time.time}"
urls.unique().each { println "\turl=${it}" }
}
Collection<SCM> harvestSCMs(Job j) {
if (j.getClass().simpleName == "WorkflowJob") {
return j.SCMs
} else {
return [j.scm]
}
}
List<String> harvestUrls(SCM scm) {
if (scm instanceof hudson.scm.NullSCM) return [] as List;
if (scm.getClass().simpleName == "MultiSCM") {
return scm.configuredSCMs.collect { harvestUrls(it) }.flatten()
}
if (scm.getClass().simpleName == "GitSCM") {
return scm.userRemoteConfigs.collect { it.url }
}
if (scm.getClass().simpleName == "SubversionSCM") {
return scm.locations.collect { it.remote }
}
println ("WARNING:Unsupported SCM " + scm);
return [] as List
}
return null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment