Module: Brainiac::Plugins::Github::Handler
- Defined in:
- lib/brainiac/plugins/github/handler.rb
Class Method Summary collapse
- .handle_issue_comment(payload) ⇒ Object
- .handle_issue_opened(payload) ⇒ Object
- .handle_pr_merged(payload) ⇒ Object
- .handle_pr_opened(payload) ⇒ Object
- .handle_pr_review_submitted(payload) ⇒ Object
- .handle_pr_synchronized(payload) ⇒ Object
- .handle_workflow_run(payload) ⇒ Object
Class Method Details
.handle_issue_comment(payload) ⇒ Object
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 |
# File 'lib/brainiac/plugins/github/handler.rb', line 132 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") 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"] pr_data = run_cmd("gh", "api", "/repos/#{repo_name}/pulls/#{pr_number}", "--jq", "{branch: .head.ref}", chdir: project_config["repo_path"]) branch = JSON.parse(pr_data)["branch"] result = find_work_item_by_branch(branch) if result _, card_info = result card_number = card_info["number"] worktree = card_info["worktree"] unless worktree && File.directory?(worktree) LOG.info "No active worktree for PR ##{pr_number}, ignoring comment" return [200, { status: "ignored", reason: "no active worktree" }.to_json] end card_key = "card-#{card_number}" else card_number = nil worktree = project_config["repo_path"] card_key = "pr-#{repo_name.tr("/", "-")}-#{pr_number}" end 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) [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.}" [500, { error: e. }.to_json] end |
.handle_issue_opened(payload) ⇒ Object
193 194 195 196 197 198 199 200 201 202 |
# File 'lib/brainiac/plugins/github/handler.rb', line 193 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 = card_info["number"] 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.}" [500, { error: e. }.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 130 |
# File 'lib/brainiac/plugins/github/handler.rb', line 84 def handle_pr_review_submitted(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 = card_info["number"] 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 card_key = "card-#{card_number}" else card_info = {} card_number = nil card_key = "pr-#{repo_name.tr("/", "-")}-#{pr_number}" end 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.}" [500, { error: e. }.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 = card_info["number"] 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.}" [500, { error: e. }.to_json] end |
.handle_workflow_run(payload) ⇒ Object
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/brainiac/plugins/github/handler.rb', line 204 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.}" [500, { error: e. }.to_json] end |