Class: VetCI::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/vet-ci/project.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Project

Returns a new instance of Project.



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vet-ci/project.rb', line 98

def initialize(attributes = {})
  attributes.each do |key, value|
    self.send("#{key}=", value)
  end
  
  # Now, let's load any existing builds in the list from the datastore
  unless Project.datastore.nil?
    Project.datastore.transaction(true) do
      builds = Project.datastore[self.name]
      self.builds.concat(builds) unless builds.nil?
    end
  end
end

Instance Attribute Details

#autoupdateObject

Returns the value of attribute autoupdate.



62
63
64
# File 'lib/vet-ci/project.rb', line 62

def autoupdate
  @autoupdate
end

#build_commandObject

Returns the value of attribute build_command.



60
61
62
# File 'lib/vet-ci/project.rb', line 60

def build_command
  @build_command
end

#buildingObject

Returns the value of attribute building.



63
64
65
# File 'lib/vet-ci/project.rb', line 63

def building
  @building
end

#default_branchObject

Returns the value of attribute default_branch.



61
62
63
# File 'lib/vet-ci/project.rb', line 61

def default_branch
  @default_branch
end

#nameObject

Returns the value of attribute name.



58
59
60
# File 'lib/vet-ci/project.rb', line 58

def name
  @name
end

#project_pathObject

Returns the value of attribute project_path.



59
60
61
# File 'lib/vet-ci/project.rb', line 59

def project_path
  @project_path
end

Class Method Details

.datastoreObject



24
25
26
# File 'lib/vet-ci/project.rb', line 24

def datastore
  @datastore
end

.datastore=(value) ⇒ Object



28
29
30
# File 'lib/vet-ci/project.rb', line 28

def datastore=(value)
  @datastore = value
end

.named(value) ⇒ Object

Returns a project in the list that is named ..



33
34
35
# File 'lib/vet-ci/project.rb', line 33

def named(value)
  self.projects[value]
end

.parse_vetfile_contents(parameters) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/vet-ci/project.rb', line 37

def parse_vetfile_contents(parameters)
  parameters.each do |project|
    name = project['name']
    path = project['path']
    command = project['command']
    default_branch = project['default_branch']
    autoupdate = (project['autoupdate'].nil? ? false : project['autoupdate'])
    
    if path.nil?
      path = Dir.pwd
    else
      path = File.expand_path path
    end
    
    project = Project.new(:name => name, :project_path => path, :build_command => command, :default_branch => default_branch, :autoupdate => autoupdate)
    
    self.projects[project.name] = project
  end
end

.projectsObject

This shouldn’t be here, but it was easy. We’ll refactor it when that time comes…



16
17
18
# File 'lib/vet-ci/project.rb', line 16

def projects
  @projects ||= {}
end

.projects=(value) ⇒ Object



20
21
22
# File 'lib/vet-ci/project.rb', line 20

def projects=(value)
  @projects = value
end

Instance Method Details

#build(faye = nil, payload = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/vet-ci/project.rb', line 143

def build(faye=nil, payload=nil)
  if is_building?
    return
  end
  
  if payload
    begin
      payload = JSON.parse payload
    rescue
      payload = nil
    end
  end
  
  Thread.new {build!(faye, payload)}
end

#build!(faye = nil, payload = nil) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/vet-ci/project.rb', line 159

def build!(faye=nil, payload=nil)
  self.building = true

  unless faye.nil?
    faye.publish '/all', :project => self.name, :status => 'running'
  end
  
  # First, let's reset the repo if we said we should in the Vetfile
  if self.autoupdate == true
    puts "Updating the repo"
    self.git_update
  end

  @result = ''
  repo = Grit::Repo.new @project_path
  commit = repo.commits.first
  Util.open_pipe("cd #{@project_path} && #{@build_command} 2>&1") do |pipe, process_id|
    puts "#{Time.now.to_i}: Building with command '#{@build_command}'..."
    @current_pid = process_id
    @result = pipe.read
  end
  
  Process.waitpid(@current_pid)
  puts $?.exitstatus.to_i
  
  if commit.nil?
    current_build = Build.new(:project => self, :status => $?.exitstatus.to_i, :output => @result, :date => Time.now, :payload => payload)
  else
    current_build = Build.new(:project => self, :status => $?.exitstatus.to_i, :output => @result, :date => Time.now, :commit => commit.id, :committer => commit.committer.name, :payload => payload)
  end

  puts "Saving build..."
  unless faye.nil?
    faye.publish '/all', :project => self.name, :status => current_build.status_class, :last_build => current_build.dashboard_time
  end
  
  self.insertBuild current_build
  self.building = false
  self.save!
end

#buildsObject



66
67
68
# File 'lib/vet-ci/project.rb', line 66

def builds
  @builds ||= []
end

#builds=(value) ⇒ Object



81
82
83
# File 'lib/vet-ci/project.rb', line 81

def builds=(value)
  @builds = value
end

#git_updateObject



120
121
122
123
# File 'lib/vet-ci/project.rb', line 120

def git_update
  reset_branch = self.default_branch.nil? ? "" : " origin/#{self.default_branch}"
  `cd #{self.project_path} && git fetch origin && git reset --hard#{reset_branch}`
end

#insertBuild(build) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/vet-ci/project.rb', line 85

def insertBuild(build)
  self.builds << build
  @builds.sort! do |a, b|
    if a.date == b.date
      0
    elsif a.date < b.date
      1
    else
      -1
    end
  end
end

#is_building?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/vet-ci/project.rb', line 129

def is_building?
  self.building == true
end

#last_build_statusObject



133
134
135
136
137
138
139
140
141
# File 'lib/vet-ci/project.rb', line 133

def last_build_status
  if is_building?
    return 'running'
  elsif self.builds.count > 0
    return self.builds.first.status_class
  else
    return ''
  end
end

#pagedBuilds(page = 1, limit = 10) ⇒ Object



70
71
72
73
74
75
76
77
78
79
# File 'lib/vet-ci/project.rb', line 70

def pagedBuilds(page=1, limit=10)
  # We need to calculate the slice
  page = page.nil? ? 1 : page.to_i
  limit = limit.nil? ? 10 : limit.to_i
  page = 1 if page < 0
  start = (page-1) * limit
  # limit is the length
  result = self.builds[start,limit]
  result.nil? ? [] : result
end

#repoObject



125
126
127
# File 'lib/vet-ci/project.rb', line 125

def repo
  Grit::Repo.new(self.project_path)
end

#save!Object



112
113
114
115
116
117
118
# File 'lib/vet-ci/project.rb', line 112

def save!
  unless Project.datastore.nil?
    Project.datastore.transaction do
      Project.datastore[self.name] = @builds
    end
  end
end