class FlashFlow::Data::Github

Attributes

repo[RW]
unmergeable_label[RW]

Public Class Methods

new(config={}) click to toggle source
# File lib/flash_flow/data/github.rb, line 10
def initialize(config={})
  initialize_connection!(config['token'])
  @repo = config['repo']
  @master_branch = config['master_branch'] || master
  @unmergeable_label = config['unmergeable_label'] || 'unmergeable'
  @do_not_merge_label = config['do_not_merge_label'] || 'do not merge'
  @code_reviewed_label = config['code_reviewed_label'] || 'code reviewed'
  @shippable_label = config['shippable_label'] || 'shippable'
end

Public Instance Methods

add_to_merge(branch) click to toggle source
# File lib/flash_flow/data/github.rb, line 47
def add_to_merge(branch)
  pr = pr_for(branch)

  pr ||= create_pr(branch.ref, branch.ref, branch.ref)
  branch.add_metadata(metadata(pr))

  if pr && @do_not_merge_label
    remove_label(pr.number, @do_not_merge_label)
  end
end
can_ship?(branch) click to toggle source
# File lib/flash_flow/data/github.rb, line 70
def can_ship?(branch)
  has_label?(branch.metadata['pr_number'], @shippable_label)
end
code_reviewed?(branch) click to toggle source
# File lib/flash_flow/data/github.rb, line 66
def code_reviewed?(branch)
  has_label?(branch.metadata['pr_number'], @code_reviewed_label)
end
fetch() click to toggle source
# File lib/flash_flow/data/github.rb, line 36
def fetch
  pull_requests.map do |pr|
    Branch.from_hash(
        'ref' => pr.head.ref,
        'status' => status_from_labels(pr),
        'metadata' => metadata(pr),
        'sha' => pr.head.sha
    )
  end
end
initialize_connection!(token) click to toggle source
# File lib/flash_flow/data/github.rb, line 20
def initialize_connection!(token)
  if token.nil?
    raise RuntimeError.new("Github token must be set in your flash_flow config file.")
  end
  octokit.configure do |c|
    c.access_token = token
  end
end
mark_failure(branch) click to toggle source
# File lib/flash_flow/data/github.rb, line 62
def mark_failure(branch)
  add_label(branch.metadata['pr_number'], @unmergeable_label)
end
mark_success(branch) click to toggle source
# File lib/flash_flow/data/github.rb, line 58
def mark_success(branch)
  remove_label(branch.metadata['pr_number'], @unmergeable_label)
end
remove_from_merge(branch) click to toggle source
# File lib/flash_flow/data/github.rb, line 29
def remove_from_merge(branch)
  pr = pr_for(branch)
  if pr && @do_not_merge_label
    add_label(pr.number, @do_not_merge_label)
  end
end

Private Instance Methods

add_label(pull_request_number, label) click to toggle source
# File lib/flash_flow/data/github.rb, line 111
def add_label(pull_request_number, label)
  unless has_label?(pull_request_number, label)
    octokit.add_labels_to_an_issue(repo, pull_request_number, [label])
  end
end
create_pr(branch, title, body) click to toggle source
# File lib/flash_flow/data/github.rb, line 95
def create_pr(branch, title, body)
  pr = octokit.create_pull_request(repo, @master_branch, branch, title, body)
  pull_requests << pr
  pr
end
has_label?(pull_request_number, label_name) click to toggle source
# File lib/flash_flow/data/github.rb, line 117
def has_label?(pull_request_number, label_name)
  !!labels(pull_request_number).detect { |label| label == label_name }
end
labels(pull_request_number) click to toggle source
# File lib/flash_flow/data/github.rb, line 121
def labels(pull_request_number)
  @labels ||= {}
  @labels[pull_request_number] ||= octokit.labels_for_issue(repo, pull_request_number).map(&:name)
end
metadata(pr) click to toggle source
# File lib/flash_flow/data/github.rb, line 126
def metadata(pr)
  {
      'pr_number' => pr.number,
      'pr_url'    => pr.html_url,
      'user_url'  => pr.user.html_url,
      'repo_url'  => pr.head.repo.html_url
  }
end
octokit() click to toggle source
# File lib/flash_flow/data/github.rb, line 135
def octokit
  Octokit
end
pr_for(branch) click to toggle source
# File lib/flash_flow/data/github.rb, line 91
def pr_for(branch)
  pull_requests.detect { |p| branch.ref == p.head.ref }
end
pull_requests() click to toggle source
# File lib/flash_flow/data/github.rb, line 101
def pull_requests
  @pull_requests ||= octokit.pull_requests(repo).sort_by(&:created_at)
end
remove_label(pull_request_number, label) click to toggle source
# File lib/flash_flow/data/github.rb, line 105
def remove_label(pull_request_number, label)
  if has_label?(pull_request_number, label)
    octokit.remove_label(repo, pull_request_number, label)
  end
end
status_from_labels(pull_request) click to toggle source
# File lib/flash_flow/data/github.rb, line 80
def status_from_labels(pull_request)
  case
    when has_label?(pull_request.number, @do_not_merge_label)
      'removed'
    when has_label?(pull_request.number, @unmergeable_label)
      'fail'
    else
      nil
  end
end