branch_creation.rb 878 Bytes
Newer Older
John Jarvis's avatar
John Jarvis committed
1 2 3 4
# frozen_string_literal: true

module ReleaseTools
  module BranchCreation
5
    def gitlab_client
John Jarvis's avatar
John Jarvis committed
6 7 8
      ReleaseTools::GitlabClient
    end

9 10 11 12
    def gitlab_ops_client
      ReleaseTools::GitlabOpsClient
    end

13
    def ignoring_duplicates
John Jarvis's avatar
John Jarvis committed
14 15 16 17 18 19 20 21 22
      yield
    rescue Gitlab::Error::Conflict, Gitlab::Error::BadRequest => ex
      if ex.message.match?('already exists')
        # no-op for idempotency
      else
        raise
      end
    end

23
    def create_branch_from_ref(project, branch, ref, client = gitlab_client)
John Jarvis's avatar
John Jarvis committed
24 25 26 27 28 29 30 31
      $stdout.puts "Creating `#{branch}` from `#{ref}` on `#{project.path}`"

      return if dry_run?

      ignoring_duplicates do
        client.create_branch(branch, ref, project)
      end
    end
32 33

    def latest_successful_ref(project, client = gitlab_client)
34
      ReleaseTools::Commits.new(project, client: client).latest_successful.id
35
    end
John Jarvis's avatar
John Jarvis committed
36 37
  end
end