Module: WTBuildHelpers::TeamCity

Defined in:
lib/teamcity.rb,
lib/teamcity_git_range_fetch.rb

Defined Under Namespace

Classes: Options, OptionsParser

Class Method Summary collapse

Class Method Details

.commandline_fetch_commit_range(args = nil) ⇒ Object



132
133
134
135
136
# 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(teamcity_url, build_type_id, username, password) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/teamcity.rb', line 72

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


128
129
130
# File 'lib/teamcity.rb', line 128

def self.print_help()
    OptionsParser.parse(["--help"])
end

.put_range(options, start_revision, end_revision) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/teamcity.rb', line 57

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