Class: Statixite::GitService

Inherits:
Object
  • Object
show all
Defined in:
app/services/statixite/git_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.expand_path("..", local_path)
  begin
    Git.ls_remote(@remote)
    @status = :success
  rescue Git::GitExecuteError => e
    @status = :failed
    @error_message = e.message
  end
end

Instance Attribute Details

#error_messageObject (readonly)

Returns the value of attribute error_message.



3
4
5
# File 'app/services/statixite/git_service.rb', line 3

def error_message
  @error_message
end

#local_parentObject (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_pathObject (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

#remoteObject (readonly)

Returns the value of attribute remote.



3
4
5
# File 'app/services/statixite/git_service.rb', line 3

def remote
  @remote
end

#remote_nameObject (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, options={})
  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_branchObject



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.tags.last.sha]
end

#clone_or_openObject



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.message
    end
  end
  self
end

#successful?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'app/services/statixite/git_service.rb', line 98

def successful?
  @status == :success
end