Module: Brainiac::Plugins::Github::Handler

Defined in:
lib/brainiac/plugins/github/handler.rb

Class Method Summary collapse

Class Method Details

.handle_issue_comment(payload) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/brainiac/plugins/github/handler.rb', line 131

def handle_issue_comment(payload)
  comment = payload["comment"]
  issue = payload["issue"]
  comment_body = comment["body"] || ""
  comment_id = comment["id"]
  comment_user = comment.dig("user", "login")
  comment_user_type = comment.dig("user", "type")
  repo_name = payload.dig("repository", "full_name")

  unless issue["pull_request"]
    LOG.info "Issue comment on non-PR issue ##{issue["number"]}, ignoring"
    return [200, { status: "ignored", reason: "not a PR comment" }.to_json]
  end

  project_result = identify_project_by_repo(repo_name)
  unless project_result
    LOG.info "No project found for GitHub repo #{repo_name}"
    return [200, { status: "ignored", reason: "no matching project" }.to_json]
  end

  project_key, project_config = project_result
  pr_number = issue["number"]
  agent_name = agent_name_for(project_config)

  # Mention detection: if the comment mentions a specific agent, dispatch
  # that agent instead of the project's default. This enables cross-agent
  # reviews on PRs.
  #
  # Supported syntax (to avoid tagging real GitHub users with @):
  #   "Threepio, review this"   — name + comma at the start
  #   "/ask Threepio review"    — /ask Name
  #   "/Threepio review this"   — /Name
  #   "@Threepio review this"   — @Name (legacy, works but tags real users)
  mentioned = detect_github_mention(comment_body)
  if mentioned && mentioned.downcase != agent_name.downcase && local_agent_names.include?(mentioned)
    LOG.info "[GitHub] Mention detected: #{mentioned} in PR comment (default agent: #{agent_name})"
    agent_name = mentioned
  end

  # Ignore comments from this project's own bot to prevent self-triggering.
  # Cross-agent bot comments (e.g. GLaDOS commenting on Galen's PR) are still processed.
  if comment_user_type == "Bot" && own_bot_comment?(comment_user, agent_name)
    LOG.info "Ignoring self-triggered bot comment from #{comment_user} (agent: #{agent_name})"
    return [200, { status: "ignored", reason: "self-triggered bot comment" }.to_json]
  end

  branch = fetch_pr_branch(repo_name, pr_number, agent_name, project_config)
  result = find_work_item_by_branch(branch)

  # If a work item exists and has an assigned agent, use that agent
  # instead of the project default. This ensures comments on a PR
  # route to whoever is actually working it (e.g. GLaDOS opened the PR,
  # so GLaDOS should handle follow-up comments — not the project's default agent).
  # Explicit mentions still take priority (already resolved above).
  if result && !mentioned
    _, card_info = result
    work_item_agent = card_info["agent"]
    if work_item_agent && work_item_agent.downcase != agent_name.downcase && local_agent_names.include?(work_item_agent)
      LOG.info "[GitHub] Work item agent override: #{work_item_agent} (project default: #{agent_name})"
      agent_name = work_item_agent
    end
  end

  card_number, worktree = resolve_comment_worktree(result, mentioned, agent_name, pr_number, project_config)
  return worktree if worktree.is_a?(Array)

  card_key = "pr-comment-#{repo_name.tr("/", "-")}-#{pr_number}-#{agent_name.downcase}"

  if session_active?(card_key)
    LOG.info "Skipping PR comment on #{card_key} — agent session already active"
    return [200, { status: "ignored", reason: "session already active" }.to_json]
  end

  card_context = card_number ? " for card ##{card_number}" : ""
  LOG.info "PR comment from #{comment_user} on PR ##{pr_number}#{card_context} (project: #{project_key})"
  dispatch_pr_comment(card_number, card_key, pr_number, comment_id, comment_user, comment_body,
                      repo_name, worktree, project_key, project_config, agent_name: agent_name)

  [200, { status: "processed", card: card_number, pr: pr_number, comment_id: comment_id, project: project_key }.to_json]
rescue StandardError => e
  LOG.error "Error handling PR comment: #{e.message}"
  [500, { error: e.message }.to_json]
end

.handle_issue_opened(payload) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'lib/brainiac/plugins/github/handler.rb', line 215

def handle_issue_opened(payload)
  issue = payload["issue"]
  issue_url = issue["html_url"]
  issue_title = issue["title"]
  issue_number = issue["number"]
  repo_name = payload.dig("repository", "full_name")

  LOG.info "New GitHub issue ##{issue_number} on #{repo_name}: #{issue_title} (#{issue_url})"
  [200, { status: "logged", issue: issue_number, title: issue_title, url: issue_url }.to_json]
end

.handle_pr_merged(payload) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/brainiac/plugins/github/handler.rb', line 8

def handle_pr_merged(payload)
  pr = payload["pull_request"]
  branch = pr.dig("head", "ref")
  base = pr.dig("base", "ref")
  pr_url = pr["html_url"]
  pr_title = pr["title"]
  repo_full_name = payload.dig("repository", "full_name")

  default_branch = payload.dig("repository", "default_branch") || "main"
  unless base == default_branch
    LOG.info "PR merged into #{base}, not #{default_branch} — ignoring"
    return [200, { status: "ignored", reason: "not merged into #{default_branch}" }.to_json]
  end

  project_result = identify_project_by_repo(repo_full_name)
  unless project_result
    LOG.info "No project found for GitHub repo #{repo_full_name}"
    return [200, { status: "ignored", reason: "no matching project" }.to_json]
  end

  project_key, project_config = project_result
  repo_path = project_config["repo_path"]

  result = find_work_item_by_branch(branch)
  unless result
    LOG.info "No card found for branch #{branch}"
    return [200, { status: "ignored", reason: "no matching card" }.to_json]
  end

  _internal_id, card_info = result
  card_number = extract_card_number(card_info)
  unless card_number
    LOG.warn "Card has no number — can't comment or move"
    return [200, { status: "ignored", reason: "card has no number" }.to_json]
  end

  LOG.info "PR merged into main for card ##{card_number} (project: #{project_key}): #{pr_url}"
  process_merged_pr(card_info, card_number, branch, pr, pr_url, pr_title, project_key, project_config, repo_path)

  [200, { status: "processed", card: card_number, pr: pr_url, action: "merged_to_uat", project: project_key }.to_json]
rescue StandardError => e
  LOG.error "Error handling merged PR: #{e.message}"
  [500, { error: e.message }.to_json]
end

.handle_pr_opened(payload) ⇒ Object



53
54
55
56
# File 'lib/brainiac/plugins/github/handler.rb', line 53

def handle_pr_opened(payload)
  track_pr_in_work_items(payload)
  [200, { status: "processed", action: "pr_tracked" }.to_json]
end

.handle_pr_review_submitted(payload) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/brainiac/plugins/github/handler.rb', line 84

def (payload)
  pr = payload["pull_request"]
  review = payload["review"]
  branch = pr.dig("head", "ref")
  pr_number = pr["number"]
  repo_name = payload.dig("repository", "full_name")
  review_state = review["state"]
  reviewer = review.dig("user", "login")

  unless %w[changes_requested commented].include?(review_state)
    return [200, { status: "ignored", reason: "review state: #{review_state}" }.to_json]
  end

  project_result = identify_project_by_repo(repo_name)
  return [200, { status: "ignored", reason: "no matching project" }.to_json] unless project_result

  project_key, project_config = project_result
  repo_path = project_config["repo_path"]

  result = find_work_item_by_branch(branch)

  if result
    _internal_id, card_info = result
    card_number = extract_card_number(card_info)
    unless card_number
      LOG.warn "Card has no number — can't dispatch review"
      return [200, { status: "ignored", reason: "card has no number" }.to_json]
    end
  else
    card_info = {}
    card_number = nil
  end
  card_key = "pr-review-#{repo_name.tr("/", "-")}-#{pr_number}"

  return [200, { status: "ignored", reason: "session already active" }.to_json] if session_active?(card_key)

  card_context = card_number ? " for card ##{card_number}" : ""
  LOG.info "PR review submitted by #{reviewer} on PR ##{pr_number}#{card_context} (project: #{project_key})"
  dispatch_pr_review(card_number, card_key, card_info, pr_number, review, reviewer,
                     repo_name, project_key, project_config, repo_path)

  [200, { status: "processed", card: card_number, pr: pr_number, reviewer: reviewer, project: project_key }.to_json]
rescue StandardError => e
  LOG.error "Error handling PR review: #{e.message}"
  [500, { error: e.message }.to_json]
end

.handle_pr_synchronized(payload) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/brainiac/plugins/github/handler.rb', line 58

def handle_pr_synchronized(payload)
  pr = payload["pull_request"]
  branch = pr.dig("head", "ref")

  result = find_work_item_by_branch(branch)
  return [200, { status: "ignored", reason: "no matching card" }.to_json] unless result

  _internal_id, card_info = result
  card_number = extract_card_number(card_info)
  worktree = card_info["worktree"]

  return [200, { status: "ignored", reason: "no worktree" }.to_json] unless worktree && File.directory?(worktree)

  results = Brainiac.emit(:pr_synchronized, card_number: card_number, card_info: card_info,
                                            worktree: worktree, pull_request: pr, branch: branch)

  if results.any?
    [200, { status: "processed", action: "pr_sync", card: card_number }.to_json]
  else
    [200, { status: "ignored", reason: "no deployment plugin" }.to_json]
  end
rescue StandardError => e
  LOG.error "[PR Sync] Error: #{e.message}"
  [500, { error: e.message }.to_json]
end

.handle_workflow_run(payload) ⇒ Object



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/brainiac/plugins/github/handler.rb', line 226

def handle_workflow_run(payload)
  workflow = payload["workflow_run"]
  workflow_name = workflow["name"]
  conclusion = workflow["conclusion"]
  repo_full_name = payload.dig("repository", "full_name")
  run_url = workflow["html_url"]

  if workflow_name == "Deploy to Production" && conclusion == "failure"
    project_key = identify_project_by_repo(repo_full_name)&.first || repo_full_name
    Notifications.send_workflow_failure(project_key, workflow_name, run_url)
    return [200, { status: "processed", action: "prod_deploy_failure_notified", project: project_key }.to_json]
  end

  if workflow_name == "Deploy to UAT" && conclusion == "success"
    project_key = identify_project_by_repo(repo_full_name)&.first || repo_full_name
    Notifications.send_uat_deploy(project_key)
    return [200, { status: "processed", action: "uat_deploy_notified", project: project_key }.to_json]
  end

  return [200, { status: "ignored", reason: "conclusion: #{conclusion}" }.to_json] unless conclusion == "success"
  return [200, { status: "ignored", reason: "workflow: #{workflow_name}" }.to_json] unless workflow_name == "Deploy to Production"

  project_result = identify_project_by_repo(repo_full_name)
  return [200, { status: "ignored", reason: "no matching project" }.to_json] unless project_result

  project_key, project_config = project_result
  close_uat_cards_after_deploy(project_key, project_config)
rescue StandardError => e
  LOG.error "Error handling workflow run: #{e.message}"
  [500, { error: e.message }.to_json]
end