lita-github 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 83ef88d9ef27a589ab808c94f5acbee5c45890f3
4
- data.tar.gz: 72f83141ec52828b41e684fce7a60371ef0f1270
3
+ metadata.gz: 96790f8b19467db1f1534304ce67a2129c90e9e8
4
+ data.tar.gz: 3b99912b047f352b94457b769967458f7e9b5cd2
5
5
  SHA512:
6
- metadata.gz: 299cbb26470c5d2d99d3eaaf34bf61b5d5de8e88b6a0b701a24e09d02eb492b4e7db75e3642a729a4a6326f75635c4fe0c933a725ed1dfa1ad3c94c0c5ffca65
7
- data.tar.gz: 3ca05325963bcc50abb0f39bb91f54acee4147699b513a3d06d272d4f8ed473b6d4d60292ab6b45877267387239a3c3ccb68b9833b9bba7710eaca892269f394
6
+ metadata.gz: 1f2e72eb7d50a462147e7266ba607727af6442a8c2aec2b6cdd10fa4b944acd942a33d895a9e8b53694a742eac2dad1f7cc9a3246242458bdea497577a67af15
7
+ data.tar.gz: b75bc26eeea1a9863fdda9102f5be53a7b2d719c5f2dc5fc117b66fb4f44cb86bbd3c0eff8fedf3186f8ebe79a37ce48eb11124635ec42fea34ca0f5b55ea4f5
data/README.md CHANGED
@@ -25,9 +25,9 @@ The configuration options will get their own in-depth doc a little further down
25
25
  * `config.handlers.github.access_token = ''`
26
26
  * Your GitHUb access token (generated from Settings > Security > Personal Applications)
27
27
  * This is an administrative utility, so the token will need pretty wide access to leverage this plugin fully
28
- * `config.handlers.default_org = ''`
28
+ * `config.handlers.github.default_org = ''`
29
29
  * Your company may only have one organization, the handler will allow you to type just the repo name (`lita-github`) instead of `PagerDuty/lita-github`.
30
- * `config.handlers.default_team_slug = ''`
30
+ * `config.handlers.github.default_team_slug = ''`
31
31
  * the default team that should be added to a repo based on the slug name:
32
32
  * When clicking on a team in your org you can use the URL to get the slug: https://github.com/orgs/<ORG>/teams/[slug]
33
33
 
@@ -41,7 +41,22 @@ The command support two prefixes, assuming you have the `robot_alias` set to `!`
41
41
 
42
42
  Here is the current functionality:
43
43
 
44
+ ### GitHub Main Handler
45
+
44
46
  * `!gh status`
45
47
  * get the current system status for GitHub
46
48
  * `!gh version`
47
49
  * get the version of handler
50
+
51
+ ### GitHub Repository Handler
52
+ * `!gh repo create PagerDuty/lita-github private:true team:<team_slug>`
53
+ * This creates a new repository, sets it to private, and adds the team you've specified.
54
+ * This method can be disabled by setting `config.handlers.github.repo_create_enabled = false` in your configuration file
55
+ * `!gh repo delete PagerDuty/lita-github`
56
+ * Deletes the repo you specify, requires confirmation before doing so
57
+ * **Note:** This method is disabled by default, you need to enable it by setting `config.handlers.github.repo_delete_enabled = true` in your configuration file
58
+
59
+ ### Github PR Handler
60
+ * `!gh pr merge #42 PagerDuty/lita-github` or `!shipit #42 PagerDuty/lita-github`
61
+ * This merges the specified pull request
62
+ * This method can be disabled by setting `config.handlers.github.pr_merge_enabled = false` in your configuration file
data/lib/lita-github.rb CHANGED
@@ -23,3 +23,4 @@ Lita.load_locales Dir[File.expand_path(
23
23
  require 'lita-github/version'
24
24
  require 'lita/handlers/github'
25
25
  require 'lita/handlers/github_repo'
26
+ require 'lita/handlers/github_pr'
@@ -16,6 +16,6 @@
16
16
 
17
17
  # Administer your Hub of Gits with Lita!
18
18
  module LitaGithub
19
- VERSION = '0.0.2'
19
+ VERSION = '0.0.3'
20
20
  MAJ, MIN, REV = VERSION.split('.').map(&:to_i)
21
21
  end
@@ -50,8 +50,18 @@ module Lita
50
50
  def self.default_config(config)
51
51
  # when setting default configuration values please remember one thing:
52
52
  # secure and safe by default
53
- config.repo_delete_enabled = false
54
53
  config.repo_private_default = true
54
+
55
+ ####
56
+ # Method Filters
57
+ ####
58
+
59
+ # Lita::Handlers::GithubRepo
60
+ config.repo_create_enabled = true
61
+ config.repo_delete_enabled = false
62
+
63
+ # Lita::Handlers::GithubPR
64
+ config.pr_merge_enabled = true
55
65
  end
56
66
 
57
67
  def gh_status(response)
@@ -0,0 +1,96 @@
1
+ # -*- coding: UTF-8 -*-
2
+ #
3
+ # Copyright 2014 PagerDuty, Inc.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'lita-github/r'
18
+ require 'lita-github/config'
19
+ require 'lita-github/octo'
20
+ require 'lita-github/org'
21
+ require 'lita-github/repo'
22
+ require 'lita-github/filters'
23
+
24
+ module Lita
25
+ # Lita handler
26
+ module Handlers
27
+ # Handler class for GitHub PR management
28
+ class GithubPR < Handler
29
+ include LitaGithub::R # Github handler common-use regex constants
30
+ include LitaGithub::Config # Github handler Lita configuration methods
31
+ include LitaGithub::Octo # Github handler common-use Octokit methods
32
+ include LitaGithub::Org # Github handler common-use Organization methods
33
+ include LitaGithub::Repo # Github handler common-use Repository methods
34
+ include LitaGithub::Filters # Github handler common-use method filters
35
+
36
+ on :loaded, :setup_octo # from LitaGithub::Octo
37
+
38
+ route(
39
+ /(?:#{LitaGithub::R::A_REG}(?:pr merge|shipit)|shipit)\s+?#(?<pr>\d+?)\s+?#{LitaGithub::R::REPO_REGEX}/,
40
+ :pr_merge, command: true, confirmation: true,
41
+ help: {
42
+ 'gh shipit #42 PagerDuty/lita-github' => 'ship it!',
43
+ 'gh pr merge #42 PagerDuty/lita-github' => 'ship it!',
44
+ 'shipit #42 PagerDuty/lita-github' => 'ship it!'
45
+ }
46
+ )
47
+
48
+ def pr_merge(response)
49
+ return response.reply(t('method_disabled')) if func_disabled?(__method__)
50
+ org, repo, pr = pr_match(response)
51
+
52
+ begin
53
+ pr_obj = octo.pull_request(rpo(org, repo), pr)
54
+ rescue Octokit::NotFound
55
+ return response.reply(t('not_found', pr: pr, org: org, repo: repo))
56
+ end
57
+
58
+ branch = pr_obj[:head][:ref]
59
+ title = pr_obj[:title]
60
+ commit = "Merge pull request ##{pr} from #{org}/#{branch}\n\n#{title}"
61
+
62
+ status = merge_pr(org, repo, pr, commit)
63
+
64
+ if !defined?(status) || status.nil?
65
+ response.reply(t('exception'))
66
+ elsif status[:merged]
67
+ response.reply(t('pr_merge.pass', pr: pr, org: org, branch: branch, title: title))
68
+ else
69
+ url = "https://github.com/#{org}/#{repo}/pull/#{pr}"
70
+ response.reply(t('pr_merge.fail', pr: pr, title: title, url: url, msg: status[:message]))
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ def pr_match(response)
77
+ md = response.match_data
78
+ [organization(md['org']), md['repo'], md['pr']]
79
+ end
80
+
81
+ def merge_pr(org, repo, pr, commit)
82
+ status = nil
83
+ # rubocop:disable Lint/HandleExceptions
84
+ begin
85
+ status = octo.merge_pull_request(rpo(org, repo), pr, commit)
86
+ rescue StandardError
87
+ # no-op
88
+ end
89
+ # rubocop:enable Lint/HandleExceptions
90
+ status
91
+ end
92
+ end
93
+
94
+ Lita.register_handler(GithubPR)
95
+ end
96
+ end
@@ -64,6 +64,8 @@ module Lita
64
64
  )
65
65
 
66
66
  def repo_create(response)
67
+ return response.reply(t('method_disabled')) if func_disabled?(__method__)
68
+
67
69
  org, repo = repo_match(response)
68
70
 
69
71
  if repo?(rpo(org, repo))
data/locales/en.yml CHANGED
@@ -7,7 +7,7 @@ en:
7
7
  minor: "GitHub is reporting minor issues (status:yellow)! Last message:\n%{created_on} :: %{body}"
8
8
  major: "GitHub is reporting major issues (status:red)! Last message:\n%{created_on} :: %{body}"
9
9
  github_repo:
10
- method_disabled: "Sorry, this function has been disabled in the config"
10
+ method_disabled: "Sorry, this function has either been disabled or not enabled in the config"
11
11
  repo_create:
12
12
  pass: "Created %{org}/%{repo}: %{repo_url}"
13
13
  fail: "Unable to create %{org}/%{repo}"
@@ -19,6 +19,9 @@ en:
19
19
  repo_info:
20
20
  reply: "%{repo} (private:%{private}) :: %{url}\nDesc: %{description}\nIssues: %{issues_count} PRs: %{pr_count}"
21
21
  github_pr:
22
- repo_merge:
22
+ method_disabled: "Sorry, this function has either been disabled or not enabled in the config"
23
+ exception: "An unexpected exception was hit during the GitHub API operation. Please make sure all arguments are proper and try again, or try checking the GitHub status (gh status)"
24
+ not_found: "Pull request #%{pr} on %{org}/%{repo} not found"
25
+ pr_merge:
23
26
  pass: "Merged pull request #%{pr} from %{org}/%{branch}\n%{title}"
24
- fail: "Failed trying to merge PR #%{pr} (%{title}) :: %{url}"
27
+ fail: "Failed trying to merge PR #%{pr} (%{title}) :: %{url}\nMessage: %{msg}"
@@ -0,0 +1,134 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ require 'spec_helper'
4
+
5
+ describe Lita::Handlers::GithubPR, lita_handler: true do
6
+ it { routes_command('gh shipit #42 GrapeDuty/lita-test').to(:pr_merge) }
7
+ it { routes_command('gh pr merge #42 GrapeDuty/lita-test').to(:pr_merge) }
8
+ it { routes_command('shipit #42 GrapeDuty/lita-test').to(:pr_merge) }
9
+
10
+ let(:github_pr) { Lita::Handlers::GithubPR.new('robot') }
11
+ let(:github_org) { 'GrapeDuty' }
12
+ let(:github_repo) { 'lita-test' }
13
+ let(:disabled_reply) { 'Sorry, this function has either been disabled or not enabled in the config' }
14
+
15
+ describe '.pr_match' do
16
+ it 'should return the content of the match data' do
17
+ mock_md = { 'org' => github_org, 'repo' => github_repo, 'pr' => 42 }
18
+ mock_resp = double('Lita::Response', match_data: mock_md)
19
+ expect(github_pr.send(:pr_match, mock_resp)).to eql [github_org, github_repo, 42]
20
+ end
21
+ end
22
+
23
+ describe '.merge_pr' do
24
+ before do
25
+ @merge_status = { sha: 'abc456', merged: true, message: 'Pull Request successfully merged' }
26
+ @octo_obj = double('Octokit::Client', merge_pull_request: @merge_status)
27
+ allow(github_pr).to receive(:octo).and_return(@octo_obj)
28
+ end
29
+
30
+ context 'when all goes well' do
31
+ it 'should return the response from trying to merge' do
32
+ expect(github_pr.send(:merge_pr, github_org, github_repo, '42', 'test commit'))
33
+ .to eql @merge_status
34
+ end
35
+ end
36
+
37
+ context 'when we hit an exception' do
38
+ before do
39
+ @merge_status = { sha: 'abc456', merged: false, message: '*BOOM*' }
40
+ @octo_obj = double('Octokit::Client')
41
+ allow(@octo_obj).to receive(:merge_pull_request).and_raise(StandardError.new)
42
+ allow(github_pr).to receive(:octo).and_return(@octo_obj)
43
+ end
44
+
45
+ it 'should return nil' do
46
+ expect(github_pr.send(:merge_pr, github_org, github_repo, 42, 'test commit'))
47
+ .to be_nil
48
+ end
49
+ end
50
+ end
51
+
52
+ describe '.pr_merge' do
53
+ before do
54
+ @cfg_obj = double('Lita::Configuration', pr_merge_enabled: true)
55
+ @pr_obj = { head: { ref: 'fix-some-bugs' }, title: 'fix bug' }
56
+ @merge_status = { sha: 'abc456', merged: true, message: 'Pull Request successfully merged' }
57
+ @octo_obj = double('Octokit::Client', pull_request: @pr_obj)
58
+ allow(github_pr).to receive(:octo).and_return(@octo_obj)
59
+ allow(github_pr).to receive(:func_disabled?).and_return(false)
60
+ allow(github_pr).to receive(:config).and_return(@cfg_obj)
61
+ allow(github_pr).to receive(:merge_pr).and_return(@merge_status)
62
+ end
63
+
64
+ context 'when command disabled' do
65
+ before do
66
+ allow(github_pr).to receive(:func_disabled?).and_return(true)
67
+ end
68
+
69
+ it 'should no-op and say such if the command is disabled' do
70
+ send_command("shipit #42 #{github_org}/#{github_repo}")
71
+ expect(replies.last).to eql disabled_reply
72
+ end
73
+ end
74
+
75
+ context 'when repo not found' do
76
+ before do
77
+ allow(@octo_obj).to receive(:pull_request).and_raise(Octokit::NotFound.new)
78
+ end
79
+
80
+ it 'should reply indicating it was invalid' do
81
+ send_command("shipit #42 #{github_org}/#{github_repo}")
82
+ expect(replies.last).to eql 'Pull request #42 on GrapeDuty/lita-test not found'
83
+ end
84
+ end
85
+
86
+ context 'when merging should succeed' do
87
+ it 'should set the right commit message' do
88
+ expect(github_pr).to receive(:merge_pr).with(
89
+ 'GrapeDuty', 'lita-test', '42', "Merge pull request #42 from GrapeDuty/fix-some-bugs\n\nfix bug"
90
+ )
91
+ send_command('shipit #42 GrapeDuty/lita-test')
92
+ end
93
+
94
+ it 'should confirm merging of PR' do
95
+ send_command("shipit #42 #{github_org}/#{github_repo}")
96
+ expect(replies.last)
97
+ .to eql "Merged pull request #42 from GrapeDuty/fix-some-bugs\nfix bug"
98
+ end
99
+ end
100
+
101
+ context 'when merging bombs' do
102
+ before do
103
+ @merge_status = { sha: 'abc456', merged: false, message: '*BOOM*' }
104
+ allow(github_pr).to receive(:merge_pr).and_return(@merge_status)
105
+ end
106
+
107
+ it 'should confirm the failure' do
108
+ send_command("shipit #42 #{github_org}/#{github_repo}")
109
+ expect(replies.last)
110
+ .to eql(
111
+ "Failed trying to merge PR #42 (fix bug) :: https://github.com/GrapeDuty/lita-test/pull/42\n"\
112
+ 'Message: *BOOM*'
113
+ )
114
+ end
115
+ end
116
+
117
+ context 'when the API request explodes' do
118
+ before do
119
+ @merge_status = { sha: 'abc456', merged: false, message: '*BOOM*' }
120
+ @octo_obj = double('Octokit::Client', pull_request: @pr_obj)
121
+ allow(github_pr).to receive(:merge_pr).and_return(nil)
122
+ end
123
+
124
+ it 'should confirm the failure' do
125
+ send_command("shipit #42 #{github_org}/#{github_repo}")
126
+ expect(replies.last)
127
+ .to eql(
128
+ 'An unexpected exception was hit during the GitHub API operation. Please make sure all ' \
129
+ 'arguments are proper and try again, or try checking the GitHub status (gh status)'
130
+ )
131
+ end
132
+ end
133
+ end
134
+ end
@@ -34,7 +34,7 @@ describe Lita::Handlers::GithubRepo, lita_handler: true do
34
34
 
35
35
  let(:github_repo) { Lita::Handlers::GithubRepo.new('robot') }
36
36
  let(:github_org) { 'GrapeDuty' }
37
- let(:disabled_reply) { 'Sorry, this function has been disabled in the config' }
37
+ let(:disabled_reply) { 'Sorry, this function has either been disabled or not enabled in the config' }
38
38
 
39
39
  ####
40
40
  # Helper Methods
@@ -295,11 +295,23 @@ describe Lita::Handlers::GithubRepo, lita_handler: true do
295
295
  describe '.repo_create' do
296
296
  before do
297
297
  @opts = { private: true, team_id: 42, organization: github_org }
298
+ allow(github_repo).to receive(:func_disabled?).and_return(false)
298
299
  allow(github_repo).to receive(:repo?).with("#{github_org}/lita-test").and_return(false)
299
300
  allow(github_repo).to receive(:extrapolate_create_opts).and_return(@opts)
300
301
  allow(github_repo).to receive(:create_repo).and_return('hello from PAX prime!')
301
302
  end
302
303
 
304
+ context 'when command disabled' do
305
+ before do
306
+ allow(github_repo).to receive(:func_disabled?).and_return(true)
307
+ end
308
+
309
+ it 'should no-op and say such if the command is disabled' do
310
+ send_command("gh repo create #{github_org}/lita-test")
311
+ expect(replies.last).to eql disabled_reply
312
+ end
313
+ end
314
+
303
315
  context 'when repo already exists' do
304
316
  before do
305
317
  allow(github_repo).to receive(:repo?).with("#{github_org}/lita-test").and_return(true)
@@ -24,12 +24,20 @@ describe Lita::Handlers::Github, lita_handler: true do
24
24
  it { routes_command('gh version').to(:gh_version) }
25
25
 
26
26
  describe '#default_config' do
27
- it 'should disable repo_delete by default' do
27
+ it 'should set repos to private by default' do
28
+ expect(Lita.config.handlers.github.repo_private_default).to be_truthy
29
+ end
30
+
31
+ it 'should enable Lita::Handlers::GithubRepo.repo_create by default' do
32
+ expect(Lita.config.handlers.github.repo_create_enabled).to be_truthy
33
+ end
34
+
35
+ it 'should disable Lita::Handlers::GithubRepo.repo_delete by default' do
28
36
  expect(Lita.config.handlers.github.repo_delete_enabled).to be_falsey
29
37
  end
30
38
 
31
- it 'should set repos to private by default' do
32
- expect(Lita.config.handlers.github.repo_private_default).to be_truthy
39
+ it 'should enable Lita::Handlers::GithubPR.pr_merge by default' do
40
+ expect(Lita.config.handlers.github.pr_merge_enabled).to be_truthy
33
41
  end
34
42
  end
35
43
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lita-github
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tim Heckman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-28 00:00:00.000000000 Z
11
+ date: 2014-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -166,6 +166,7 @@ files:
166
166
  - lib/lita-github/repo.rb
167
167
  - lib/lita-github/version.rb
168
168
  - lib/lita/handlers/github.rb
169
+ - lib/lita/handlers/github_pr.rb
169
170
  - lib/lita/handlers/github_repo.rb
170
171
  - lita-github.gemspec
171
172
  - locales/en.yml
@@ -177,6 +178,7 @@ files:
177
178
  - spec/unit/lita-github/r_spec.rb
178
179
  - spec/unit/lita-github/repo_spec.rb
179
180
  - spec/unit/lita-github/version_spec.rb
181
+ - spec/unit/lita/handlers/github_pr_spec.rb
180
182
  - spec/unit/lita/handlers/github_repo_spec.rb
181
183
  - spec/unit/lita/handlers/github_spec.rb
182
184
  homepage: https://github.com/PagerDuty/lita-github
@@ -213,5 +215,6 @@ test_files:
213
215
  - spec/unit/lita-github/r_spec.rb
214
216
  - spec/unit/lita-github/repo_spec.rb
215
217
  - spec/unit/lita-github/version_spec.rb
218
+ - spec/unit/lita/handlers/github_pr_spec.rb
216
219
  - spec/unit/lita/handlers/github_repo_spec.rb
217
220
  - spec/unit/lita/handlers/github_spec.rb