module WTBuildHelpers::TeamCity

Constants

Options

Public Class Methods

commandline_fetch_commit_range(args = nil) click to toggle source
# File lib/teamcity.rb, line 132
def self.commandline_fetch_commit_range(args = nil)
    options = OptionsParser.parse(args)

    fetch_commit_range(options)
end
fetch_commit_range(options) click to toggle source
# File lib/teamcity.rb, line 105
def self.fetch_commit_range(options)
    branch = `git rev-parse --abbrev-ref HEAD`
    branch.strip!
    
    start_revision = ENV["BUILD_VCS_NUMBER"]
    STDERR.puts "Start revision is #{start_revision}"
    
    begin
        check_branches = [branch, "refs/heads/#{branch}", branch.split("/").last]
        end_revision = nil
        while end_revision == nil && check_branches.length > 0 do
            check_branch = check_branches.shift
            end_revision = get_last_successful_rev(options, check_branch)
        end
        
        put_range(options, start_revision, end_revision)
    rescue RestClient::Exception => e
        puts "ERROR getting last successful build from TeamCity:"
        pp e
        exit 1
    end 
end
get_last_successful_rev(options, branch) click to toggle source
# File lib/teamcity.rb, line 73
def self.get_last_successful_rev(options, branch)
    STDERR.puts "Getting last successful revision from branch #{branch}"
    auth = "#{options.username}:#{options.password}"
    auth_token = Base64.strict_encode64(auth)

    headers = { "Authorization" => "Basic #{auth_token}",
                "Accept" => "application/json" 
              }
    
    end_revision = nil
    escaped_branch = URI.escape(branch)
    
    full_url = "#{options.url}/app/rest/buildTypes/id:#{options.build_type_id}/builds/?locator=status:SUCCESS,branch:name:#{escaped_branch}"
    response = RestClient.get(full_url, headers=headers)
    json_response = JSON.parse(response.body)

    if json_response["build"] && json_response["build"].count > 0
        last_build = json_response["build"].first
        last_build_id = last_build["id"]
        STDERR.puts "Got #{json_response["build"].count} successful builds, last successful was #{last_build_id}"
        

        build_full_url = "#{options.url}/app/rest/builds/id:#{last_build["id"]}"
        build_response = RestClient.get(build_full_url, headers=headers)
        build_json_response = JSON.parse(build_response.body) 

        end_revision = build_json_response["revisions"]["revision"][0]["version"]
    end
    
    return end_revision
end
print_help() click to toggle source
put_range(options, start_revision, end_revision) click to toggle source
# File lib/teamcity.rb, line 58
def self.put_range(options, start_revision, end_revision)
    value = ""
    if end_revision
        value = "#{end_revision}..#{start_revision}"
    else
        value = "#{start_revision}"
    end

    if options.export
        puts value
    else
        puts "##teamcity[setParameter name='env.TEAMCITY_COMMIT_RANGE' value='#{value}']"
    end
end