vx-common 0.2.1 → 0.3.0

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: 5b928dea4a82d1bc17cd3f669f2099de604c2db5
4
- data.tar.gz: 92e0a9deaad7d0b554e39024ac59ef23c90d913c
3
+ metadata.gz: 1e52cb8a831a12ebb0b4da518a8f38f3b5582512
4
+ data.tar.gz: 21afcd8458f66eaf687c1fda335983400fcd15df
5
5
  SHA512:
6
- metadata.gz: ee9be5d556a05478f0e7d985561be48493cd6d6b9847ad817ecbf79395adb62d495f7fd6ae508088cfad7c80f91cb8a43716110d46557df62b5b4eadfa96d792
7
- data.tar.gz: de26c840af0c321b5be8f4370c6851a129af5fe41abce21fa278a98c42a44780d490a9f1b91e687dc6465e87f540202245a604c5096cbae6b24f91c73f35ac73
6
+ metadata.gz: 7c4e40ceecbb3be8c2b457aa46e9d5423c1f0f6f696d9f12cb37a267184929ee790474e44bab7a0a266c91b1e84f8d4fa3a4d657b7b944ecf6703967b619b13b
7
+ data.tar.gz: 65a54e01ebce7d38927db4fe18eb60e3a4eeb4b316ecff455a8fb0495dde0461e43671bbf465012ca28d1d6efd574f578ccc5dc053064d51bdf2df4b0da7a74c
data/lib/vx/common.rb CHANGED
@@ -11,10 +11,7 @@ module Vx
11
11
  autoload :OutputBuffer, File.expand_path("../common/output_buffer", __FILE__)
12
12
  autoload :EnvFile, File.expand_path("../common/env_file", __FILE__)
13
13
  autoload :ErrorNotifier, File.expand_path("../common/error_notifier", __FILE__)
14
- end
15
-
16
- module SCM
17
- autoload :Git, File.expand_path("../scm/git", __FILE__)
14
+ autoload :Git, File.expand_path("../common/git", __FILE__)
18
15
  end
19
16
 
20
17
  end
@@ -0,0 +1,61 @@
1
+ require 'ostruct'
2
+
3
+ module Vx
4
+ module Common
5
+
6
+ class Git
7
+
8
+ COMMIT_RE = /^(.*) -:- (.*) \((.*)\) -:- (.*)$/
9
+
10
+ attr_reader :src, :sha, :path, :logger, :git_ssh, :branch, :pull_request_id
11
+
12
+ def initialize(src, sha, path, options = {}, &block)
13
+ @src = src
14
+ @sha = sha
15
+ @path = path
16
+ @branch = options[:branch]
17
+ @pull_request_id = options[:pull_request_id]
18
+ @logger = block
19
+ end
20
+
21
+ def git_ssh_content(key_location)
22
+ key = key_location ? "-i #{key_location}" : ""
23
+ out = "#!/bin/sh\n"
24
+ out << "/usr/bin/ssh"
25
+ out << " -A -o LogLevel=quiet"
26
+ out << " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
27
+ out << " #{key} $@\n"
28
+ out
29
+ end
30
+
31
+ def fetch_cmd(options = {})
32
+ depth = options.key?(:depth) ? options[:depth] : 50
33
+ clone_branch = " --branch=#{branch}" if branch
34
+ checkout_cmd = "git checkout -qf #{sha}"
35
+ fetch_cmd = nil
36
+
37
+ if pull_request_id
38
+ clone_branch = ""
39
+ fetch_cmd = "git fetch origin +refs/pull/#{pull_request_id}/merge:"
40
+ checkout_cmd = "git checkout -q FETCH_HEAD"
41
+ end
42
+
43
+ clone_cmd = "git clone --depth=#{depth}#{clone_branch} #{src} #{path}"
44
+
45
+ cmd = []
46
+ cmd << %{ echo "$ #{clone_cmd}" }
47
+ cmd << clone_cmd
48
+ if fetch_cmd
49
+ cmd << %{ echo "$ #{fetch_cmd}" }
50
+ cmd << %{ ( cd #{path} && #{fetch_cmd} ) }
51
+ end
52
+ cmd << %{ echo "$ #{checkout_cmd}" }
53
+ cmd << %{ ( cd #{path} && #{checkout_cmd} ) }
54
+
55
+ cmd.join("\n")
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+ end
@@ -1,5 +1,5 @@
1
1
  module Vx
2
2
  module Common
3
- VERSION = "0.2.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -0,0 +1,113 @@
1
+ require 'spec_helper'
2
+ require 'tmpdir'
3
+ require 'vx/message/testing'
4
+ require 'vx/common/spawn'
5
+
6
+ describe Vx::Common::Git do
7
+ let(:path) { Dir.tmpdir }
8
+ let(:message) { Vx::Message::PerformBuild.test_message }
9
+ let(:src) { message.src }
10
+ let(:sha) { message.sha }
11
+ let(:deploy_key) { message.deploy_key }
12
+ let(:output) { "" }
13
+ let(:options) { {} }
14
+ let(:git) {
15
+ described_class.new src, sha, "#{path}/repo", options, &method(:add_to_output)
16
+ }
17
+
18
+ subject { git }
19
+
20
+ before do
21
+ FileUtils.rm_rf path
22
+ FileUtils.mkdir_p path
23
+ File.open("#{path}/ssh.key", 'w', 0600) do |io|
24
+ io.write deploy_key
25
+ end
26
+ end
27
+
28
+ after { FileUtils.rm_rf path }
29
+
30
+ context "just created" do
31
+ its(:src) { should eq src }
32
+ its(:sha) { should eq sha }
33
+ its(:path) { should eq "#{path}/repo" }
34
+ its(:branch) { should be_nil }
35
+ end
36
+
37
+ context "assign branch" do
38
+ let(:options) { { branch: 'master' } }
39
+ its(:branch) { should eq 'master' }
40
+ end
41
+
42
+ context "run fetch_cmd" do
43
+ include Vx::Common::Spawn
44
+
45
+ let(:options) { {
46
+ branch: "master"
47
+ } }
48
+ let(:git_ssh_content) {
49
+ git.git_ssh_content "$(dirname $0)/ssh.key"
50
+ }
51
+ let(:run) {
52
+ cmd = git.fetch_cmd
53
+ cmd = "set -e\n#{cmd}"
54
+ spawn(git_ssh_env, cmd, &method(:add_to_output))
55
+ }
56
+ subject { run }
57
+
58
+ before do
59
+ File.open("#{path}/git_ssh", 'w', 0755) do |io|
60
+ io.write git_ssh_content
61
+ end
62
+ run
63
+ end
64
+
65
+ context "success" do
66
+ it { should be }
67
+
68
+ it "should return zero" do
69
+ expect(run).to eq 0
70
+ end
71
+
72
+ it "should create nessesary directories and checkout sha" do
73
+ expect(File.directory? "#{path}/repo").to be
74
+ expect(File.directory? "#{path}/repo/.git/objects").to be
75
+ Dir.chdir "#{path}/repo" do
76
+ expect((`git rev-parse HEAD`).strip).to eq sha
77
+ end
78
+ end
79
+
80
+ context "with pull_request" do
81
+ let(:options) { { pull_request_id: 1 } }
82
+ it "should return zero" do
83
+ expect(run).to eq 0
84
+ end
85
+ end
86
+ end
87
+
88
+ context "fail" do
89
+ context "when repo does not exists" do
90
+ let(:src) { "/not-exists-repo.git" }
91
+ it "should return non zero status" do
92
+ expect(run).to_not eq 0
93
+ end
94
+ end
95
+
96
+ context "when sha does not exists" do
97
+ let(:sha) { "is-not-exists" }
98
+ it "should return non zero status" do
99
+ expect(run).to_not eq 0
100
+ end
101
+ end
102
+ end
103
+
104
+ def git_ssh_env
105
+ { 'GIT_SSH' => "#{path}/git_ssh" }
106
+ end
107
+ end
108
+
109
+ def add_to_output(out)
110
+ puts "==> #{out}"
111
+ output << out
112
+ end
113
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vx-common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Galinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-19 00:00:00.000000000 Z
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: vx-common-spawn
@@ -139,19 +139,18 @@ files:
139
139
  - lib/vx/common/amqp_setup.rb
140
140
  - lib/vx/common/env_file.rb
141
141
  - lib/vx/common/error_notifier.rb
142
+ - lib/vx/common/git.rb
142
143
  - lib/vx/common/helper/middlewares.rb
143
144
  - lib/vx/common/helper/shell.rb
144
145
  - lib/vx/common/helper/upload_sh_command.rb
145
146
  - lib/vx/common/output_buffer.rb
146
147
  - lib/vx/common/tagged_logging.rb
147
148
  - lib/vx/common/version.rb
148
- - lib/vx/scm/git.rb
149
- - lib/vx/scm/git/git_ssh.rb
149
+ - spec/lib/common/git_spec.rb
150
150
  - spec/lib/common/helper/middlewares_spec.rb
151
151
  - spec/lib/common/helper/shell_spec.rb
152
152
  - spec/lib/common/helper/upload_sh_command_spec.rb
153
153
  - spec/lib/common/output_buffer_spec.rb
154
- - spec/lib/scm/git_spec.rb
155
154
  - spec/spec_helper.rb
156
155
  - vx-common.gemspec
157
156
  homepage: ''
@@ -179,9 +178,9 @@ signing_key:
179
178
  specification_version: 4
180
179
  summary: Common code for ci
181
180
  test_files:
181
+ - spec/lib/common/git_spec.rb
182
182
  - spec/lib/common/helper/middlewares_spec.rb
183
183
  - spec/lib/common/helper/shell_spec.rb
184
184
  - spec/lib/common/helper/upload_sh_command_spec.rb
185
185
  - spec/lib/common/output_buffer_spec.rb
186
- - spec/lib/scm/git_spec.rb
187
186
  - spec/spec_helper.rb
data/lib/vx/scm/git.rb DELETED
@@ -1,108 +0,0 @@
1
- require 'ostruct'
2
- require File.expand_path("../git/git_ssh", __FILE__)
3
-
4
- module Vx
5
- module SCM
6
-
7
- class Git
8
-
9
- include Common::Helper::Shell
10
-
11
- COMMIT_RE = /^(.*) -:- (.*) \((.*)\) -:- (.*)$/
12
-
13
- attr_reader :src, :sha, :path, :logger, :git_ssh, :branch, :pull_request_id
14
-
15
- def initialize(src, sha, path, options = {}, &block)
16
- @src = src
17
- @sha = sha
18
- @path = path
19
- @branch = options[:branch]
20
- @git_ssh = GitSSH.new options[:deploy_key]
21
- @pull_request_id = options[:pull_request_id]
22
- @logger = block
23
- end
24
-
25
- def open
26
- git_ssh.open do
27
- yield if block_given?
28
- end
29
- end
30
-
31
- def fetch
32
- open do
33
- run_git make_fetch_command
34
- end
35
- end
36
-
37
- def self.make_export_command(from, to)
38
- %{ (cd '#{from}' && git checkout-index -a -f --prefix='#{to}/') }.strip
39
- end
40
-
41
- def make_fetch_command(options = {})
42
- depth = options.key?(:depth) ? options[:depth] : 50
43
- clone_branch = " --branch=#{branch}" if branch
44
- checkout_cmd = "git checkout -qf #{sha}"
45
- fetch_cmd = nil
46
-
47
- if pull_request_id
48
- clone_branch = ""
49
- fetch_cmd = "git fetch origin +refs/pull/#{pull_request_id}/merge:"
50
- checkout_cmd = "git checkout -q FETCH_HEAD"
51
- end
52
-
53
- clone_cmd = "git clone -q --depth=#{depth}#{clone_branch} #{src} #{path}"
54
-
55
- cmd = []
56
- cmd << %{ echo "$ #{clone_cmd}" && #{clone_cmd} }
57
- cmd << %{ cd #{path} }
58
- cmd << %{ echo "$ #{fetch_cmd}" && #{fetch_cmd} } if fetch_cmd
59
- cmd << %{ echo "$ #{checkout_cmd}" && #{checkout_cmd} }
60
-
61
- cmd = cmd.join(" && ").gsub("\n", ' ').gsub(/\ +/, ' ').strip
62
- cmd
63
- end
64
-
65
- def commit_info
66
- rs = {}
67
- if str = commit_info_string
68
- if m = str.match(COMMIT_RE)
69
- rs.merge!(
70
- sha: m[1],
71
- author: m[2],
72
- email: m[3],
73
- message: m[4]
74
- )
75
- end
76
- end
77
- OpenStruct.new rs
78
- end
79
-
80
- private
81
-
82
- def commit_info_string
83
- output = ""
84
- code = spawn commit_info_cmd, chdir: path do |io|
85
- output << io
86
- end
87
- if code == 0
88
- output.strip
89
- else
90
- nil
91
- end
92
- end
93
-
94
- def commit_info_cmd
95
- %{git log -1 --pretty=format:'%H -:- %cn (%ce) -:- %s'}
96
- end
97
-
98
- def run_git(cmd, options = {})
99
- env = {
100
- 'GIT_SSH' => git_ssh.location.path
101
- }
102
- spawn(env, cmd, options, &logger)
103
- end
104
-
105
- end
106
-
107
- end
108
- end
@@ -1,58 +0,0 @@
1
- module Vx
2
- module SCM
3
-
4
- class Git
5
-
6
- class GitSSH
7
-
8
- include Common::Helper::Shell
9
-
10
- attr_reader :deploy_key
11
-
12
- def initialize(deploy_key)
13
- @deploy_key = deploy_key
14
- end
15
-
16
- def open
17
- begin
18
- yield create
19
- ensure
20
- destroy
21
- end
22
- end
23
-
24
- def create
25
- key_location
26
- location
27
- end
28
-
29
- def destroy
30
- key_location.unlink if key_location
31
- location.unlink
32
- @location = nil
33
- @key_location = nil
34
- end
35
-
36
- def location
37
- @location ||= write_tmp_file 'git', self.class.template(key_location && key_location.path), 0700
38
- end
39
-
40
- def key_location
41
- if deploy_key
42
- @key_location ||= write_tmp_file 'key', deploy_key, 0600
43
- end
44
- end
45
-
46
- class << self
47
- def template(key_location)
48
- key = key_location ? "-i #{key_location}" : ""
49
- out = ['#!/bin/sh']
50
- out << "exec /usr/bin/ssh -A -o LogLevel=quiet -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null #{key} $@"
51
- out.join "\n"
52
- end
53
- end
54
-
55
- end
56
- end
57
- end
58
- end
@@ -1,169 +0,0 @@
1
- require 'spec_helper'
2
- require 'vx/message/testing'
3
-
4
- describe Vx::SCM::Git do
5
- let(:path) { '/tmp/.test/repo' }
6
- let(:message) { Vx::Message::PerformBuild.test_message }
7
- let(:src) { message.src }
8
- let(:sha) { message.sha }
9
- let(:deploy_key) { message.deploy_key }
10
- let(:output) { "" }
11
- let(:options) { {} }
12
- let(:git) {
13
- described_class.new src, sha, path, options, &method(:add_to_output)
14
- }
15
-
16
- subject { git }
17
-
18
- before do
19
- FileUtils.rm_rf path
20
- FileUtils.mkdir_p path
21
- end
22
-
23
- after { FileUtils.rm_rf path }
24
-
25
- context "just created" do
26
- its(:src) { should eq src }
27
- its(:sha) { should eq sha }
28
- its(:path) { should eq path }
29
- its(:branch) { should be_nil }
30
- its("git_ssh.deploy_key") { should be_nil }
31
- end
32
-
33
- context "assign branch" do
34
- let(:options) { { branch: 'master' } }
35
- its(:branch) { should eq 'master' }
36
- end
37
-
38
- context "assign deploy_key" do
39
- let(:options) { { deploy_key: deploy_key } }
40
- its("git_ssh.deploy_key") { should eq deploy_key }
41
- end
42
-
43
- context "fetch" do
44
- let(:options) { { deploy_key: deploy_key } }
45
- subject { git.fetch }
46
-
47
- it { should eq 0 }
48
-
49
- it "should create nessesary directories and checkout sha" do
50
- subject
51
- expect(File.directory? path).to be
52
- expect(File.directory? "#{path}/.git").to be
53
- Dir.chdir path do
54
- expect((`git rev-parse HEAD`).strip).to eq sha
55
- end
56
- end
57
-
58
- it "should capture output" do
59
- subject
60
- expect(output).to match(Regexp.escape "$ git clone -q --depth=50 #{src} #{path}")
61
- end
62
-
63
- context "with error" do
64
- let(:src) { "/not-exists-repo.git" }
65
-
66
- it "should return 128 exitstatus and add error to output" do
67
- expect(subject).to eq 128
68
- expect(output).to match('does not exist')
69
- end
70
- end
71
- end
72
-
73
- context "make_fetch_command" do
74
- include Vx::Common::Spawn
75
-
76
- let(:options) { { deploy_key: deploy_key, branch: "master" } }
77
- let(:run) do
78
- git.open do
79
- spawn(git_ssh_env, git.make_fetch_command, &method(:add_to_output))
80
- end
81
- end
82
- subject { git.make_fetch_command }
83
-
84
- before do
85
- run
86
- end
87
-
88
- it { should be }
89
-
90
- it "should be success" do
91
- expect(run).to eq 0
92
- end
93
-
94
- it "should create nessesary directories and checkout sha" do
95
- expect(File.directory? path).to be
96
- expect(File.directory? "#{path}/.git").to be
97
- Dir.chdir path do
98
- expect((`git rev-parse HEAD`).strip).to eq sha
99
- end
100
- end
101
-
102
- context "with error" do
103
- let(:src) { "/not-exists-repo.git" }
104
-
105
- it "should return 128 exitstatus and add error to output" do
106
- expect(run).to eq 128
107
- end
108
- end
109
-
110
- def git_ssh_env
111
- { 'GIT_SSH' => git.git_ssh.location.path }
112
- end
113
- end
114
-
115
- context ".make_export_command" do
116
- let(:options) { { deploy_key: deploy_key } }
117
- let(:from) { path }
118
- let(:to) { '/tmp/.test/export' }
119
- let(:expected) { "(cd '#{ from }' && git checkout-index -a -f --prefix='#{ to}/')" }
120
- subject { described_class.make_export_command from, to}
121
- it { should eq expected }
122
-
123
- context "run" do
124
- before do
125
- git.fetch
126
- system subject
127
- end
128
-
129
- it "should be success" do
130
- expect($?.to_i).to eq 0
131
- end
132
-
133
- it "should export repo" do
134
- expect(File.readable? "#{to}/Gemfile").to be_true
135
- end
136
-
137
- context "run with pull_request" do
138
- let(:options) { { deploy_key: deploy_key, pull_request_id: 1 } }
139
-
140
- it "should be success" do
141
- expect($?.to_i).to eq 0
142
- end
143
-
144
- it "should export repo" do
145
- expect(File.readable? "#{to}/Gemfile").to be_true
146
- end
147
- end
148
- end
149
-
150
- end
151
-
152
- context "commit_info" do
153
- let(:options) { { deploy_key: deploy_key } }
154
- subject { git.commit_info }
155
- before { git.fetch }
156
-
157
- it "should be" do
158
- expect(subject.sha).to eq 'b665f90239563c030f1b280a434b3d84daeda1bd'
159
- expect(subject.author).to eq "Dmitry Galinsky"
160
- expect(subject.email).to eq 'dima.exe@gmail.com'
161
- expect(subject.message).to eq 'first release'
162
- end
163
- end
164
-
165
- def add_to_output(out)
166
- puts "==> #{out}"
167
- output << out
168
- end
169
- end