class Ghlabel::Ghlabel

Public Class Methods

new(token:, repo: nil, organization: nil, with_references: true, pr_number: nil) click to toggle source
# File lib/ghlabel.rb, line 7
def initialize(token:, repo: nil, organization: nil, with_references: true, pr_number: nil)
  @token = token
  @user = github.users.get.login

  @organization = organization || current_repo_info[:organization] 
  @repo = repo || current_repo_info[:repo]
  @with_references = with_references

  @pr = pr_number ? pr_from_num(pr_number) : pr_from_ref(File.read('.git/HEAD').gsub('ref: ', '').strip)
end

Public Instance Methods

add_labels(labels) click to toggle source
# File lib/ghlabel.rb, line 27
def add_labels(labels)
  related_issues = if @with_references
    @pr[:title].scan(/\#(\d+)/).flatten
  end || []
  ([@pr[:issue_number]] + related_issues).map do |issue_number|
    github.issues.edit(user: @organization, repo: @repo, number: issue_number, labels: current_labels(issue_number) + labels)
  end
end
remove_labels(labels) click to toggle source
# File lib/ghlabel.rb, line 18
def remove_labels(labels)
  related_issues = if @with_references
    @pr[:title].scan(/\#(\d+)/).flatten
  end || []
  ([@pr[:issue_number]] + related_issues).map do |issue_number|
    github.issues.edit(user: @organization, repo: @repo, number: issue_number, labels: current_labels(issue_number) - labels)
  end
end

Private Instance Methods

current_labels(issue_number) click to toggle source
# File lib/ghlabel.rb, line 56
def current_labels(issue_number)
  pr_issue = github.issues.get(user: @organization, repo: @repo, number: issue_number)
  pr_issue.labels.map(&:name)
end
current_repo_info() click to toggle source
# File lib/ghlabel.rb, line 61
def current_repo_info
  organization, repo = (/url = git@github.com:(.*)\/(.*).git/.match(File.read('.git/config')).captures)
  {organization: organization, repo: repo}
end
github() click to toggle source
# File lib/ghlabel.rb, line 70
def github
  @_github ||= Github.new oauth_token: @token
end
organizations() click to toggle source
# File lib/ghlabel.rb, line 66
def organizations
  @_orgz ||= github.organizations.list.map{|x| x.login}
end
pr_from_num(pr_number) click to toggle source
# File lib/ghlabel.rb, line 38
def pr_from_num(pr_number)
  pr = github.pull_requests.get(user: @organization, repo: @repo, number: pr_number)
  {id: pr.id, ref: pr.head.ref, title: pr.title, created_at: pr.created_at, merged_at: pr.merged_at,
   href: pr['_links']['self']['href'], issue_number: /\/(\d+)$/.match(pr['_links']['issue']['href']).captures.first}
end
pr_from_ref(ref) click to toggle source
# File lib/ghlabel.rb, line 44
def pr_from_ref(ref)
  clean_ref = ref.gsub('refs/heads/', '')
  pr = github.pull_requests.list(user: @organization, repo: @repo, state: 'all').each_page do |page|
    if pr = page.find{|x| x.head.ref == clean_ref}
      break pr
    end
  end
  fail "Can't find your pr" unless pr
  {id: pr.id, ref: pr.head.ref, title: pr.title, created_at: pr.created_at, merged_at: pr.merged_at,
   href: pr['_links']['self']['href'], issue_number: /\/(\d+)$/.match(pr['_links']['issue']['href']).captures.first }
end