vx-service_connector 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +5 -0
  5. data/Gemfile +8 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +29 -0
  8. data/Rakefile +10 -0
  9. data/lib/vx/service_connector.rb +37 -0
  10. data/lib/vx/service_connector/base.rb +27 -0
  11. data/lib/vx/service_connector/github.rb +49 -0
  12. data/lib/vx/service_connector/github/commits.rb +33 -0
  13. data/lib/vx/service_connector/github/deploy_keys.rb +29 -0
  14. data/lib/vx/service_connector/github/files.rb +21 -0
  15. data/lib/vx/service_connector/github/hooks.rb +33 -0
  16. data/lib/vx/service_connector/github/notices.rb +40 -0
  17. data/lib/vx/service_connector/github/payload.rb +124 -0
  18. data/lib/vx/service_connector/github/repos.rb +52 -0
  19. data/lib/vx/service_connector/gitlab_v3.rb +29 -0
  20. data/lib/vx/service_connector/gitlab_v3/deploy_keys.rb +20 -0
  21. data/lib/vx/service_connector/gitlab_v3/repos.rb +28 -0
  22. data/lib/vx/service_connector/model.rb +82 -0
  23. data/lib/vx/service_connector/version.rb +5 -0
  24. data/spec/fixtures/github/add_deploy_key.json +6 -0
  25. data/spec/fixtures/github/commit.json +83 -0
  26. data/spec/fixtures/github/create_hook.json +12 -0
  27. data/spec/fixtures/github/create_status.json +5 -0
  28. data/spec/fixtures/github/deploy_keys.json +8 -0
  29. data/spec/fixtures/github/hooks.json +18 -0
  30. data/spec/fixtures/github/org_repos.json +51 -0
  31. data/spec/fixtures/github/orgs.json +8 -0
  32. data/spec/fixtures/github/payload/closed_pull_request.json +392 -0
  33. data/spec/fixtures/github/payload/foreign_pull_request.json +1 -0
  34. data/spec/fixtures/github/payload/pull_request.json +392 -0
  35. data/spec/fixtures/github/payload/push.json +92 -0
  36. data/spec/fixtures/github/payload/push_tag.json +135 -0
  37. data/spec/fixtures/github/user_repos.json +48 -0
  38. data/spec/fixtures/gitlab_v3/deploy_keys.json +14 -0
  39. data/spec/fixtures/gitlab_v3/projects.json +72 -0
  40. data/spec/lib/github_payload_spec.rb +136 -0
  41. data/spec/lib/github_spec.rb +159 -0
  42. data/spec/lib/gitlab_v3_spec.rb +44 -0
  43. data/spec/lib/model_spec.rb +37 -0
  44. data/spec/lib/service_connector_spec.rb +30 -0
  45. data/spec/spec_helper.rb +14 -0
  46. data/spec/support/create.rb +6 -0
  47. data/spec/support/github_web_mocks.rb +101 -0
  48. data/spec/support/gitlab_v3_mocks.rb +29 -0
  49. data/spec/support/read_fixture.rb +12 -0
  50. data/vx-service_connector.gemspec +28 -0
  51. metadata +203 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d0739d7378b49d46cb7a382a39aaf5cff3c95384
4
+ data.tar.gz: 30d5bc44ec412a91f492479e249bdb85aa95a5f4
5
+ SHA512:
6
+ metadata.gz: c310b2305c166e554d7898e5e88d8d88021f6cc48b152ea3cf90377dc42c6726c286c263fdbc71afe0f6d82655b1ff390e1d80ae97b63dcea1f88fd711ee6acb
7
+ data.tar.gz: 7c1e2ce1ee3e1e019311d181406156c275fb547097b0ccf7e7b53d7be98af0194e7349e4ef08e4e38697789fa3ce511380e794ba4766098574baec1c35e4a7c2
data/.gitignore ADDED
@@ -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
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --order=rand
3
+ -fd
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - "2.0.0"
3
+ - "2.1.0"
4
+
5
+ script: "rspec spec/ -fp"
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vx-service_connector.gemspec
4
+ gemspec
5
+
6
+ group :development do
7
+ gem 'httplog'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Dmitry Galinsky
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Vx::ServiceConnector
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'vx-service_connector'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install vx-service_connector
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.require
4
+ require 'rspec/core/rake_task'
5
+ require "bundler/gem_tasks"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task :default => :spec
10
+
@@ -0,0 +1,37 @@
1
+ require File.expand_path("../service_connector/version", __FILE__)
2
+
3
+ module Vx
4
+ module ServiceConnector
5
+
6
+ autoload :Base, File.expand_path("../service_connector/base", __FILE__)
7
+ autoload :Github, File.expand_path("../service_connector/github", __FILE__)
8
+ autoload :GitlabV3, File.expand_path("../service_connector/gitlab_v3", __FILE__)
9
+ autoload :Model, File.expand_path("../service_connector/model", __FILE__)
10
+
11
+ extend self
12
+
13
+ def github ; Github end
14
+ def gitlab_v3 ; GitlabV3 end
15
+
16
+ def to(name)
17
+ case name.to_sym
18
+ when :github
19
+ Github
20
+ when :gitlab_v3
21
+ GitlabV3
22
+ else
23
+ raise ArgumentError, "Serivice for #{name.inspect} is not defined"
24
+ end
25
+ end
26
+
27
+ def payload(name, params)
28
+ case name.to_sym
29
+ when :github
30
+ Github::Payload.new(params).to_model
31
+ else
32
+ raise ArgumentError, "Payload for #{name.inspect} is not defined"
33
+ end
34
+ end
35
+
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ module Vx
2
+ module ServiceConnector
3
+ module Base
4
+ def repos
5
+ raise ArgumentError, 'not implemented'
6
+ end
7
+
8
+ def deploy_keys(repo)
9
+ raise ArgumentError, 'not implemented'
10
+ end
11
+
12
+ def hooks(repo)
13
+ raise ArgumentError, 'not implemented'
14
+ end
15
+
16
+ def notices(repo)
17
+ raise ArgumentError, 'not implemented'
18
+ end
19
+
20
+ private
21
+
22
+ def session
23
+ @session ||= create_session
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,49 @@
1
+ require 'octokit'
2
+
3
+ Octokit.configure do |config|
4
+ config.auto_paginate = true
5
+ end
6
+
7
+ module Vx
8
+ module ServiceConnector
9
+ Github = Struct.new(:login, :access_token) do
10
+
11
+ include ServiceConnector::Base
12
+
13
+ def repos
14
+ @repos ||= Github::Repos.new(session).to_a
15
+ end
16
+
17
+ def hooks(repo)
18
+ Github::Hooks.new(session, repo)
19
+ end
20
+
21
+ def deploy_keys(repo)
22
+ Github::DeployKeys.new(session, repo)
23
+ end
24
+
25
+ def notices(repo)
26
+ Github::Notices.new(session, repo)
27
+ end
28
+
29
+ def files(repo)
30
+ Github::Files.new(session, repo)
31
+ end
32
+
33
+ def commits(repo)
34
+ Github::Commits.new(session, repo)
35
+ end
36
+
37
+ private
38
+
39
+ def create_session
40
+ Octokit::Client.new(login: login, access_token: access_token)
41
+ end
42
+
43
+ end
44
+ end
45
+ end
46
+
47
+ %w{ hooks deploy_keys notices repos payload files commits }.each do |f|
48
+ require File.expand_path("../github/#{f}", __FILE__)
49
+ end
@@ -0,0 +1,33 @@
1
+ module Vx
2
+ module ServiceConnector
3
+ class Github
4
+ Commits = Struct.new(:session, :repo) do
5
+
6
+ def get(sha)
7
+ begin
8
+ commit = session.commit repo.full_name, sha
9
+ commit_to_model commit
10
+ rescue ::Octokit::NotFound => e
11
+ $stderr.puts "ERROR: #{e.inspect}"
12
+ nil
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def commit_to_model(commit)
19
+ url = commit.rels[:html]
20
+
21
+ Model::Commit.new(
22
+ commit.sha,
23
+ commit.commit.message,
24
+ commit.commit.author.name,
25
+ commit.commit.author.email,
26
+ url && url.href
27
+ )
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,29 @@
1
+ module Vx
2
+ module ServiceConnector
3
+ class Github
4
+ DeployKeys = Struct.new(:session, :repo) do
5
+
6
+ def all
7
+ session.deploy_keys(repo.full_name)
8
+ end
9
+
10
+ def create(key_name, public_key)
11
+ session.add_deploy_key(
12
+ repo.full_name,
13
+ key_name,
14
+ public_key
15
+ )
16
+ end
17
+
18
+ def destroy(key_name)
19
+ all.select do |key|
20
+ key.title == key_name
21
+ end.map do |key|
22
+ session.remove_deploy_key(repo.full_name, key.id)
23
+ end
24
+ end
25
+
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ require 'base64'
2
+
3
+ module Vx
4
+ module ServiceConnector
5
+ class Github
6
+ Files = Struct.new(:session, :repo) do
7
+
8
+ def get(sha, path)
9
+ begin
10
+ file = session.contents repo.full_name, ref: sha, path: path
11
+ ::Base64.decode64 file.content
12
+ rescue ::Octokit::NotFound => e
13
+ $stderr.puts "ERROR: #{e.inspect}"
14
+ nil
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ module Vx
2
+ module ServiceConnector
3
+ class Github
4
+ Hooks = Struct.new(:session, :repo) do
5
+
6
+ def all
7
+ session.hooks(repo.full_name)
8
+ end
9
+
10
+ def create(url, token)
11
+ config = {
12
+ url: url,
13
+ secret: token,
14
+ content_type: "json"
15
+ }
16
+ options = { events: %w{ push pull_request } }
17
+ session.create_hook(repo.full_name, "web", config, options)
18
+ end
19
+
20
+ def destroy(url_mask)
21
+ all.select do |hook|
22
+ if url = hook.config.rels[:self]
23
+ url.href =~ /#{Regexp.escape url_mask}/
24
+ end
25
+ end.map do |hook|
26
+ session.remove_hook(repo.full_name, hook.id)
27
+ end
28
+ end
29
+
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,40 @@
1
+ module Vx
2
+ module ServiceConnector
3
+ class Github
4
+ Notices = Struct.new(:session, :repo) do
5
+
6
+ def create(build_sha, build_status, build_url, description)
7
+ if status = github_status(build_status)
8
+ begin
9
+ session.create_status(
10
+ repo.full_name,
11
+ build_sha,
12
+ status,
13
+ description: description,
14
+ target_url: build_url
15
+ )
16
+ rescue Octokit::UnprocessableEntity => e
17
+ STDERR.puts "ERROR: #{e.inspect}"
18
+ end
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def github_status(build_status)
25
+ case build_status.to_sym
26
+ when :started
27
+ 'pending'
28
+ when :passed
29
+ 'success'
30
+ when :failed
31
+ 'failure'
32
+ when :errored
33
+ 'error'
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,124 @@
1
+ module Vx
2
+ module ServiceConnector
3
+ class Github
4
+ class Payload
5
+
6
+ def initialize(params)
7
+ @params = params || {}
8
+ end
9
+
10
+ def pull_request?
11
+ key? "pull_request"
12
+ end
13
+
14
+ def tag?
15
+ !pull_request? && self['ref'] =~ /^#{Regexp.escape 'refs/tags/'}/
16
+ end
17
+
18
+ def pull_request_number
19
+ if pull_request? && key?("number")
20
+ self["number"]
21
+ end
22
+ end
23
+
24
+ def head
25
+ if pull_request?
26
+ pull_request["head"]["sha"]
27
+ else
28
+ self["after"]
29
+ end
30
+ end
31
+
32
+ def base
33
+ if pull_request?
34
+ pull_request["base"]["sha"]
35
+ else
36
+ self["before"]
37
+ end
38
+ end
39
+
40
+ def branch
41
+ if pull_request?
42
+ pull_request["head"]["ref"]
43
+ else
44
+ self["ref"].to_s.split("refs/heads/").last
45
+ end
46
+ end
47
+
48
+ def branch_label
49
+ if pull_request?
50
+ pull_request["head"]["label"]
51
+ else
52
+ branch
53
+ end
54
+ end
55
+
56
+ def url
57
+ if pull_request?
58
+ pull_request["url"]
59
+ else
60
+ self["compare"]
61
+ end
62
+ end
63
+
64
+ def pull_request_head_repo_id
65
+ if pull_request?
66
+ pull_request['head']['repo']['id']
67
+ end
68
+ end
69
+
70
+ def pull_request_base_repo_id
71
+ if pull_request?
72
+ pull_request['base']['repo']['id']
73
+ end
74
+ end
75
+
76
+ def closed_pull_request?
77
+ pull_request? && (pull_request["state"] == 'closed')
78
+ end
79
+
80
+ def foreign_pull_request?
81
+ pull_request? && (pull_request_head_repo_id != pull_request_base_repo_id)
82
+ end
83
+
84
+ def ignore?
85
+ if pull_request?
86
+ closed_pull_request? || !foreign_pull_request?
87
+ else
88
+ head == '0000000000000000000000000000000000000000' ||
89
+ tag?
90
+ end
91
+ end
92
+
93
+ def to_model
94
+ ServiceConnector::Model::Payload.new(
95
+ !!pull_request,
96
+ pull_request_number,
97
+ head,
98
+ base,
99
+ branch,
100
+ branch_label,
101
+ url,
102
+ !!ignore?
103
+ )
104
+ end
105
+
106
+ private
107
+
108
+ def pull_request
109
+ self["pull_request"]
110
+ end
111
+
112
+ def key?(name)
113
+ @params.key? name
114
+ end
115
+
116
+ def [](val)
117
+ @params[val]
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+ end
124
+ end