Updating Rally via Ruby

Below is a basic example for querying and updating Rally objects using Rally’s “rally_api” Ruby API.

Changes you will need to make:
– set your Rally username & password
– project name & IDs. Set them to names/IDs that match yours
– defect – use one of your defect numbers

Rally’s “rally_api” can be found here:
Ruby Toolkit for Rally REST API (rally_api)

########################################################################
#
# Example: how to query and update Rally projects and
# defects using Ruby.
#
########################################################################

require 'rally_api'				# used to talk to Rally
require 'active_support/all' 	# used for math calls like 15.minutes ago

# setup config headers
headers = RallyAPI::CustomHttpHeader.new()
headers.name = "Rally Ruby test"
headers.vendor = "Your Company"
headers.version = "1.0"

# setup config info
config = {:base_url => "https://rally1.rallydev.com/slm"}
config[:username]   = "username@yourcompany.com" # enter your Rally username
config[:password]   = "yourpassword"			 # enter your Rally password
config[:workspace]  = "your workspace"
config[:project]    = "your project"
config[:headers]    = headers #from RallyAPI::CustomHttpHeader.new()

# connect to Rally API
@rally = RallyAPI::RallyRestJson.new(config)

# confirm connection by printing out the user
# puts "Connected user: " + @rally.user.to_s()


########################################################################
#
# Find recently updated defects in select projects that we care about.
#
########################################################################

# setup a couple of arrays for the Rally project IDs and project names we care about
projectID_array = Array.new
projectName_array = Array.new


# Yes, I'm using hardcoded project numbers. I figure that a project name may change
# but the project number won't, so I'm intentionally keying off of the number here.

# To find your project number:
# go to https://rally1.rallydev.com/slm/doc/webservice/ and select the "Project"
# link on the left. Find for your project by entering 
# (Name contains "some string in your project's name") in the Query String 
# textbox and click Query button. The response will contain an URL
# with the project number you are looking for. Use that number below.

# Add project numbers that you are interested in searching to the projectID array.
projectID_array << 1234567890	# "acme project"
projectID_array << 2345678901 # "widget project" # continue to add additional project numbers # retrieve project names that we care about and store then into a project name array. # we'll use the project names later when checking for defects we care about. projectID_array.each do |projID| # find the project name corresponding to the project's ID. # If found, fetch the project "Name" projects = @rally.find(RallyAPI::RallyQuery.new({:type => :project, :query_string => "(ObjectID = " + projID.to_s() + ")", :fetch => "Name"}))
	
	# add it to the name array if we got something back from our query
	if projects.total_result_count > 0
		projectName_array << projects.first.Name end end # check for defects updated within the last "N" minutes last_check_time = 15.minutes.ago # outside of Rails, uses "active_support/all" requirement defined at top #puts "\nLast updated: " + last_check_time.in_time_zone("Pacific Time (US & Canada)").to_s() # adjust for your timezone #puts "\nLast updated: " + last_check_time.utc.to_s() # get all defects since last check time. Retrieve the last update date, the defect ID, # the defect name, and it's project. results = @rally.find do |q| q.type = "defect" q.query_string = "(LastUpdateDate > " + last_check_time.utc.strftime("%Y-%m-%dT%H:%M:%S.000Z") + ")"
    q.fetch = "LastUpdateDate,FormattedID,Name,Project"
end


# loop thru all defects. print last update date, ID, and name. Also check 
# if defect is part of a project we care about.
puts "\ntotal_result_count: %d" % results.total_result_count
results.each do |defect|

  # if the defect is in a project that we care about, print YES & defect info
  if projectName_array.include? defect.Project.Name
	puts "YES: " + defect.Project.Name
	puts defect.LastUpdateDate + ": " + defect.FormattedID + " - " + defect.Name + "\n\n"
  # otherwise print NO & defect info
  else
	puts "NO : " + defect.Project.Name
	puts defect.LastUpdateDate + ": " + defect.FormattedID + " - " + defect.Name + "\n\n"
  end
  
end



########################################################################
#
# Find a specific defect and update some information in it.
#
########################################################################

# fetch a specific defect
results = @rally.find(RallyAPI::RallyQuery.new({:type => :defect, :query_string => "(FormattedID = DE1234)", :fetch => "FormattedID,Name,Notes"}))
defect = results.first
defect.read

# print defect info
puts defect.FormattedID + " - " + defect.Name
puts "Original notes: \n" + defect.Notes

# create a timestamp and message to add to the defect's notes
time_now = Time.now.strftime("%Y-%m-%d %H:%M:%S")
msg = (defect.Notes.nil? ? "" : "<br\>") + time_now + ": defect updated."

# create a hash of defect fields to update. then call @rally.update to update it.
fields = {}
fields["Notes"] = defect.Notes + msg
updated_defect = @rally.update(:defect, "FormattedID|" + defect.FormattedID, fields) #by FormattedID

# print updated notes
puts "New notes: \n" + updated_defect.Notes
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments