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...

Example Groovy Scripts and Snippets

Codebeamer Session

The starting point for CB groovy processing is a session. A session object is available for all resources you obtain from a server.

Connecting to a Codebeamer 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"
  1. import the cbscript package library
  2. connect and obtain a session reference
  3. access the session fields

There is a more descriptive way to specify parameter:

def jf = CB.login(
    url:     "http://10.177.19.63:8080",
    login:   "bond",
    password:"007")

Closing a session

After all the "scripting work" a session needs to be closed.

jf.logout()

Accessing Session Information

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"

Managing Projects

From a session object (e.g jf) a reference to a list of all projects can obtained. Since this is a collection, a closure makes it easy to iterate and find/select/filter one or more projects. The session object has the projects property.

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"}

Managing Tracker

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 
}

Find Issues that are not Closed

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
}

Submitting a new task

You can submit a new Task easily:

bugTracker.submit(summary:"My Bug", text:"This is a new Bug", priority:NORMAL)

Find all the New issues submitted today

bugTracker.issues.findAll{
   today(it.submittedAt) && it.status == 'New'
}.each{
  println it.name
}

Find all issues assigned to me and submitted last week

bugTracker.issues.findAll{
   it.assignedTo == jf.user && it.status == 'New'
}.each{
  println it.name
}

Find a tracker issue that has the string 'big' in summary

bugTracker.issues.findAll{
   it.summary.contains('big')
}.each{
  println it.name
}

All 'Resolved' by user 'bond' this month

bugTracker.issues.findAll{
   it.status == "Resolved" &&
   it.assignedTo.name == "bond" &&
   thisMonth(it.submittedAt)
}.each{
   println it.name
}

All tracker issues submitted by me

bugTracker.issues.findAll{
   return item.submitter == jf.user
}.each{
   println it.name
}

All submitted from 'bond'

bugTracker.issues.findAll{
   it.submitter == "bond"
}.each{
   println it.name
}

High priority tracker issues

bugTracker.issues.findAll{
  it.priority == 1
}.each{
  println it.name
}

Forum management

Post a new message to a forum

def newsForum= jf.projects.find{it.name == "oms"}.forums.find {it.name == "News"}
newsForum.post(subject:"New message", text:"Content here ...")

Check for new post today

newsForum.findAll{
   today(it.submittedAt)
}.each{
  print it.text
}

Search 'big' in summary

newsForum.findAll{
   it.subject.contains('Re:')
}.each{
  print it.text
}

List All posts submitted by me

 
newsForum.findAll{
  it.submitter == jf.user
}.each{
  print it.text
}

List All posts from 'bond'

newsForum.findAll{
  it.submitter.login == "bond"
}.each{
  print it.text
}

Submitted this week by me

newsForum.findAll{
  thisWeek(it.submittedAt) && it.submitter == jf.user
}.each{
  print it.text
}

Documents

You can access documents in a CB project's filesystem and copy them to your local filesystem.
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

Integrated Automation

Any closed items, do we need to build?

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} "
    }

Analysis of projects on different CB server

Lets suppose you have to manage CB server, one private for your corporation behind a firewall, and there is a public part of the project on javaforge.com.

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
}

More Integration: SCRUM Support

There are some CB Groovy examples within the context of Scrum.

Supporting the Sprint review Meeting

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}"
}