![]() |
Collaborative ALM as a Service Free hosting service for Open Source projects on Amazon EC2 |
|
Example Automation using Groovy |
|
Tags: No tags associated yet. |
|
All Tags... |
The starting point for CB groovy processing is a session. A session object is available for all resources you obtain from a server.
When connecting to a CB server, you have to provide a URL and your credentials for authentication. You will have then the same set of resources available to you as you would have connected using the Web interface.
import cbscript.* // 1. // connect and create a new session def jf= CB.login("http://10.177.19.63:8080", "bond", "007") // 2. assert jf.login == "bond" // 3. assert jf.url == "http://10.177.19.63:8080"
There is a more descriptive way to specify parameter:
def jf = CB.login( url: "http://10.177.19.63:8080", login: "bond", password:"007")
After all the "scripting work" a session needs to be closed.
jf.logout()
Some read-only meta information can be obtained about the session.
def jf = CB.login("http://localhost:8080", "bond", "007") assert jf.login == "bond" assert jf.url == "http://localhost:8080" assert jf.user.realName == "Default System Administrator"
The code above prints for each project its Id and name.
jf.projects.each { println " id: ${it.id} name : ${it.name}" }
This fragment finds the project with the name "Moose" and prints its id
assert jf.projects.find{it.name == "Moose"}.id == 2374
Now, print out the name of the project with the id 2374
assert jf.projects.find{it.id == 2374}.name == "Moose"
Find all the projects that are created by a user bond and print out the project name. The findAll call creates a sub-list, that later is being processed with each
jf.projects.findAll{ it.createdBy.name == "bond" }.each{ println it.name }
You can also obtain a reference to a project for later use. The project object will be used in the following examples.
def project = jf.projects.find{it.name == "Moose"}
From a project you can get to the lists of trackers, builds, forums, and documents.
The code below prints the name of all the tracker on the project oms
jf.projects.find{it.name == "oms"}.trackers.each{ print it.name }
Obtain the Bug tracker in the oms project. Then find all the Closed tracker issues.
def bugTracker = jf.projects.find{it.name == "oms"}.trackers.find {it.name == "Bugs"} bugTracker.issues.findAll{ it.status != 'Closed' }.each{ println it.name }
You can submit a new Task easily:
bugTracker.submit(summary:"My Bug", text:"This is a new Bug", priority:NORMAL)
bugTracker.issues.findAll{ today(it.submittedAt) && it.status == 'New' }.each{ println it.name }
bugTracker.issues.findAll{ it.assignedTo == jf.user && it.status == 'New' }.each{ println it.name }
bugTracker.issues.findAll{ it.summary.contains('big') }.each{ println it.name }
bugTracker.issues.findAll{ it.status == "Resolved" && it.assignedTo.name == "bond" && thisMonth(it.submittedAt) }.each{ println it.name }
bugTracker.issues.findAll{ return item.submitter == jf.user }.each{ println it.name }
bugTracker.issues.findAll{ it.submitter == "bond" }.each{ println it.name }
bugTracker.issues.findAll{ it.priority == 1 }.each{ println it.name }
def newsForum= jf.projects.find{it.name == "oms"}.forums.find {it.name == "News"} newsForum.post(subject:"New message", text:"Content here ...")
newsForum.findAll{ today(it.submittedAt) }.each{ print it.text }
newsForum.findAll{ it.subject.contains('Re:') }.each{ print it.text }
newsForum.findAll{ it.submitter == jf.user }.each{ print it.text }
newsForum.findAll{ it.submitter.login == "bond" }.each{ print it.text }
newsForum.findAll{ thisWeek(it.submittedAt) && it.submitter == jf.user }.each{ print it.text }
def doc = jf.projects.find{it.name == "cbtest"}.artifact("download/EC2TW.txt") assert doc.bytes.size() == 709 assert doc.name == EC2TW.txt assert doc.directory == false
Note that there is no trailing '/' needed to indicate the root of the file system
Copying a file is easy:
new File('c:/tmp/test').withOutputStream { it.write doc.bytes }or...
new File('c:/tmp/test1').withOutputStream { it.write prj.artifact("download/EC2TW.txt").bytes
The following fragment checks for closed tracker items. If so, a build process will be invoked
needsBuild = project.trackers.find {it.name == "Task"}.items.any { it.status == "Closed" } println " Do we need to build - ${needsBuild}" if (needsBuild) { println "starting build ...." log = project.builds.find{it.name == "Project Statistic"}.invoke() println " Build done: ${log.status} " println " Output: ${log.stdOut} " }
You would like to count the hours that "Joe Coder" spent so far on fixing bugs in two project trackers.
Note: the tracker item lists are merged before they are queried.
// connect to JF and the intranet server def intra= CB.connect("http://10.177.19.63:8080", "bond", "007") def jf= CB.connect("http://javaforge.com/remote-api", "me", "pword") // get both tracker def tracker_jf = jf.projects.find{it.name = "GeoFacade"}.trackers.find{it.name="Bugs"} def tracker_intra = intra.projects.find{it.name = "GeoFacade_I"}.trackers.find{it.name="Bugs"} // put them together and thread them as one!!!!!!!!!!!! def tracker = tracker_jf + tracker_intra double total = 0 tracker.issues.findAll{ it.assignedTo.user.realName == "Joe Coder" }.each { total += it.spentHours }
There are some CB Groovy examples within the context of Scrum.
Check which tasks in 'Feature' requests in the 'oms' project were finished over the last 30 days.
jf.projects.find{it.name == "oms"}.trackers.find{it.name == "Features"}.issues.findAll { it.status == "Closed" && withinDays(it.modifiedAt, 30) }.each { println " ${it.summary} - ${it.description}" }
Find all the bugs that are still not closed and were submitted since April 1st 2008
jf.projects.find{it.name == "oms"}.trackers.find {it.name == "Bugs"}.issues.findAll{ it.status != "Closed" && it.submittedAt.before(new Date(2008, 1, 4)) }.each { println " ${it.summary} - ${it.description}" }