Class: Lita::Handlers::GithubPR

Inherits:
Handler
  • Object
show all
Includes:
LitaGithub::Config, LitaGithub::Filters, LitaGithub::Octo, LitaGithub::Org, LitaGithub::R, LitaGithub::Repo
Defined in:
lib/lita/handlers/github_pr.rb

Overview

Handler class for GitHub PR management

Constant Summary

Constants included from LitaGithub::Repo

LitaGithub::Repo::PR_LIST_MAX_COUNT

Constants included from LitaGithub::R

LitaGithub::R::REPO_NAME_REGEX, LitaGithub::R::REPO_REGEX

Instance Method Summary collapse

Methods included from LitaGithub::Filters

#func_disabled?

Methods included from LitaGithub::Repo

#repo?, #repo_has_team?, #repo_match, #rpo

Methods included from LitaGithub::Org

#organization, #sort_by_name, #team?, #team_id, #team_id_by_slug

Methods included from LitaGithub::Octo

#access_token, #octo, #setup_octo

Methods included from LitaGithub::Config

#config

Instance Method Details

#pr_info(response) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/lita/handlers/github_pr.rb', line 67

def pr_info(response)
  org, repo, pr = pr_match(response.match_data)
  full_name = rpo(org, repo)

  pr_h = pull_request(full_name, pr)
  return response.reply(t('not_found', pr: pr, org: org, repo: repo)) if pr_h[:fail] && pr_h[:not_found]

  info = build_pr_info(pr_h[:pr], full_name)
  comparison_url = "https://github.com/#{full_name}/compare/#{info[:base_sha]}...#{info[:pr_sha]}"
  info.merge!(repo: full_name, compare: comparison_url)

  reply = t('pr_info.header', info) << t('pr_info.status', info)
  reply << (info[:state] == :merged ? t('pr_info.merged', info) : t('pr_info.mergeable', info))
  reply << t('pr_info.commit_info', info) << t('pr_info.comments', info)

  response.reply(reply)
end

#pr_list(response) ⇒ Object

rubocop:enable Metrics/CyclomaticComplexity rubocop:enable Metrics/PerceivedComplexity


111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/lita/handlers/github_pr.rb', line 111

def pr_list(response)
  org, repo = repo_match(response.match_data)
  full_name = rpo(org, repo)
  reply = ''

  prs = octo.pull_requests(full_name)

  if prs.length > LitaGithub::Repo::PR_LIST_MAX_COUNT
    reply = t('pr_list.large_list', max: LitaGithub::Repo::PR_LIST_MAX_COUNT)

    prs.slice(0, 10).each { |pr| reply << list_line(pr, full_name) }

    reply << "----\n"

    prs.slice(-10, 10).each { |pr| reply << list_line(pr, full_name) }
  elsif prs.length > 0
    prs.each { |pr| reply << list_line(pr, full_name) }
  else
    reply = t('pr_list.no_prs')
  end

  response.reply(reply)
end

#pr_merge(response) ⇒ Object


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/lita/handlers/github_pr.rb', line 85

def pr_merge(response)
  return response.reply(t('method_disabled')) if func_disabled?(__method__)
  org, repo, pr = pr_match(response.match_data)
  fullname = rpo(org, repo)

  pr_h = pull_request(fullname, pr)
  return response.reply(t('not_found', pr: pr, org: org, repo: repo)) if pr_h[:fail] && pr_h[:not_found]

  branch = pr_h[:pr][:head][:ref]
  title = pr_h[:pr][:title]
  commit = "Merge pull request ##{pr} from #{org}/#{branch}\n\n#{title}"

  status = merge_pr(org, repo, pr, commit)

  if !defined?(status) || status.nil?
    response.reply(t('exception'))
  elsif status[:merged]
    response.reply(t('pr_merge.pass', pr: pr, org: org, branch: branch, title: title))
  else
    url = "https://github.com/#{org}/#{repo}/pull/#{pr}"
    response.reply(t('pr_merge.fail', pr: pr, title: title, url: url, msg: status[:message]))
  end
end