Class: Statixite::GitService
- Inherits:
-
Object
- Object
- Statixite::GitService
- Defined in:
- app/services/statixite/git_service.rb
Instance Attribute Summary collapse
-
#error_message ⇒ Object
readonly
Returns the value of attribute error_message.
-
#local_parent ⇒ Object
readonly
Returns the value of attribute local_parent.
-
#local_path ⇒ Object
readonly
Returns the value of attribute local_path.
-
#remote ⇒ Object
readonly
Returns the value of attribute remote.
-
#remote_name ⇒ Object
readonly
Returns the value of attribute remote_name.
Class Method Summary collapse
Instance Method Summary collapse
- #build_branch ⇒ Object
- #build_deploy(next_version) ⇒ Object
- #clone_or_open ⇒ Object
-
#initialize(local_path, remote, status = nil) ⇒ GitService
constructor
A new instance of GitService.
- #make_changes(&block) ⇒ Object
- #successful? ⇒ Boolean
Constructor Details
#initialize(local_path, remote, status = nil) ⇒ GitService
Returns a new instance of GitService.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'app/services/statixite/git_service.rb', line 5 def initialize(local_path, remote, status = nil) @local_path = local_path @remote = remote @remote_name = File.basename(local_path) @local_parent = File.("..", local_path) begin Git.ls_remote(@remote) @status = :success rescue Git::GitExecuteError => e @status = :failed @error_message = e. end end |
Instance Attribute Details
#error_message ⇒ Object (readonly)
Returns the value of attribute error_message.
3 4 5 |
# File 'app/services/statixite/git_service.rb', line 3 def @error_message end |
#local_parent ⇒ Object (readonly)
Returns the value of attribute local_parent.
3 4 5 |
# File 'app/services/statixite/git_service.rb', line 3 def local_parent @local_parent end |
#local_path ⇒ Object (readonly)
Returns the value of attribute local_path.
3 4 5 |
# File 'app/services/statixite/git_service.rb', line 3 def local_path @local_path end |
#remote ⇒ Object (readonly)
Returns the value of attribute remote.
3 4 5 |
# File 'app/services/statixite/git_service.rb', line 3 def remote @remote end |
#remote_name ⇒ Object (readonly)
Returns the value of attribute remote_name.
3 4 5 |
# File 'app/services/statixite/git_service.rb', line 3 def remote_name @remote_name end |
Class Method Details
.create(local_path, remote, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'app/services/statixite/git_service.rb', line 26 def self.create(local_path, remote, ={}) FileUtils.mkdir_p(local_path) git_path = File.join(local_path, ".git") FileUtils.rm_rf(git_path) if Dir.exist?(git_path) File.open(File.join(local_path, ".gitignore"), 'a') do |f| f << "\n_config_preview.yml\n_config_deploy.yml" end FileUtils.mkdir_p(remote) Git.init(remote, :bare => true) g = Git.init(local_path) g.add_remote('origin', remote) g.config('user.name', 'Statixite') g.config('user.email', '[email protected]') g.add(:all => true) g.commit('Initial') begin g.push new(local_path, remote) rescue Git::GitExecuteError => e Rails.logger.error e new(local_path, nil) end end |
Instance Method Details
#build_branch ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/services/statixite/git_service.rb', line 68 def build_branch g = clone_or_open unless g.branches.remote.map(&:name).include?("statixite_build") g.branch('statixite_build').checkout g.remove('.', {:recursive => true}) g.add(:all => true) g.commit("Initial", :allow_empty => true) g.push('origin', 'statixite_build') end g.checkout('statixite_build') g.pull('origin', 'statixite_build') end |
#build_deploy(next_version) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/services/statixite/git_service.rb', line 81 def build_deploy(next_version) g = clone_or_open g.add(:all => true) g.commit("Deployment for #{@remote_name}", :allow_empty => true) version_to_try = next_version begin g.add_tag("v#{version_to_try}") rescue Git::GitExecuteError => e Rails.logger.info "v#{version_to_try} exists retrying" version_to_try = (version_to_try + 0.1).round(1) retry end g.commit("Release tag created #{@remote_name}: v#{version_to_try}", :allow_empty => true) g.push('origin', 'statixite_build') [version_to_try, g..last.sha] end |
#clone_or_open ⇒ Object
19 20 21 22 23 24 |
# File 'app/services/statixite/git_service.rb', line 19 def clone_or_open if successful? Git.clone(remote, remote_name, :path => local_parent) unless Dir.exist?(local_path) Git.open(local_path, :log => Logger.new("/dev/null") ) end end |
#make_changes(&block) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'app/services/statixite/git_service.rb', line 50 def make_changes(&block) if successful? begin g = clone_or_open g.checkout('master') g.pull yield g.add(:all => true) g.commit('Auto commit', :allow_empty => true) g.push rescue Git::GitExecuteError => e @status = :failed @error_message = e. end end self end |
#successful? ⇒ Boolean
98 99 100 |
# File 'app/services/statixite/git_service.rb', line 98 def successful? @status == :success end |