Class: HubMerge::Merger

Inherits:
Object
  • Object
show all
Defined in:
lib/hubmerge/merger.rb

Constant Summary collapse

MERGEABILITY_RETRY_COUNT =
5
MERGEABILITY_RETRY_DELAY =
5

Class Method Summary collapse

Class Method Details

.approve_if_not_approved(gh_client, pr) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/hubmerge/merger.rb', line 12

def self.approve_if_not_approved(gh_client, pr)
  repo = repo_from_pr(pr)
  reviews = gh_client.pull_request_reviews(repo, pr.number)
  unless reviews.find { |review| review.state == "APPROVED" }
    gh_client.create_pull_request_review(repo, pr.number, event: "APPROVE")
  end
end

.check_mergeability(gh_client, pr) ⇒ Object

Throws UnmergeableError if not mergeable. Returns true if mergeable.

Yes, this interface is weird. Makes it easier to deal with spinners.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/hubmerge/merger.rb', line 25

def self.check_mergeability(gh_client, pr)
  repo = repo_from_pr(pr)

  with_retries(MERGEABILITY_RETRY_COUNT, MERGEABILITY_RETRY_DELAY) do
    pr = gh_client.pull_request(repo, pr.number)
    if pr.mergeable_state == "clean"
      return true
    elsif pr.mergeable_state == "unknown"
      raise RetryError
    else
      raise UnmergeableError.new(pr.mergeable_state)
    end
  end
end

.merge(gh_client, pr) ⇒ Object



40
41
42
43
# File 'lib/hubmerge/merger.rb', line 40

def self.merge(gh_client, pr)
  repo = repo_from_pr(pr)
  gh_client.merge_pull_request(repo, pr.number)
end

.repo_from_pr(pr) ⇒ Object



45
46
47
48
49
# File 'lib/hubmerge/merger.rb', line 45

def self.repo_from_pr(pr)
  # GitHub doesn't return a full repository name in the response, only way
  # to avoid an extra API call is to parse the `repository_url` string
  pr.repository_url.sub("https://api.github.com/repos/", "")
end

.with_retries(n_tries, delay) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/hubmerge/merger.rb', line 51

def self.with_retries(n_tries, delay)
  n_tries.times do
    return yield
  rescue RetryError
    sleep delay
    next
  end
end