brainiac-github 0.3.3 → 0.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +39 -13
- data/lib/brainiac/plugins/github/handler.rb +73 -16
- data/lib/brainiac/plugins/github/version.rb +1 -1
- data/lib/brainiac/plugins/github.rb +2 -1
- metadata +3 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 460a875b7a10748b20c7c446ca8c1ea3e1e7210a4c7f83582530fb02b5d2979e
|
|
4
|
+
data.tar.gz: 7a25fe851195a7b85eab61803ae1e7befaecf98192bf5b741ec1a1b40b04c7f5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5ef1cdca60d2f6b38891954d4ceeb548ac362c1e3f8d872e4a3d359124ab8e7581f22ec011b6283e73f893d332ede1d49d4af91e3a6384861e20c66c8e5b34e3
|
|
7
|
+
data.tar.gz: 7066eab52546b19d6809dcfcab22af44e26a88f32fd2b28f43c11c490aef835eceac48310ea3acaeafcf00d77fef71c30a17fba94e05e68c59737c4741083850
|
data/README.md
CHANGED
|
@@ -6,9 +6,11 @@ GitHub webhook plugin for [Brainiac](https://github.com/stowzilla/brainiac) —
|
|
|
6
6
|
|
|
7
7
|
- **PR Reviews** — dispatches agents to address review feedback
|
|
8
8
|
- **PR Comments** — routes PR conversation to the assigned agent
|
|
9
|
+
- **PR Opens / Pushes** — tracks new PRs against work items; redeploys ephemeral envs and emits hooks on new pushes
|
|
9
10
|
- **PR Merges** — cleans up worktrees, emits hooks for card management plugins
|
|
10
|
-
- **Workflow Runs** —
|
|
11
|
-
- **Issues** — logs new issues
|
|
11
|
+
- **Workflow Runs** — matches workflows by name: `Deploy to Production` failure → notify; `Deploy to UAT` success → notify; `Deploy to Production` success → emits `:production_deployed`
|
|
12
|
+
- **Issues** — logs new issues (no dispatch)
|
|
13
|
+
- **Agent crashes** — posts a crash comment on the PR the agent was working on
|
|
12
14
|
|
|
13
15
|
## Installation
|
|
14
16
|
|
|
@@ -17,9 +19,12 @@ brainiac install github
|
|
|
17
19
|
brainiac restart
|
|
18
20
|
```
|
|
19
21
|
|
|
22
|
+
Install creates `~/.brainiac/github.json` from `templates/github.json.example`
|
|
23
|
+
(or run `brainiac github setup` to create it later).
|
|
24
|
+
|
|
20
25
|
## Configuration
|
|
21
26
|
|
|
22
|
-
Config lives at `~/.brainiac/github.json
|
|
27
|
+
Config lives at `~/.brainiac/github.json` (under `$BRAINIAC_DIR` if set):
|
|
23
28
|
|
|
24
29
|
```json
|
|
25
30
|
{
|
|
@@ -110,7 +115,10 @@ identity — so the webhook-receiver app doubles as the brainiac agent's identit
|
|
|
110
115
|
|
|
111
116
|
#### Step 2: Create Agent Identity Apps (One Per Agent)
|
|
112
117
|
|
|
113
|
-
For each agent that should have its own avatar on PR comments
|
|
118
|
+
For each agent that should have its own avatar on PR comments. This is still
|
|
119
|
+
manual: `brainiac agent create` (in Brainiac core) creates the agent, but this
|
|
120
|
+
plugin has no `on_agent_created` hook, so create the app and add its `apps` entry
|
|
121
|
+
yourself. `github.json` is re-read when it changes, so no restart is needed.
|
|
114
122
|
|
|
115
123
|
1. Go to **Settings → Developer settings → GitHub Apps → New GitHub App**
|
|
116
124
|
2. Set the following:
|
|
@@ -166,7 +174,21 @@ For each agent that should have its own avatar on PR comments:
|
|
|
166
174
|
```
|
|
167
175
|
|
|
168
176
|
Each key in `apps` matches the agent's registry key (lowercase). The
|
|
169
|
-
`installations` hash maps account/org names to their Installation IDs
|
|
177
|
+
`installations` hash maps account/org names to their Installation IDs (a flat
|
|
178
|
+
`installation_id` also works for a single account).
|
|
179
|
+
|
|
180
|
+
#### Single-App Mode
|
|
181
|
+
|
|
182
|
+
Instead of `apps`, you can configure one shared app under `app` (the template's
|
|
183
|
+
default shape). It's used for any agent without an `apps` entry:
|
|
184
|
+
|
|
185
|
+
```json
|
|
186
|
+
"app": {
|
|
187
|
+
"id": "100000",
|
|
188
|
+
"private_key_path": "~/.brainiac/github-app-private-key.pem",
|
|
189
|
+
"installation_id": "11111111"
|
|
190
|
+
}
|
|
191
|
+
```
|
|
170
192
|
|
|
171
193
|
#### Private Key Details
|
|
172
194
|
|
|
@@ -189,12 +211,14 @@ commit `.pem` files to version control.
|
|
|
189
211
|
|
|
190
212
|
#### Fallback
|
|
191
213
|
|
|
192
|
-
If no app credentials are configured for an agent
|
|
193
|
-
using the `gh` CLI (which
|
|
214
|
+
If no app credentials are configured for an agent (no `apps` entry, `app`
|
|
215
|
+
section, or env vars), the plugin falls back to using the `gh` CLI (which
|
|
216
|
+
authenticates as your personal GitHub account).
|
|
194
217
|
|
|
195
218
|
### Environment Variables
|
|
196
219
|
|
|
197
|
-
|
|
220
|
+
Used when the config file doesn't set the value. The `GITHUB_APP_*` variables
|
|
221
|
+
describe one shared app and are the last fallback after `apps.<agent>` and `app`:
|
|
198
222
|
|
|
199
223
|
- `GITHUB_WEBHOOK_SECRET` — webhook signature secret
|
|
200
224
|
- `GITHUB_APP_ID` — GitHub App ID
|
|
@@ -215,19 +239,21 @@ avoid duplicate event deliveries.
|
|
|
215
239
|
## CLI
|
|
216
240
|
|
|
217
241
|
```bash
|
|
218
|
-
brainiac github setup # Create config file from template
|
|
219
|
-
brainiac github config # Show
|
|
220
|
-
brainiac github status # Check
|
|
242
|
+
brainiac github setup # Create config file from template (if missing)
|
|
243
|
+
brainiac github config # Show webhook secret, repos, and shared `app` credentials
|
|
244
|
+
brainiac github status # Check the Brainiac server is up (POST /github active)
|
|
221
245
|
```
|
|
222
246
|
|
|
223
247
|
## Hooks Emitted
|
|
224
248
|
|
|
225
249
|
| Hook | When | Payload |
|
|
226
250
|
|------|------|---------|
|
|
251
|
+
| `:pr_opened` | PR opened on a tracked work item branch | card_number, pr_number, repo_name, branch, base_branch, ... |
|
|
227
252
|
| `:pr_merged` | PR merged to default branch | card_number, pr_url, project_key, ... |
|
|
253
|
+
| `:pr_merged_to_branch` | Any merge of a `fizzy-<n>-…` branch (incl. epic branches) | card_number, branch, base_branch, pr_url, ... |
|
|
228
254
|
| `:pr_review_received` | Review submitted | card_number, reviewer, agent_name, ... |
|
|
229
|
-
| `:pr_synchronized` |
|
|
230
|
-
| `:production_deployed` | Deploy workflow succeeds | project_key, project_config |
|
|
255
|
+
| `:pr_synchronized` | New commits pushed to a PR | card_number, worktree, branch, ... |
|
|
256
|
+
| `:production_deployed` | `Deploy to Production` workflow succeeds | project_key, project_config |
|
|
231
257
|
|
|
232
258
|
## Development
|
|
233
259
|
|
|
@@ -4,6 +4,17 @@ module Brainiac
|
|
|
4
4
|
module Plugins
|
|
5
5
|
module Github
|
|
6
6
|
module Handler
|
|
7
|
+
# Marker that starts every crash-notification comment (see register_crash_handler!).
|
|
8
|
+
CRASH_COMMENT_PREFIX = "💥"
|
|
9
|
+
|
|
10
|
+
# Circuit breaker: max comment dispatches per (repo, PR, agent) within the window.
|
|
11
|
+
# Stops any comment -> dispatch -> crash -> comment feedback loop, regardless of
|
|
12
|
+
# who posted the comment or what it says.
|
|
13
|
+
COMMENT_DISPATCH_LIMIT = 4
|
|
14
|
+
COMMENT_DISPATCH_WINDOW = 120 # seconds
|
|
15
|
+
COMMENT_DISPATCH_LOCK = Mutex.new
|
|
16
|
+
COMMENT_DISPATCH_TIMES = Hash.new { |h, k| h[k] = [] }
|
|
17
|
+
|
|
7
18
|
class << self
|
|
8
19
|
def handle_pr_merged(payload)
|
|
9
20
|
pr = payload["pull_request"]
|
|
@@ -254,20 +265,8 @@ module Brainiac
|
|
|
254
265
|
comment_user_type = comment.dig("user", "type")
|
|
255
266
|
repo_name = payload.dig("repository", "full_name")
|
|
256
267
|
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return [200, { status: "ignored", reason: "not a PR comment" }.to_json]
|
|
260
|
-
end
|
|
261
|
-
|
|
262
|
-
# If the PR was opened by a remote agent bot (not local to this machine),
|
|
263
|
-
# skip dispatch — the other machine that owns the agent will handle it.
|
|
264
|
-
pr_author = issue.dig("user", "login")
|
|
265
|
-
pr_author_type = issue.dig("user", "type")
|
|
266
|
-
unless pr_author_owned_locally?(pr_author, pr_author_type)
|
|
267
|
-
pr_number = issue["number"]
|
|
268
|
-
LOG.info "[GitHub] PR ##{pr_number} owned by remote agent (#{pr_author}) — skipping dispatch"
|
|
269
|
-
return [200, { status: "ignored", reason: "PR owned by remote agent" }.to_json]
|
|
270
|
-
end
|
|
268
|
+
rejection = issue_comment_rejection(issue, comment_body)
|
|
269
|
+
return rejection if rejection
|
|
271
270
|
|
|
272
271
|
project_result = identify_project_by_repo(repo_name)
|
|
273
272
|
unless project_result
|
|
@@ -301,6 +300,12 @@ module Brainiac
|
|
|
301
300
|
bot_ignore_reason = bot_comment_ignore_reason(comment_user, comment_user_type, agent_name, pr_number)
|
|
302
301
|
return bot_ignore_reason if bot_ignore_reason
|
|
303
302
|
|
|
303
|
+
if comment_dispatch_throttled?(repo_name, pr_number, agent_name)
|
|
304
|
+
LOG.warn "[GitHub] PR ##{pr_number}: >#{COMMENT_DISPATCH_LIMIT} dispatches for #{agent_name} in " \
|
|
305
|
+
"#{COMMENT_DISPATCH_WINDOW}s — possible loop, ignoring comment"
|
|
306
|
+
return [200, { status: "ignored", reason: "dispatch rate limit" }.to_json]
|
|
307
|
+
end
|
|
308
|
+
|
|
304
309
|
branch = fetch_pr_branch(repo_name, pr_number, agent_name, project_config)
|
|
305
310
|
result = find_work_item_by_branch(branch)
|
|
306
311
|
|
|
@@ -421,6 +426,42 @@ module Brainiac
|
|
|
421
426
|
pr_author_owned_locally?(pr_author, pr_author_type)
|
|
422
427
|
end
|
|
423
428
|
|
|
429
|
+
# Early-exit guards for issue_comment events. Returns a Rack-style response
|
|
430
|
+
# array when the comment should be ignored, or nil to continue dispatch.
|
|
431
|
+
def issue_comment_rejection(issue, comment_body)
|
|
432
|
+
unless issue["pull_request"]
|
|
433
|
+
LOG.info "Issue comment on non-PR issue ##{issue["number"]}, ignoring"
|
|
434
|
+
return [200, { status: "ignored", reason: "not a PR comment" }.to_json]
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
# If the PR was opened by a remote agent bot (not local to this machine),
|
|
438
|
+
# skip dispatch — the other machine that owns the agent will handle it.
|
|
439
|
+
pr_author = issue.dig("user", "login")
|
|
440
|
+
pr_author_type = issue.dig("user", "type")
|
|
441
|
+
unless pr_author_owned_locally?(pr_author, pr_author_type)
|
|
442
|
+
LOG.info "[GitHub] PR ##{issue["number"]} owned by remote agent (#{pr_author}) — skipping dispatch"
|
|
443
|
+
return [200, { status: "ignored", reason: "PR owned by remote agent" }.to_json]
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# Crash notifications are posted via `gh` as the human's account when the app
|
|
447
|
+
# client isn't configured, so the bot-login checks below can't catch them.
|
|
448
|
+
# Without this guard each crash comment re-dispatches the agent, which crashes
|
|
449
|
+
# again, forever.
|
|
450
|
+
if crash_notification_comment?(comment_body)
|
|
451
|
+
LOG.info "[GitHub] Ignoring crash notification comment on PR ##{issue["number"]}"
|
|
452
|
+
return [200, { status: "ignored", reason: "crash notification" }.to_json]
|
|
453
|
+
end
|
|
454
|
+
|
|
455
|
+
nil
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
# True when a comment is a crash notification posted by the crash handler.
|
|
459
|
+
# These are posted via `gh` as the human account when no app client is
|
|
460
|
+
# configured, so the bot-login checks can't catch them — match on the prefix.
|
|
461
|
+
def crash_notification_comment?(comment_body)
|
|
462
|
+
comment_body.to_s.lstrip.start_with?(CRASH_COMMENT_PREFIX)
|
|
463
|
+
end
|
|
464
|
+
|
|
424
465
|
# Check PR ownership given the author login and account type.
|
|
425
466
|
# Used by both pull_request_review (has full PR object) and issue_comment
|
|
426
467
|
# (has issue.user which is the PR author on PR-linked issues).
|
|
@@ -631,6 +672,20 @@ module Brainiac
|
|
|
631
672
|
# 1. Self-triggered: bot matches the dispatching agent (prevents loops)
|
|
632
673
|
# 2. Remote agent: bot matches a known agent that isn't local (other machine handles it)
|
|
633
674
|
# Allows: cross-agent LOCAL bot comments (e.g. GLaDOS commenting on Galen's PR)
|
|
675
|
+
# Records a dispatch attempt; true when the (repo, PR, agent) is over the limit.
|
|
676
|
+
def comment_dispatch_throttled?(repo_name, pr_number, agent_name)
|
|
677
|
+
key = [repo_name, pr_number, agent_name.to_s.downcase]
|
|
678
|
+
now = Time.now.to_f
|
|
679
|
+
COMMENT_DISPATCH_LOCK.synchronize do
|
|
680
|
+
times = COMMENT_DISPATCH_TIMES[key]
|
|
681
|
+
times.reject! { |t| now - t > COMMENT_DISPATCH_WINDOW }
|
|
682
|
+
return true if times.size >= COMMENT_DISPATCH_LIMIT
|
|
683
|
+
|
|
684
|
+
times << now
|
|
685
|
+
false
|
|
686
|
+
end
|
|
687
|
+
end
|
|
688
|
+
|
|
634
689
|
def bot_comment_ignore_reason(comment_user, comment_user_type, agent_name, pr_number)
|
|
635
690
|
return nil unless comment_user_type == "Bot"
|
|
636
691
|
|
|
@@ -755,7 +810,8 @@ module Brainiac
|
|
|
755
810
|
pid, log_file = run_agent(prompt, **run_agent_opts)
|
|
756
811
|
return unless pid
|
|
757
812
|
|
|
758
|
-
register_session(card_key, pid, log_file: log_file, agent_name: agent_name
|
|
813
|
+
register_session(card_key, pid, log_file: log_file, agent_name: agent_name,
|
|
814
|
+
title: "PR ##{pr_number}: #{comment_body}")
|
|
759
815
|
end
|
|
760
816
|
|
|
761
817
|
def dispatch_pr_review(card_number, card_key, card_info, pr_number, review, reviewer,
|
|
@@ -807,7 +863,8 @@ module Brainiac
|
|
|
807
863
|
pid, log_file = run_agent(prompt, **run_agent_opts)
|
|
808
864
|
return unless pid
|
|
809
865
|
|
|
810
|
-
register_session(card_key, pid, log_file: log_file, agent_name: agent_name
|
|
866
|
+
register_session(card_key, pid, log_file: log_file, agent_name: agent_name,
|
|
867
|
+
title: "PR ##{pr_number} review: #{review["body"]}")
|
|
811
868
|
end
|
|
812
869
|
|
|
813
870
|
# run_agent options for the CLI provider, model, effort and profile of a dispatch.
|
|
@@ -67,7 +67,8 @@ module Brainiac
|
|
|
67
67
|
agent_key = ctx[:agent_key]
|
|
68
68
|
snippet = ctx[:snippet]
|
|
69
69
|
snippet_block = snippet ? "\n```\n#{snippet[-1500..]}\n```" : ""
|
|
70
|
-
comment_body = "
|
|
70
|
+
comment_body = "#{Handler::CRASH_COMMENT_PREFIX} **#{agent_display} crashed** " \
|
|
71
|
+
"(exit code #{ctx[:exit_status]})\n\nLog: `#{ctx[:log_file]}`#{snippet_block}"
|
|
71
72
|
|
|
72
73
|
begin
|
|
73
74
|
if AppClient.configured?(agent_key)
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: brainiac-github
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.3.
|
|
4
|
+
version: 0.3.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andy Davis
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 0.
|
|
18
|
+
version: 0.7.1
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 0.
|
|
25
|
+
version: 0.7.1
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: jwt
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|