Class: TravisBuildTools::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/travis-build-tools/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(service_user, git_repository = nil) ⇒ Builder

Returns a new instance of Builder.



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/travis-build-tools/builder.rb', line 3

def initialize(service_user, git_repository = nil)
  @service_user = service_user
  origin_url = %x[git config --get remote.origin.url]
  git_repository ||= origin_url.split('://')[1] || origin_url.split('@')[1]
  raise 'git_repository is not specified' if !git_repository
  raise 'service_user is not specified' if !service_user
  
  #Set the service remote
  puts %x[git remote add service https://#{service_user}@#{git_repository}]
  puts %x[git fetch service]
end

Instance Method Details

#merge_downstream(release_branch_name, master_branch_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/travis-build-tools/builder.rb', line 29

def merge_downstream(release_branch_name, master_branch_name)
  if ENV['TRAVIS'] && ENV['TRAVIS_PULL_REQUEST'].match(/false/i)
    #get all branches
    branches = %x[git ls-remote --heads origin].scan(/^.*refs\/heads\/(.*)$/).flatten
    matching_branches = branches.select{|b| b.match(/#{Regexp.quote(release_branch_name)}/)}

    #If this branch doesn't match the downstream then ignore creating the merge
    return if !matching_branches.include?(ENV['TRAVIS_BRANCH'])

    #Sort the branches by the match that comes after the release branch name
    sorted_branches = matching_branches.map{|b| Gem::Version.new(b.match(/#{Regexp.quote(release_branch_name)}(.*)$/)[1].strip)}

    #Find the next branch in the array that isn't the current branch
    current_release_version = Gem::Version.new(ENV['TRAVIS_BRANCH'].match(/#{Regexp.quote(release_branch_name)}(.*)$/)[1].strip)
    next_branch_to_merge = (sorted_branches.drop_while{|b| b <= current_release_version}.map{|v| release_branch_name + v.to_s} + ['master']).first

    #create a merge commit for that branch
    puts %x[git merge --no-ff service/#{next_branch_to_merge} -m"Merge remote-tracking branch '#{ENV['TRAVIS_BRANCH']}'"]
    puts "Merging to downstream branch: #{next_branch_to_merge}"
    #push origin for that branch using the service user
    %x[git push --quiet service HEAD:#{next_branch_to_merge} > /dev/null 2>&1]
  end
end

#publish_git_tag(tag_name) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/travis-build-tools/builder.rb', line 15

def publish_git_tag(tag_name)
  if ENV['TRAVIS'] && ENV['TRAVIS_PULL_REQUEST'].match(/false/i)
    raise 'tag_name is not specified' if !tag_name

    #Setup up deploy
    puts %x[git config --global user.email "[email protected]"]
    puts %x[git config --global user.name "Travis CI"]
    puts %x[git tag #{tag_name} -a -m "Generated tag from TravisCI."]
    puts "Pushing Git tag #{tag_name}."

    %x[git push --tags --quiet service #{tag_name} > /dev/null 2>&1]
  end
end