Class: ChatNotifier::Messenger

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/chat_notifier/messenger.rb

Direct Known Subclasses

Failure

Defined Under Namespace

Modules: Debug Classes: Failure

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(summary:, app:, repository:, environment:) ⇒ Messenger

Returns a new instance of Messenger.



32
33
34
35
36
37
# File 'lib/chat_notifier/messenger.rb', line 32

def initialize(summary:, app:, repository:, environment:)
  @summary = summary
  @app = app
  @repository = repository
  @environment = environment
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



39
40
41
# File 'lib/chat_notifier/messenger.rb', line 39

def app
  @app
end

#environmentObject (readonly)

Returns the value of attribute environment.



39
40
41
# File 'lib/chat_notifier/messenger.rb', line 39

def environment
  @environment
end

#repositoryObject (readonly)

Returns the value of attribute repository.



39
40
41
# File 'lib/chat_notifier/messenger.rb', line 39

def repository
  @repository
end

#summaryObject (readonly)

Returns the value of attribute summary.



39
40
41
# File 'lib/chat_notifier/messenger.rb', line 39

def summary
  @summary
end

Class Method Details

.debug=(val) ⇒ Object



23
24
25
# File 'lib/chat_notifier/messenger.rb', line 23

def debug=(val)
  prepend Debug if val
end

.for(summary, repository:, environment:, app:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/chat_notifier/messenger.rb', line 10

def for(summary, repository:, environment:, app:)
  if summary.failed_examples.empty?
    self
  else
    Failure
  end.new(
    summary:,
    app:,
    repository:,
    environment:
  )
end

Instance Method Details

#bodyObject



53
# File 'lib/chat_notifier/messenger.rb', line 53

def body = ""

#digest(reports) ⇒ Object

Render a parent-message summary from the thread's status reports, keeping only the latest run's report per job. A pure function of the reports plus state shared across matrix jobs (app, sha, branch, test_run_url) — never the writer's own ruby version or success state — so any writer, in any order, converges on identical text.



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/chat_notifier/messenger.rb', line 80

def digest(reports)
  latest = latest_run(reports)
  parts = latest.sort_by { |report| report["job"].to_s }.map do |report|
    if report["status"] == "passed"
      "#{report["job"]}"
    else
      "#{report["job"]}#{report["failures"]}"
    end
  end
  prefix = resolved?(reports) ? "" : ":boom:"
  "#{prefix} #{app} #{sha} in #{branch} · #{parts.join(" · ")}\n#{environment.test_run_url}"
end

#failure?Boolean

Returns:

  • (Boolean)


73
# File 'lib/chat_notifier/messenger.rb', line 73

def failure? = !success?

#failuresObject



41
# File 'lib/chat_notifier/messenger.rb', line 41

def failures = summary.failed_examples

#identifierObject



55
56
57
# File 'lib/chat_notifier/messenger.rb', line 55

def identifier
  "#{app} #{ruby_version} #{sha}"
end

#ledeObject



51
# File 'lib/chat_notifier/messenger.rb', line 51

def lede = message

#messageObject



47
48
49
# File 'lib/chat_notifier/messenger.rb', line 47

def message
  "#{message_prefix} #{identifier} is OK on branch #{repository.link(branch)}"
end

#message_prefixObject



63
# File 'lib/chat_notifier/messenger.rb', line 63

def message_prefix = ":thumbsup:"

#resolved?(reports) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/chat_notifier/messenger.rb', line 93

def resolved?(reports)
  latest = latest_run(reports)
  !latest.empty? && latest.all? { |report| report["status"] == "passed" }
end

#status_reportObject



65
66
67
68
69
# File 'lib/chat_notifier/messenger.rb', line 65

def status_report
  # The payload field stays named run_id (persisted in existing threads)
  # but carries run_key so re-run attempts group as newer runs.
  {job: environment.job_identifier, status: "passed", failures: 0, run_id: environment.run_key}
end

#success?Boolean

Returns:

  • (Boolean)


71
# File 'lib/chat_notifier/messenger.rb', line 71

def success? = true

#thread_keyObject



59
60
61
# File 'lib/chat_notifier/messenger.rb', line 59

def thread_key
  "#{app}##{environment.pull_request_ref || branch}"
end

#to_hObject



98
99
100
101
102
# File 'lib/chat_notifier/messenger.rb', line 98

def to_h
  {
    text: message
  }
end

#to_hashObject



104
# File 'lib/chat_notifier/messenger.rb', line 104

def to_hash = to_h