Class: Git::Story::SemaphoreResponse

Inherits:
JSON::GenericObject
  • Object
show all
Defined in:
lib/git/story/semaphore.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get(url, debug: false) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/git/story/semaphore.rb', line 7

def self.get(url, debug: false)
  data = URI.open(url).read
  debug and STDERR.puts JSON.pretty_generate(JSON(data))
  result = JSON(data, object_class: self)
  result.debug = debug
  result
end

Instance Method Details

#branch_historyObject



66
67
68
69
70
71
72
# File 'lib/git/story/semaphore.rb', line 66

def branch_history
  if branch_history_url
    self.class.get(branch_history_url, debug: debug)&.builds
  else
    []
  end
end

#building?Boolean



34
35
36
# File 'lib/git/story/semaphore.rb', line 34

def building?
  !started_at.nil?
end

#canceled?Boolean



46
47
48
# File 'lib/git/story/semaphore.rb', line 46

def canceled?
  result == 'canceled'
end

#duration(time = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/git/story/semaphore.rb', line 15

def duration(time = nil)
  unless time
    if finished_at.nil?
      time = Time.now
    else
      time = Time.parse(finished_at)
    end
  end
  if started_at
    Tins::Duration.new(time - Time.parse(started_at))
  else
    Tins::Duration.new(0)
  end
end

#entity_nameObject



62
63
64
# File 'lib/git/story/semaphore.rb', line 62

def entity_name
  branch_name || server_name
end

#entity_urlObject



58
59
60
# File 'lib/git/story/semaphore.rb', line 58

def entity_url
  server_html_url || build_url
end

#estimated_durationObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/git/story/semaphore.rb', line 74

def estimated_duration
  if ed = super
    ed
  else
    times = branch_history.select(&:passed?).map { |b|
      Time.parse(b.finished_at) - Time.parse(b.started_at)
    }
    if times.empty?
      duration
    else
      times.sum / times.size
    end.to_f
  end
end

#failed?Boolean



42
43
44
# File 'lib/git/story/semaphore.rb', line 42

def failed?
  result == 'failed'
end

#finished?Boolean



50
51
52
# File 'lib/git/story/semaphore.rb', line 50

def finished?
  finished_at.blank?
end

#infobar_styleObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/git/story/semaphore.rb', line 89

def infobar_style
  case
  when passed?, pending?
    {
      done_fg_color: '#005f00',
      done_bg_color: '#00d700',
      todo_fg_color: '#00d700',
      todo_bg_color: '#005f00',
    }
  else
    {
      done_fg_color: '#5f0000',
      done_bg_color: '#d70000',
      todo_fg_color: '#d70000',
      todo_bg_color: '#5f0000',
    }
  end
end

#passed?Boolean



38
39
40
# File 'lib/git/story/semaphore.rb', line 38

def passed?
  result == 'passed'
end

#pending?Boolean



30
31
32
# File 'lib/git/story/semaphore.rb', line 30

def pending?
  result == 'pending'
end

#sha1Object



54
55
56
# File 'lib/git/story/semaphore.rb', line 54

def sha1
  commit.id[0,10]
end

#to_sObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/git/story/semaphore.rb', line 108

def to_s
  r = case
      when pending? && building?
        "#{entity_name} ##{sha1} building for #{duration(Time.now)}".yellow.bold
      when pending?
        "#{entity_name} ##{sha1} pending at the moment".yellow
      when passed?
        "#{entity_name} ##{sha1} passed after #{duration}".green
      when failed?
        "#{entity_name} ##{sha1} failed after #{duration}".red
      else
        "#{entity_name} ##{sha1} in state #{result}".blue
      end
  r = StringIO.new(r)
  duration_seconds = duration.to_f.to_i
  if passed? || failed?
    total_seconds = duration_seconds
  else
    total_seconds = estimated_duration.to_i
  end
  Infobar(
    current: duration_seconds,
    total: total_seconds,
    message: ' %l %c/%t seconds ',
    style: infobar_style,
    output: r
  ).update
  r <<
    "\n  Semaphore: #{entity_url}" <<
    "\n  Commit: #{commit.url}\n#{commit.message&.gsub(/^/, " " * 10)&.color(33)}" <<
    "\n  Authored: #{(commit.author_name + ' <' + commit.author_email + ?>).bold} @#{commit.timestamp}"
  r.tap(&:rewind).read
end