Skip to content

Instantly share code, notes, and snippets.

@bdkosher
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save bdkosher/25c31bffd3cd52cf823c to your computer and use it in GitHub Desktop.

Select an option

Save bdkosher/25c31bffd3cd52cf823c to your computer and use it in GitHub Desktop.
Sonar library scraper
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2')
def url = 'https://example.com/sonar/dependencies?resource=commons-io%3Acommons-io&search=commons-io%3Acommons-io&version=1.1'
def parser = new XmlSlurper(new org.ccil.cowan.tagsoup.Parser())
new URL(url).withReader { page ->
def html = parser.parse(page)
html.'**'.find { it.@id == 'results_col' }.'**'.findAll { it.name() == 'td' }.each { td ->
println td.span.text()
}
}
import groovy.json.*
import groovy.transform.*
def url = new URL('https://example.com/sonar/api/projects/index?libs=true&format=json&desc=false&subprojects=true&versions=true')
@ToString
class Lib {
String id
String name
List<String> versions = []
}
def libs = new JsonSlurper().parse(url).findAll { it.qu == 'LIB' && !it.nm.startsWith('com.example') } .collect { prj ->
def v = prj.v ? ((prj.v as Map).keySet() as List) : ['unknown']
new Lib(id: prj.k, name: prj.nm, versions: v)
}
def csv = new File(/C:\Users\jwolf2\Desktop\libs2.csv/)
csv << 'Name,Group ID,Artifact ID,Version\n'
libs.each { lib ->
lib?.versions.each { version ->
def (groupId, artifactId) = lib.id.split(':')
csv << "$lib.name,$groupId,$artifactId,$version\n"
}
}
println "${libs.size()} libraries in total."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment