Version 13.1 by Vincent Massol on 2011/10/10 21:30
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}>>http://github.com/${committer}]]|${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}>>http://github.com/${committer}]]|${committer}|${user.name}|${user.company}|${projects.join(", ")}"
}
{{/groovy}}
{{/cache}}
Result:
List last commits on a project/branch
For example to list latest commits for the XWiki organization on the master branch of the xwiki-commons project. Note that we also add a link when recognizing a JIRA URL
{{groovy}}
import groovy.json.*
println("|=Author|=Date|=Commit Message|=Details")
def commitsRoot = new JsonSlurper().parseText("http://github.com/api/v2/json/commits/list/xwiki/xwiki-commons/master".toURL().text)
commitsRoot.commits.each() { commit ->
def message = (commit.message =~ /([A-Z]+-\d+)/).replaceAll('[[$1>>http://jira.xwiki.org/browse/$1]]')
println "|${commit.author.name} (${commit.author.login})|${commit.committed_date}|((("
println message
println ")))|[[View>>http://github.com${commit.url}]]"
}
{{/groovy}}
import groovy.json.*
println("|=Author|=Date|=Commit Message|=Details")
def commitsRoot = new JsonSlurper().parseText("http://github.com/api/v2/json/commits/list/xwiki/xwiki-commons/master".toURL().text)
commitsRoot.commits.each() { commit ->
def message = (commit.message =~ /([A-Z]+-\d+)/).replaceAll('[[$1>>http://jira.xwiki.org/browse/$1]]')
println "|${commit.author.name} (${commit.author.login})|${commit.committed_date}|((("
println message
println ")))|[[View>>http://github.com${commit.url}]]"
}
{{/groovy}}
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!