Class: CollavreGithub::GithubPrChannel

Inherits:
Collavre::Channel
  • Object
show all
Defined in:
app/models/collavre_github/github_pr_channel.rb

Constant Summary collapse

PR_STATES =

PR lifecycle state used by the chip badge color. Defaults to "open" so freshly attached channels render the green badge before any close event has been received. Persisted in config to avoid a schema change for a channel-subtype-specific concern.

%w[open merged closed_without_merge].freeze

Instance Method Summary collapse

Instance Method Details

#attached_messageObject



61
62
63
64
65
66
67
68
# File 'app/models/collavre_github/github_pr_channel.rb', line 61

def attached_message
  Collavre::Channel::InjectedMessage.new(
    speaker: channel_bot_user,
    message: t("attached_message", label: label, url: pr_url),
    label: label,
    link: pr_url
  )
end

#badge_stateObject



32
33
34
# File 'app/models/collavre_github/github_pr_channel.rb', line 32

def badge_state
  pr_state
end

#badge_titleObject



36
37
38
# File 'app/models/collavre_github/github_pr_channel.rb', line 36

def badge_title
  I18n.t("collavre_github.channel.pr.badge.#{pr_state}", default: pr_state.to_s)
end

#default_labelObject

Chip fallbacks: derived directly from config so the chip can render the full "PR #N" + URL immediately on attach, without waiting for the first webhook event to populate latest_label / latest_link.



24
25
26
# File 'app/models/collavre_github/github_pr_channel.rb', line 24

def default_label
  label
end


28
29
30
# File 'app/models/collavre_github/github_pr_channel.rb', line 28

def default_link
  pr_url
end

#handle(event:, payload:) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/models/collavre_github/github_pr_channel.rb', line 79

def handle(event:, payload:)
  case event
  when "issue_comment"
    handle_issue_comment(payload)
  when "pull_request_review_comment"
    handle_review_comment(payload)
  when "pull_request_review"
    (payload)
  when "pull_request"
    handle_pull_request(payload)
  end
end

#labelObject



17
18
19
# File 'app/models/collavre_github/github_pr_channel.rb', line 17

def label
  t("label", number: pr_number)
end

#pr_numberObject



9
10
11
# File 'app/models/collavre_github/github_pr_channel.rb', line 9

def pr_number
  config["pr_number"].to_i
end

#pr_stateObject



46
47
48
49
# File 'app/models/collavre_github/github_pr_channel.rb', line 46

def pr_state
  state = config["pr_state"].to_s
  PR_STATES.include?(state) ? state : "open"
end

#pr_state=(value) ⇒ Object

Symmetric with the reader: refuse to persist values outside PR_STATES rather than silently downgrading to "open" at read time. Without this any caller that mistypes (e.g. "merged_") would corrupt the badge color with no error surfaced.

Raises:

  • (ArgumentError)


55
56
57
58
59
# File 'app/models/collavre_github/github_pr_channel.rb', line 55

def pr_state=(value)
  value = value.to_s
  raise ArgumentError, "Invalid pr_state: #{value.inspect}" unless PR_STATES.include?(value)
  self.config = config.merge("pr_state" => value)
end

#pr_urlObject



13
14
15
# File 'app/models/collavre_github/github_pr_channel.rb', line 13

def pr_url
  "https://github.com/#{repo_full_name}/pull/#{pr_number}"
end

#reopened_messageObject



70
71
72
73
74
75
76
77
# File 'app/models/collavre_github/github_pr_channel.rb', line 70

def reopened_message
  Collavre::Channel::InjectedMessage.new(
    speaker: channel_bot_user,
    message: t("reopened_message", label: label, url: pr_url),
    label: label,
    link: pr_url
  )
end

#repo_full_nameObject



5
6
7
# File 'app/models/collavre_github/github_pr_channel.rb', line 5

def repo_full_name
  config["repo_full_name"]
end