Version 8.2 by Vincent Massol on 2011/10/08 22:20
Uses REST and Groovy to integrate with GitHub |
Type | Snippet |
Category | |
Developed by | Unknown |
Rating | |
License | Unknown |
Table of contents
Description
Shows how to integrate with GitHub using its REST API, using Groovy to perform the REST calls.
Examples
List all Git repositories for an organization
{{cache}}
{{groovy}}
import groovy.json.*
def url = "http://github.com/api/v2/json/repos/show/xwiki".toURL().text
def root = new JsonSlurper().parseText(url)
println "|=Project|=Description|=Use Wiki?|=Use Issues?"
root.repositories.each() { repo ->
println "|[[${repo.name}>>http://github.com/xwiki/${repo.name}]]|${repo.description}|${repo.has_wiki}|${repo.has_issues}"
}
{{/groovy}}
{{/cache}}
{{groovy}}
import groovy.json.*
def url = "http://github.com/api/v2/json/repos/show/xwiki".toURL().text
def root = new JsonSlurper().parseText(url)
println "|=Project|=Description|=Use Wiki?|=Use Issues?"
root.repositories.each() { repo ->
println "|[[${repo.name}>>http://github.com/xwiki/${repo.name}]]|${repo.description}|${repo.has_wiki}|${repo.has_issues}"
}
{{/groovy}}
{{/cache}}
Note: If you wish you could also use Groovy's HttpBuilder (although it's more complex but the example shows how to use the Grape's @Grab annotation):
{{cache}}
{{groovy}}
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.1')
import groovyx.net.http.RESTClient
github = new RESTClient( 'http://github.com/api/v2/json/' )
println "|=Project|=Description|=Use Wiki?|=Use Issues?"
def response = github.get( path : 'repos/show/xwiki' )
response.data.repositories.each() { repo ->
println "|[[${repo.name}>>http://github.com/xwiki/${repo.name}]]|${repo.description}|${repo.has_wiki}|${repo.has_issues}"
}
{{/groovy}}
{{/cache}}
{{groovy}}
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.5.1')
import groovyx.net.http.RESTClient
github = new RESTClient( 'http://github.com/api/v2/json/' )
println "|=Project|=Description|=Use Wiki?|=Use Issues?"
def response = github.get( path : 'repos/show/xwiki' )
response.data.repositories.each() { repo ->
println "|[[${repo.name}>>http://github.com/xwiki/${repo.name}]]|${repo.description}|${repo.has_wiki}|${repo.has_issues}"
}
{{/groovy}}
{{/cache}}
Result:
List all committers for an organization
{{cache}}
{{groovy}}
import groovy.json.*
def committers = [:]
// Find all repositories for the "xwiki" organization
def reposRoot = new JsonSlurper().parseText("http://github.com/api/v2/json/repos/show/xwiki".toURL().text)
reposRoot.repositories.each() { repo ->
// For each repository get the collaborators
def collaboratorsRoot = new JsonSlurper().parseText("http://github.com/api/v2/json/repos/show/xwiki/${repo.name}/collaborators".toURL().text)
collaboratorsRoot.collaborators.each() { collaborator ->
def projects = committers.get(collaborator)
if (projects == null) {
projects = []
committers.put(collaborator, projects)
}
projects.add("[[${repo.name}>>http://github.com/xwiki/${repo.name}]]")
}
}
println("|=Avatar|=Id|=Name|=Company|=Top level modules")
committers.sort().each() { committer, projects ->
// Get information about committer
def userRoot = new JsonSlurper().parseText("http://github.com/api/v2/json/user/show/${committer}".toURL().text)
def user = userRoot.user
println "|[[image:http://www.gravatar.com/avatar/${user. gravatar_id}]]|${committer}|${user.name}|${user.company}|${projects.join(", ")}"
}
{{/groovy}}
{{/cache}}
{{groovy}}
import groovy.json.*
def committers = [:]
// Find all repositories for the "xwiki" organization
def reposRoot = new JsonSlurper().parseText("http://github.com/api/v2/json/repos/show/xwiki".toURL().text)
reposRoot.repositories.each() { repo ->
// For each repository get the collaborators
def collaboratorsRoot = new JsonSlurper().parseText("http://github.com/api/v2/json/repos/show/xwiki/${repo.name}/collaborators".toURL().text)
collaboratorsRoot.collaborators.each() { collaborator ->
def projects = committers.get(collaborator)
if (projects == null) {
projects = []
committers.put(collaborator, projects)
}
projects.add("[[${repo.name}>>http://github.com/xwiki/${repo.name}]]")
}
}
println("|=Avatar|=Id|=Name|=Company|=Top level modules")
committers.sort().each() { committer, projects ->
// Get information about committer
def userRoot = new JsonSlurper().parseText("http://github.com/api/v2/json/user/show/${committer}".toURL().text)
def user = userRoot.user
println "|[[image:http://www.gravatar.com/avatar/${user. gravatar_id}]]|${committer}|${user.name}|${user.company}|${projects.join(", ")}"
}
{{/groovy}}
{{/cache}}
Result:
Prerequisites & Installation Instructions
- If you're using an XWiki version lower than 3.2:
- Download the Ivy JAR and copy it in your WEB-INF/lib directory. This is needed by the Groovy @Grab directive.
- If you're using XWiki 3.2+ then you're good to go!