Skip to content

Instantly share code, notes, and snippets.

@scottsbaldwin
Created February 3, 2012 17:33
Show Gist options
  • Save scottsbaldwin/1731305 to your computer and use it in GitHub Desktop.
Save scottsbaldwin/1731305 to your computer and use it in GitHub Desktop.
Get build cause for Go pipeline (including material changes)
require 'rexml/document'
require 'open-uri'
include REXML
user = "YOUR_USER_NAME"
password = "YOUR_PASSWORD"
pipeline_name = "YOUR_PIPELINE_NAME"
def get_xml_from_url(url, auth=[])
remote_file = open(url, :http_basic_authentication => auth)
doc = Document.new(remote_file)
doc
end
def create_stages_url(pipeline)
host = "pipeline"
return "http://#{host}:8153/go/api/pipelines/#{pipeline}/stages.xml"
end
# Get an XML document for the stages.xml feed
stages_url = create_stages_url pipeline_name
stages_doc = get_xml_from_url stages_url, [user, password]
# Using the stages.xml feed, find the first entry
# and then find the related link for pipeline details
first_entry = XPath.first(stages_doc, "//entry")
link_title = "#{pipeline_name} Pipeline Detail"
link_type = "application/vnd.go+xml"
pipeline_link_node = XPath.first(first_entry, "//link[@title='#{link_title}' and @type='#{link_type}']")
pipeline_link_url = pipeline_link_node.attributes["href"]
# Get an XML document for the pipeline detail
pipeline_doc = get_xml_from_url pipeline_link_url, [user, password]
# With the pipeline detail feed, first look up the approved by name
approved_by_node = XPath.first(pipeline_doc, "//approvedBy")
approved_by = approved_by_node.get_text.to_s
# Grab some other details about the pipeline
schedule_time_node = XPath.first(pipeline_doc, "//scheduleTime")
schedule_time = Time.parse schedule_time_node.get_text.to_s
pipeline_label_node = XPath.first(pipeline_doc, "/pipeline")
pipeline_label = pipeline_label_node.attributes["label"]
# Dump what we found about the pipeline
puts "Pipeline: #{pipeline_name}"
puts "Label: #{pipeline_label}"
puts "Build Cause: #{approved_by}"
puts "Schedule Time: #{schedule_time}"
# If the trigger for the pipeline was a material change,
# find out who made the changes
if approved_by == "changes"
# Loop over each of the materials
XPath.each(pipeline_doc, "//material") do |material|
puts
puts "Material: #{material.attributes['url']}"
# For each material, look at each changeset
XPath.each(material, "//changeset") do |changeset|
# Dump what we found for the change set
puts "\tRevision: #{XPath.first(changeset, "revision").get_text.to_s}"
puts "\tUser: #{XPath.first(changeset, "user").get_text.to_s}"
puts "\tMessage: #{XPath.first(changeset, "message").get_text.to_s}"
puts "\tCheckin Time: #{Time.parse XPath.first(changeset, "checkinTime").get_text.to_s}"
puts
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment