lita-gerrit 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 03892ea9cf8863f344836f19e638f7ae7a091821
4
+ data.tar.gz: dd388d3257cdbeab80e960b2aace43fb199bba88
5
+ SHA512:
6
+ metadata.gz: bbb6b6115f5e70481ec70b1029fe77c20413232d7d4d2041a70314c4f4b08d5a0224f982f1c043d8cef3965b1076e644c8df518afd00ec290e2ef63a9529bad8
7
+ data.tar.gz: 63501906c688043f9f0e7e9e241b2f26bd031f0ba178779e6e033a64c5d37960c35ff1adc8bfe4c0dbaca7c3d0701e0bd10571f25206b245f7dc14b933e7f88f
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ script: bundle exec rake
5
+ before_install:
6
+ - gem update --system
7
+ services:
8
+ - redis-server
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2013 Jonathan Amiez
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # lita-gerrit
2
+
3
+ [![Build Status](https://travis-ci.org/josqu4red/lita-gerrit.png?branch=master)](https://travis-ci.org/josqu4red/lita-gerrit)
4
+ [![Coverage Status](https://coveralls.io/repos/josqu4red/lita-gerrit/badge.png)](https://coveralls.io/r/josqu4red/lita-gerrit)
5
+
6
+ **lita-gerrit** is a handler for [Lita](https://github.com/jimmycuadra/lita) that allows interaction with Gerrit code review tool.
7
+
8
+ It allows to fetch Gerrit changes details from the chat and listens for hook events on HTTP interface.
9
+
10
+ It depends on HTTParty because Gerrit uses HTTP digest authentication, which is not supported by Lita's built-in HTTP client, Faraday.
11
+
12
+ ## Installation
13
+
14
+ Add lita-gerrit to your Lita instance's Gemfile:
15
+
16
+ ```ruby
17
+ gem "lita-gerrit"
18
+ ```
19
+
20
+ ## Configuration
21
+
22
+ * `url` (String) - Gerrit service URL
23
+ * `username` (String) - Username for REST API
24
+ * `password` (String) - Password for REST API
25
+
26
+ ### Example
27
+
28
+ ```ruby
29
+ Lita.configure do |config|
30
+ config.handlers.gerrit.url = "https://gerrit.example.com"
31
+ config.handlers.gerrit.username = "foo"
32
+ config.handlers.gerrit.password = "bar"
33
+ end
34
+ ```
35
+
36
+ ## Usage
37
+
38
+ ### Chat functions
39
+
40
+ ```
41
+ lita > gerrit 42
42
+ gerrit: Display debug informations with correct log level by John Doe in chef. http://gerrit.example.com/42
43
+ (gerrit: <commit message> by <author> in <project>. <url>)
44
+ ```
45
+
46
+ ### HTTP endpoints
47
+
48
+ lita-gerrit listens for events triggered by Gerrit hooks. An example hook is provided in [contrib](https://github.com/josqu4red/lita-gerrit/tree/master/contrib) directory.
49
+
50
+ Currently only these hooks are implemented:
51
+ * patchset_created
52
+ * comment_added
53
+ * change_merged
54
+
55
+ See list of supported hooks in [Gerrit doc](https://gerrit-review.googlesource.com/Documentation/config-hooks.html)
56
+
57
+ ## License
58
+
59
+ [MIT](http://opensource.org/licenses/MIT)
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #
4
+ ## Lita gerrit handler
5
+ #
6
+ # Usage:
7
+ #
8
+ # Symlink this script to Gerrit hook folder ($GERRIT_HOME/hooks/<hook_name>)
9
+ # Where <hook_name> is one of the list https://gerrit-review.googlesource.com/Documentation/config-hooks.html#_supported_hooks
10
+ #
11
+ # e.g ln -s lita-gerrit-hook $GERRIT_HOME/hooks/patchset-created to enable that hook
12
+ #
13
+
14
+ require "rubygems"
15
+ require "net/http"
16
+ require "getoptlong"
17
+
18
+ lita_url = "http://localhost:8080/gerrit/hooks"
19
+ lita_room = "#my_chan"
20
+
21
+ args = [
22
+ "CRVW", "VRID",
23
+ "abandoner", "author", "branch", "change", "change-url", "changer",
24
+ "cla-id", "comment", "commit", "is-draft", "new-topic", "newrev",
25
+ "old-topic", "oldrev", "patchset", "project", "reason", "refname",
26
+ "restorer", "reviewer", "submitter", "topic", "uploader", "user-id"
27
+ ]
28
+
29
+ opts = GetoptLong.new(*args.map{ |arg| [ "--#{arg}", GetoptLong::REQUIRED_ARGUMENT] })
30
+
31
+ options = {
32
+ "room" => lita_room,
33
+ "action" => File.basename($0)
34
+ }
35
+
36
+ opts.each do |opt, arg|
37
+ key = opt.gsub(/^--/, "")
38
+ options[key] = URI.encode(arg)
39
+ end
40
+
41
+ request = Net::HTTP.post_form(URI.parse(lita_url), options)
@@ -0,0 +1 @@
1
+ require "lita/handlers/gerrit"
@@ -0,0 +1,103 @@
1
+ require "lita"
2
+ require "httparty"
3
+
4
+ module Lita
5
+ module Handlers
6
+ class Gerrit < Handler
7
+
8
+ def self.default_config(config)
9
+ config.url = "https://gerrit.example.com"
10
+ config.username = "foo"
11
+ config.password = "bar"
12
+ config.default_room = nil
13
+ end
14
+
15
+ #
16
+ # Fetch details of a given patchset
17
+ #
18
+
19
+ route /gerrit\s+(\d+)/, :change_details, help: { "gerrit <change #>" => "Displays details of a gerrit change" }
20
+
21
+ def change_details(response)
22
+ change_id = response.matches.flatten.first
23
+ change_uri = URI.join(Lita.config.handlers.gerrit.url, "/a/changes/", change_id)
24
+ change_link = URI.join(Lita.config.handlers.gerrit.url, change_id)
25
+
26
+ http_resp = HTTParty.get(change_uri, :digest_auth => {
27
+ username: Lita.config.handlers.gerrit.username,
28
+ password: Lita.config.handlers.gerrit.password
29
+ })
30
+
31
+ case http_resp.code
32
+ when 200
33
+ change = MultiJson.load(http_resp.body.lines.to_a[1..-1].join)
34
+ message = "gerrit: #{change["subject"]} by #{change["owner"]["name"]} in #{change["project"]}. #{change_link}"
35
+ when 404
36
+ message = "Change ##{change_id} does not exist"
37
+ else
38
+ raise "Failed to fetch #{change_uri} (#{http_resp.code})"
39
+ end
40
+
41
+ response.reply(message)
42
+ rescue Exception => e
43
+ response.reply("Error: #{e.message}")
44
+ end
45
+
46
+ #
47
+ # Notify the creation/comment/merge/etc. of a Gerrit patchset
48
+ #
49
+
50
+ http.post "/gerrit/hooks", :hook
51
+
52
+ def hook(request, response)
53
+ if request.params.has_key?("action")
54
+ action = request.params["action"].gsub("-", "_").to_sym
55
+
56
+ unless respond_to?(action, true)
57
+ raise "Action #{action} is not supported by Gerrit handler"
58
+ end
59
+ else
60
+ raise "Action must be defined in hook's parameters"
61
+ end
62
+
63
+ if request.params.has_key?("room")
64
+ room = request.params["room"]
65
+ elsif Lita.config.handlers.gerrit.default_room
66
+ room = Lita.config.handlers.gerrit.default_room
67
+ else
68
+ raise "Room must be defined. Either fix your hook or specify a default room ('config.handlers.gerrit.default_room')"
69
+ end
70
+
71
+ # build message from action and params
72
+ message = send(action, request.params)
73
+ target = Source.new(room: room)
74
+
75
+ robot.send_message(target, message)
76
+ rescue Exception => e
77
+ Lita.logger.error(e.message)
78
+ end
79
+
80
+ private
81
+
82
+ # List of supported hooks
83
+ # (https://gerrit-review.googlesource.com/Documentation/config-hooks.html#_supported_hooks)
84
+
85
+ def patchset_created(params)
86
+ message = "gerrit: patchset %s has been uploaded by %s in %s. %s"
87
+ message % [params["patchset"], params["uploader"], params["project"], params["change-url"]]
88
+ end
89
+
90
+ def comment_added(params)
91
+ message = "gerrit(%s): %s commented %s (V:%s/CR:%s)"
92
+ message % [params["project"], params["author"], params["change-url"], params["verified"], params["reviewed"]]
93
+ end
94
+
95
+ def change_merged(params)
96
+ message = "gerrit: Merge of %s by %s in %s"
97
+ message % [params["change-url"], params["submitted"], params["project"]]
98
+ end
99
+ end
100
+
101
+ Lita.register_handler(Gerrit)
102
+ end
103
+ end
@@ -0,0 +1,25 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "lita-gerrit"
3
+ spec.version = "0.0.1"
4
+ spec.authors = ["Jonathan Amiez"]
5
+ spec.email = ["jonathan.amiez@gmail.com"]
6
+ spec.description = "Gerrit API client and hook events handler"
7
+ spec.summary = "Retrieve change status from chat and display events"
8
+ spec.homepage = "https://github.com/josqu4red/lita-gerrit"
9
+ spec.license = "MIT"
10
+ spec.metadata = { "lita_plugin_type" => "handler" }
11
+
12
+ spec.files = `git ls-files`.split($/)
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ["lib"]
16
+
17
+ spec.add_runtime_dependency "lita", "~> 3.0"
18
+ spec.add_runtime_dependency "httparty", "~> 0.13.0"
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec", ">= 3.0.0.beta2"
23
+ spec.add_development_dependency "simplecov"
24
+ spec.add_development_dependency "coveralls"
25
+ end
@@ -0,0 +1,73 @@
1
+ require "spec_helper"
2
+
3
+ describe Lita::Handlers::Gerrit, lita_handler: true do
4
+
5
+ before do
6
+ Lita.configure do |config|
7
+ config.handlers.gerrit.url = "https://gerrit.example.com"
8
+ config.handlers.gerrit.username = "foo"
9
+ config.handlers.gerrit.password = "bar"
10
+ end
11
+ end
12
+
13
+ it { routes("get me gerrit 123, please").to(:change_details) }
14
+ it { doesnt_route("gerrit foo").to(:change_details) }
15
+
16
+ it { routes_http(:post, "/gerrit/hooks").to(:hook) }
17
+
18
+ describe "#change_details" do
19
+ let(:response) do
20
+ double("HTTParty::Response")
21
+ end
22
+
23
+ before do
24
+ allow(HTTParty).to receive(:get).and_return(response)
25
+ end
26
+
27
+ it "replies with the title and URL for the issue" do
28
+ allow(response).to receive(:code).and_return(200)
29
+
30
+ allow(response).to receive(:body).and_return(<<-JSON.chomp
31
+ )]}'
32
+ {
33
+ "kind": "gerritcodereview#change",
34
+ "id": "chef~master~Ib0b61ed3eebb8e22596a8401bf976949d798826a",
35
+ "project": "chef",
36
+ "branch": "master",
37
+ "topic": "beanstalk",
38
+ "change_id": "Ib0b61ed3eebb8e22596a8401bf976949d798826a",
39
+ "subject": "haproxy : migrate beanstalk frontend",
40
+ "status": "MERGED",
41
+ "created": "2014-03-14 12:42:15.320000000",
42
+ "updated": "2014-03-17 10:32:21.311000000",
43
+ "_sortkey": "002bcd180000096b",
44
+ "_number": 42,
45
+ "owner": {
46
+ "name": "John Doe"
47
+ }
48
+ }
49
+ JSON
50
+ )
51
+
52
+ send_command("gerrit 42")
53
+
54
+ expect(replies.last).to eq("gerrit: haproxy : migrate beanstalk frontend by John Doe in chef. https://gerrit.example.com/42")
55
+ end
56
+
57
+ it "replies that the issue doesn't exist" do
58
+ allow(response).to receive(:code).and_return(404)
59
+
60
+ send_command("gerrit 42")
61
+
62
+ expect(replies.last).to eq("Change #42 does not exist")
63
+ end
64
+
65
+ it "replies with an exception message" do
66
+ allow(response).to receive(:code).and_return(500)
67
+
68
+ send_command("gerrit 42")
69
+
70
+ expect(replies.last).to match("Error: Failed to fetch https://gerrit.example.com/a/changes/42 (500)")
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,10 @@
1
+ require "simplecov"
2
+ require "coveralls"
3
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
4
+ SimpleCov::Formatter::HTMLFormatter,
5
+ Coveralls::SimpleCov::Formatter
6
+ ]
7
+ SimpleCov.start { add_filter "/spec/" }
8
+
9
+ require "lita-gerrit"
10
+ require "lita/rspec"
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lita-gerrit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Amiez
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: lita
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: httparty
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.13.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.13.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.0.beta2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.0.beta2
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Gerrit API client and hook events handler
112
+ email:
113
+ - jonathan.amiez@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".travis.yml"
120
+ - Gemfile
121
+ - LICENSE
122
+ - README.md
123
+ - Rakefile
124
+ - contrib/lita-gerrit-hook
125
+ - lib/lita-gerrit.rb
126
+ - lib/lita/handlers/gerrit.rb
127
+ - lita-gerrit.gemspec
128
+ - spec/lita/handlers/gerrit_spec.rb
129
+ - spec/spec_helper.rb
130
+ homepage: https://github.com/josqu4red/lita-gerrit
131
+ licenses:
132
+ - MIT
133
+ metadata:
134
+ lita_plugin_type: handler
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.2.0
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Retrieve change status from chat and display events
155
+ test_files:
156
+ - spec/lita/handlers/gerrit_spec.rb
157
+ - spec/spec_helper.rb