Class: CodeChange

Inherits:
Object
  • Object
show all
Defined in:
lib/models/code_change.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, owner, project, subject, web_url = nil, pipeline_updated_at = nil, pipeline_status = nil, approved = nil, is_merged = nil, is_self = nil, includes_self = nil) ⇒ CodeChange

Returns a new instance of CodeChange.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/models/code_change.rb', line 8

def initialize(id, owner, project, subject, web_url = nil, pipeline_updated_at = nil, pipeline_status = nil, approved = nil, is_merged = nil, is_self = nil, includes_self = nil)
  @id = id
  @owner = owner
  @is_merged = is_merged
  @project = project
  @subject = subject.gsub("'", "")
  @web_url = web_url
  @pipeline_updated_at = pipeline_updated_at
  @pipeline_status = pipeline_status
  @approved = approved
  row = Rubiclifier::DB.query_single_row("SELECT pipeline_updated_at, last_approval_status, last_pipeline_status FROM code_change WHERE id = '#{id}'") || [Time.now().to_s, approved, pipeline_status]
  @last_approval_status = row[1] == 1
  @last_pipeline_status = row[2]
  @last_pipeline_updated_at = row[0] == "" ? Time.now() : Time.parse(row[0])
  @is_self = is_self
  @includes_self = includes_self
end

Instance Attribute Details

#approvedObject (readonly)

Returns the value of attribute approved.



5
6
7
# File 'lib/models/code_change.rb', line 5

def approved
  @approved
end

#code_change_activityObject

Returns the value of attribute code_change_activity.



4
5
6
# File 'lib/models/code_change.rb', line 4

def code_change_activity
  @code_change_activity
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/models/code_change.rb', line 5

def id
  @id
end

#includes_selfObject (readonly)

Returns the value of attribute includes_self.



5
6
7
# File 'lib/models/code_change.rb', line 5

def includes_self
  @includes_self
end

#is_mergedObject (readonly)

Returns the value of attribute is_merged.



5
6
7
# File 'lib/models/code_change.rb', line 5

def is_merged
  @is_merged
end

#is_selfObject (readonly)

Returns the value of attribute is_self.



5
6
7
# File 'lib/models/code_change.rb', line 5

def is_self
  @is_self
end

#last_approval_statusObject (readonly)

Returns the value of attribute last_approval_status.



5
6
7
# File 'lib/models/code_change.rb', line 5

def last_approval_status
  @last_approval_status
end

#last_pipeline_statusObject (readonly)

Returns the value of attribute last_pipeline_status.



5
6
7
# File 'lib/models/code_change.rb', line 5

def last_pipeline_status
  @last_pipeline_status
end

#last_pipeline_updated_atObject (readonly)

Returns the value of attribute last_pipeline_updated_at.



5
6
7
# File 'lib/models/code_change.rb', line 5

def last_pipeline_updated_at
  @last_pipeline_updated_at
end

#ownerObject (readonly)

Returns the value of attribute owner.



5
6
7
# File 'lib/models/code_change.rb', line 5

def owner
  @owner
end

#pipeline_statusObject (readonly)

Returns the value of attribute pipeline_status.



5
6
7
# File 'lib/models/code_change.rb', line 5

def pipeline_status
  @pipeline_status
end

#pipeline_updated_atObject (readonly)

Returns the value of attribute pipeline_updated_at.



5
6
7
# File 'lib/models/code_change.rb', line 5

def pipeline_updated_at
  @pipeline_updated_at
end

#projectObject (readonly)

Returns the value of attribute project.



5
6
7
# File 'lib/models/code_change.rb', line 5

def project
  @project
end

#subjectObject (readonly)

Returns the value of attribute subject.



5
6
7
# File 'lib/models/code_change.rb', line 5

def subject
  @subject
end

#web_urlObject (readonly)

Returns the value of attribute web_url.



5
6
7
# File 'lib/models/code_change.rb', line 5

def web_url
  @web_url
end

Instance Method Details

#activity_from_self_atObject



26
27
28
# File 'lib/models/code_change.rb', line 26

def activity_from_self_at
  @activity_from_self_at ||= code_change_activity.find { |a| a.is_self }&.created_at
end

#generate_additional_activityObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/models/code_change.rb', line 30

def generate_additional_activity
  if is_self
    @code_change_activity.push(GitlabCodeChangeActivity.new("#{id}-placeholder", owner, true, "placeholder", Time.now() - 100000, self))
  end
  if !is_merged
    @code_change_activity.push(GitlabCodeChangeActivity.new("#{id}-opened", owner, is_self, "just opened merge request", Time.now(), self))
  end
  if pipeline_updated_at && pipeline_updated_at > last_pipeline_updated_at && pipeline_status != last_pipeline_status
    @code_change_activity.push(GitlabCodeChangeActivity.new(nil, "Gitlab", false, "pipeline status: #{pipeline_status}", Time.now(), self))
  end
  if approved != last_approval_status
    @code_change_activity.push(GitlabCodeChangeActivity.new(nil, "Gitlab", false, approved ? "fully approved" : "needs approvals again", Time.now(), self))
  end
end

#persistObject



45
46
47
48
49
50
51
# File 'lib/models/code_change.rb', line 45

def persist
  if Rubiclifier::DB.query_single_row("SELECT id FROM code_change WHERE id = '#{id}'")
    Rubiclifier::DB.execute("UPDATE code_change SET pipeline_updated_at = '#{pipeline_updated_at.to_s}', last_pipeline_status = '#{pipeline_status}', last_approval_status = #{approved} WHERE id = '#{id}';")
  else
    Rubiclifier::DB.execute("INSERT INTO code_change (id, pipeline_updated_at, last_pipeline_status, last_approval_status) VALUES('#{id}', '#{pipeline_updated_at.to_s}', '#{pipeline_status}', #{approved});")
  end
end