codescout-runner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 80d1341e3132b625b860c552941d26e6bf532b24
4
+ data.tar.gz: d369ff8c61a1e7157247b5d54a72bd622bf6169b
5
+ SHA512:
6
+ metadata.gz: 65d127e52062fc0c8c548d8f91272123e67e6d2568bebff20c131218c4bfc0bc1665e7cf526922c01378cc5075d464844450c8987bec596ca026144c1ffef6ce
7
+ data.tar.gz: 0e6bbbfb44d0fa5cd8f3563af43484cddd44c35c4296fc4fa308e7c0da946bce85c7504496822ac79417b19345dce69b6d2f1b532549188c207600952c694aea
@@ -0,0 +1,24 @@
1
+ #*
2
+ *.gem
3
+ *.rbc
4
+ *.swp
5
+ *.tmproj
6
+ *~
7
+ .#*
8
+ .DS_Store
9
+ .bundle
10
+ .config
11
+ .yardoc
12
+ Gemfile.lock
13
+ InstalledFiles
14
+ _yardoc
15
+ coverage
16
+ doc/
17
+ lib/bundler/man
18
+ pkg
19
+ rdoc
20
+ spec/reports
21
+ test/tmp
22
+ test/version_tmp
23
+ tmp
24
+ tmtags
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=documentation
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
@@ -0,0 +1,8 @@
1
+ require "bundler"
2
+ require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:test) do |t|
6
+ t.pattern = "spec/**/*_spec.rb"
7
+ t.verbose = false
8
+ end
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path(File.dirname(__FILE__) + "/../lib")
4
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
+
6
+ require "rubygems"
7
+ require "codescout/runner"
8
+
9
+ if ENV["CODESCOUT_URL"].nil?
10
+ STDERR.puts "CODESCOUT_URL is required"
11
+ exit 1
12
+ end
13
+
14
+ if ENV["CODESCOUT_PUSH"].nil?
15
+ STDERR.puts "CODESCOUT_PUSH is required"
16
+ exit 1
17
+ end
18
+
19
+ Codescout::Runner.perform(ENV["CODESCOUT_URL"], ENV["CODESCOUT_PUSH"])
@@ -0,0 +1,27 @@
1
+ require File.expand_path("../lib/codescout/runner/version", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = "codescout-runner"
5
+ s.version = Codescout::Runner::VERSION
6
+ s.summary = "No description for now"
7
+ s.description = "No description for now, maybe later"
8
+ s.homepage = "https://github.com"
9
+ s.authors = ["Dan Sosedoff"]
10
+ s.email = ["dan.sosedoff@gmail.com"]
11
+ s.license = "MIT"
12
+
13
+ s.add_dependency "json", "~> 1.8"
14
+ s.add_dependency "hashie", "~> 3.3"
15
+ s.add_dependency "faraday", "~> 0.9"
16
+ s.add_dependency "codescout-analyzer", "0.0.1"
17
+
18
+ s.add_development_dependency "rake", "~> 10"
19
+ s.add_development_dependency "rspec", "~> 3.0"
20
+ s.add_development_dependency "simplecov", "~> 0.9"
21
+ s.add_development_dependency "webmock", "~> 1.18"
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{|f| File.basename(f)}
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,14 @@
1
+ require "codescout/runner/version"
2
+ require "codescout/runner/errors"
3
+ require "codescout/runner/shell"
4
+ require "codescout/runner/client"
5
+ require "codescout/runner/key"
6
+ require "codescout/runner/build"
7
+
8
+ module Codescout
9
+ module Runner
10
+ def self.perform(service_url, push_token)
11
+ Codescout::Runner::Build.new(service_url, push_token).run
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,67 @@
1
+ module Codescout::Runner
2
+ class Build
3
+ attr_reader :client, :push_token, :push
4
+ attr_reader :clone_path, :output_path
5
+
6
+ def initialize(service_url, push_token)
7
+ @shell = Codescout::Runner::Shell.new
8
+ @client = Codescout::Runner::Client.new(service_url)
9
+ @push_token = push_token
10
+ @clone_path = "/tmp/#{@push_token}"
11
+ @output_path = "#{@clone_path}/codescout.json"
12
+ end
13
+
14
+ def run
15
+ cleanup
16
+ fetch_push
17
+ setup_ssh_keys
18
+ clone_repository
19
+ checkout_commit
20
+ generate_report
21
+ submit_report
22
+ end
23
+
24
+ private
25
+
26
+ def cleanup
27
+ shell("rm -rf #{clone_path}")
28
+ end
29
+
30
+ def fetch_push
31
+ @push = client.fetch_push(push_token)
32
+ end
33
+
34
+ def setup_ssh_keys
35
+ # No need to install ssh keys for public repos
36
+ return unless @push.repository =~ /git@/
37
+
38
+ Codescout::Runner::Key.new(@push.public_key, @push.private_key).install
39
+ end
40
+
41
+ def clone_repository
42
+ options = "--depth 50 --branch #{push.branch}"
43
+ shell("git clone #{options} #{push.repository} #{clone_path}")
44
+ end
45
+
46
+ def checkout_commit
47
+ shell("cd #{clone_path} && git checkout -f #{push.commit}")
48
+ end
49
+
50
+ def generate_report
51
+ shell("codescout #{clone_path} > #{output_path}")
52
+ end
53
+
54
+ def submit_report
55
+ client.send_payload(push_token, File.read(output_path))
56
+ end
57
+
58
+ def shell(command)
59
+ result = @shell.execute(command)
60
+
61
+ if !result
62
+ message = "Command failed: #{command}"
63
+ raise Codescout::Runner::BuildError, message
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,41 @@
1
+ require "faraday"
2
+ require "json"
3
+ require "hashie"
4
+
5
+ module Codescout::Runner
6
+ class Client
7
+ def initialize(url)
8
+ @url = url
9
+ end
10
+
11
+ def fetch_push(token)
12
+ response = connection.get("/worker/push/#{token}")
13
+ json = JSON.load(response.body)
14
+ obj = Hashie::Mash.new(json)
15
+
16
+ if obj.error
17
+ raise Codescout::Runner::ClientError, obj.error
18
+ end
19
+
20
+ obj
21
+
22
+ rescue JSON::ParserError => err
23
+ raise Codescout::Runner::ClientError, err.message
24
+ rescue Faraday::ConnectionFailed => err
25
+ raise Codescout::Runner::ClientError, err.message
26
+ end
27
+
28
+ def send_payload(token, payload)
29
+ connection.post("/worker/payload/#{token}", payload) do |c|
30
+ c.headers["Content-Type"] = "text/plain"
31
+ end
32
+ end
33
+
34
+ def connection
35
+ @connection ||= Faraday.new(@url) do |c|
36
+ c.use(Faraday::Request::UrlEncoded)
37
+ c.adapter(Faraday.default_adapter)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Codescout::Runner
2
+ class Error < StandardError ; end
3
+ class ClientError < Error ; end
4
+ class BuildError < Error ; end
5
+ end
@@ -0,0 +1,41 @@
1
+ module Codescout::Runner
2
+ class Key
3
+ def initialize(public_key, private_key)
4
+ @public_key = public_key
5
+ @private_key = private_key
6
+ end
7
+
8
+ def install
9
+ return if keys_installed?
10
+
11
+ write(private_key_path, @private_key)
12
+ write(public_key_path, @public_key)
13
+ end
14
+
15
+ private
16
+
17
+ def keys_installed?
18
+ installed?(private_key_path) && installed?(public_key_path)
19
+ end
20
+
21
+ def home_path
22
+ ENV["HOME"]
23
+ end
24
+
25
+ def private_key_path
26
+ File.join(home_path, ".ssh/id_rsa")
27
+ end
28
+
29
+ def public_key_path
30
+ File.join(home_path, ".ssh/id_rsa.pub")
31
+ end
32
+
33
+ def write(path, content)
34
+ File.open(path, "w") { |f| f.write(content) }
35
+ end
36
+
37
+ def installed?(path)
38
+ File.exists?(path) && File.size(path) > 0
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,8 @@
1
+ module Codescout::Runner
2
+ class Shell
3
+ def execute(command)
4
+ `#{command}`
5
+ $?.success?
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ module Codescout
2
+ module Runner
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ {
2
+ "error": "Invalid push token",
3
+ "status": 400
4
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "repository": "https://github.com/foo/bar.git",
3
+ "branch": "master",
4
+ "commit": "master",
5
+ "private_key": "private",
6
+ "public_key": "public"
7
+ }
@@ -0,0 +1,76 @@
1
+ require "spec_helper"
2
+
3
+ describe Codescout::Runner::Build do
4
+ let(:build) { described_class.new("http://foo", "token") }
5
+
6
+ describe "#initialize" do
7
+ it "initializes build" do
8
+ expect(build.push_token).to eq "token"
9
+ expect(build.clone_path).to eq "/tmp/token"
10
+ expect(build.output_path).to eq "/tmp/token/codescout.json"
11
+ end
12
+ end
13
+
14
+ describe "#run" do
15
+ let(:shell) { double }
16
+
17
+ let(:push) do
18
+ double(
19
+ repository: "repo",
20
+ branch: "master",
21
+ commit: "commit"
22
+ )
23
+ end
24
+
25
+ before do
26
+ allow(ENV).to receive(:[]).with("HOME") { "/tmp" }
27
+ allow(File).to receive(:read).with("/tmp/token/codescout.json") { "output" }
28
+ allow(Codescout::Runner::Shell).to receive(:new) { shell }
29
+ allow(build.client).to receive(:fetch_push) { push }
30
+ allow(build.client).to receive(:send_payload).with("token", "output")
31
+
32
+ stub_shell(shell, "rm -rf /tmp/token") { true }
33
+ end
34
+
35
+ it "executes the build" do
36
+ stub_shell(shell, "git clone --depth 50 --branch master repo /tmp/token") { true }
37
+ stub_shell(shell, "cd /tmp/token && git checkout -f commit") { true }
38
+ stub_shell(shell, "codescout /tmp/token > /tmp/token/codescout.json") { true }
39
+
40
+ build.run
41
+ end
42
+
43
+ context "when git clone fails" do
44
+ before do
45
+ stub_shell(shell, "git clone --depth 50 --branch master repo /tmp/token") { false }
46
+ end
47
+
48
+ it "raises error" do
49
+ expect { build.run }.to raise_error Codescout::Runner::BuildError, "Command failed: git clone --depth 50 --branch master repo /tmp/token"
50
+ end
51
+ end
52
+
53
+ context "when git checkout fails" do
54
+ before do
55
+ stub_shell(shell, "git clone --depth 50 --branch master repo /tmp/token") { true }
56
+ stub_shell(shell, "cd /tmp/token && git checkout -f commit") { false }
57
+ end
58
+
59
+ it "raises error" do
60
+ expect { build.run }.to raise_error Codescout::Runner::BuildError, "Command failed: cd /tmp/token && git checkout -f commit"
61
+ end
62
+ end
63
+
64
+ context "when codescout command fails" do
65
+ before do
66
+ stub_shell(shell, "git clone --depth 50 --branch master repo /tmp/token") { true }
67
+ stub_shell(shell, "cd /tmp/token && git checkout -f commit") { true }
68
+ stub_shell(shell, "codescout /tmp/token > /tmp/token/codescout.json") { false }
69
+ end
70
+
71
+ it "raises error" do
72
+ expect { build.run }.to raise_error Codescout::Runner::BuildError, "Command failed: codescout /tmp/token > /tmp/token/codescout.json"
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,64 @@
1
+ require "spec_helper"
2
+
3
+ describe Codescout::Runner::Client do
4
+ let(:client) { described_class.new("http://foo") }
5
+
6
+ describe "#fetch_push" do
7
+ context "when connection failed" do
8
+ before do
9
+ allow_any_instance_of(Faraday::Connection).to receive(:get) do
10
+ raise Faraday::ConnectionFailed, "Refused"
11
+ end
12
+ end
13
+
14
+ it "raises error" do
15
+ expect { client.fetch_push("bar") }.
16
+ to raise_error Codescout::Runner::ClientError
17
+ end
18
+ end
19
+
20
+ context "when push token is invalid" do
21
+ before do
22
+ stub_request(:get, "http://foo/worker/push/bar").
23
+ with(headers: {
24
+ 'Accept' => '*/*',
25
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
26
+ 'User-Agent' => 'Faraday v0.9.0'
27
+ }).
28
+ to_return(
29
+ status: 400, body: fixture("invalid_token.json"), headers: {}
30
+ )
31
+ end
32
+
33
+ it "raises error" do
34
+ expect { client.fetch_push("bar") }.
35
+ to raise_error Codescout::Runner::ClientError
36
+ end
37
+ end
38
+
39
+ context "when push token is valid" do
40
+ let(:result) { client.fetch_push("bar") }
41
+
42
+ before do
43
+ stub_request(:get, "http://foo/worker/push/bar").
44
+ with(headers: {
45
+ 'Accept' => '*/*',
46
+ 'Accept-Encoding' => 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
47
+ 'User-Agent' => 'Faraday v0.9.0'
48
+ }).
49
+ to_return(
50
+ status: 200, body: fixture("push.json"), headers: {}
51
+ )
52
+ end
53
+
54
+ it "returns a hashie object" do
55
+ expect(result).to be_a Hashie::Mash
56
+ expect(result.repository).to eq "https://github.com/foo/bar.git"
57
+ expect(result.branch).to eq "master"
58
+ expect(result.commit).to eq "master"
59
+ expect(result.private_key).to eq "private"
60
+ expect(result.public_key).to eq "public"
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,34 @@
1
+ require "spec_helper"
2
+
3
+ describe Codescout::Runner::Key do
4
+ let(:private_key) { "private" }
5
+ let(:public_key) { "public" }
6
+
7
+ let(:key) do
8
+ described_class.new(public_key, private_key)
9
+ end
10
+
11
+ before(:all) do
12
+ FileUtils.mkdir_p("/tmp/.ssh")
13
+ end
14
+
15
+ before do
16
+ allow(ENV).to receive(:[]).with("HOME") { "/tmp" }
17
+ end
18
+
19
+ after(:all) do
20
+ FileUtils.rm_rf("/tmp/.ssh")
21
+ end
22
+
23
+ describe "#install" do
24
+ before { key.install }
25
+
26
+ it "writes private key" do
27
+ expect(File.read("/tmp/.ssh/id_rsa")).to eq private_key
28
+ end
29
+
30
+ it "write public key" do
31
+ expect(File.read("/tmp/.ssh/id_rsa.pub")).to eq public_key
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe Codescout::Runner::Shell do
4
+ let(:shell) { described_class.new }
5
+
6
+ describe "#execute" do
7
+ pending
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ require "spec_helper"
2
+
3
+ describe Codescout::Runner do
4
+ describe ".perform" do
5
+ let(:build) { double }
6
+
7
+ before do
8
+ allow(build).to receive(:run)
9
+ allow(Codescout::Runner::Build).to receive(:new) { build }
10
+
11
+ described_class.perform("url", "token")
12
+ end
13
+
14
+ it "executes a build" do
15
+ expect(build).to have_received(:run)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,23 @@
1
+ $:.unshift File.expand_path("../..", __FILE__)
2
+
3
+ require "simplecov"
4
+
5
+ SimpleCov.start do
6
+ add_filter "spec/"
7
+ end
8
+
9
+ require "webmock/rspec"
10
+ require "lib/codescout/runner"
11
+
12
+ def fixture_path(filename = nil)
13
+ path = File.expand_path("../fixtures", __FILE__)
14
+ filename.nil? ? path : File.join(path, filename)
15
+ end
16
+
17
+ def fixture(file)
18
+ File.read(File.join(fixture_path, file))
19
+ end
20
+
21
+ def stub_shell(shell, command)
22
+ allow(shell).to receive(:execute).with(command) { yield }
23
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codescout-runner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Sosedoff
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.9'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.9'
55
+ - !ruby/object:Gem::Dependency
56
+ name: codescout-analyzer
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.0.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.0.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.9'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.9'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.18'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.18'
125
+ description: No description for now, maybe later
126
+ email:
127
+ - dan.sosedoff@gmail.com
128
+ executables:
129
+ - codescout-runner
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - Gemfile
136
+ - Rakefile
137
+ - bin/codescout-runner
138
+ - codescout-runner.gemspec
139
+ - lib/codescout/runner.rb
140
+ - lib/codescout/runner/build.rb
141
+ - lib/codescout/runner/client.rb
142
+ - lib/codescout/runner/errors.rb
143
+ - lib/codescout/runner/key.rb
144
+ - lib/codescout/runner/shell.rb
145
+ - lib/codescout/runner/version.rb
146
+ - spec/fixtures/invalid_token.json
147
+ - spec/fixtures/push.json
148
+ - spec/lib/runner/build_spec.rb
149
+ - spec/lib/runner/client_spec.rb
150
+ - spec/lib/runner/key_spec.rb
151
+ - spec/lib/runner/shell_spec.rb
152
+ - spec/lib/runner_spec.rb
153
+ - spec/spec_helper.rb
154
+ homepage: https://github.com
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubyforge_project:
174
+ rubygems_version: 2.2.2
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: No description for now
178
+ test_files:
179
+ - spec/fixtures/invalid_token.json
180
+ - spec/fixtures/push.json
181
+ - spec/lib/runner/build_spec.rb
182
+ - spec/lib/runner/client_spec.rb
183
+ - spec/lib/runner/key_spec.rb
184
+ - spec/lib/runner/shell_spec.rb
185
+ - spec/lib/runner_spec.rb
186
+ - spec/spec_helper.rb