brainiac-fizzy 0.0.26 → 0.0.27

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7b9d6e5b3e6e6ea9306dc6fd9ce800dffc01e46f99b05f2861e0acf096793fe5
4
- data.tar.gz: ec15a0b6f7a498de7662b8bd662fb519ae45f8a14d66e5912cf6dd540cc3726b
3
+ metadata.gz: dabc51c154f2caad4353e83a1c837a961bf1df27139ede86181929c8fa28290d
4
+ data.tar.gz: 6edde302d45dfe3628606cd005ca0ea6e3964d004bc6fe0c4c93be2d1590800d
5
5
  SHA512:
6
- metadata.gz: 51284c7c4cca09f1bde28d3589fc7c17b29314e898f4de8bd7345fa1fd930baf6501ef47e42a241b40f17baede635780676154e7c6f456a70ea6f1d9902da59b
7
- data.tar.gz: 868fc4f7e922d67138125385c486f87bc221766ddadf45e640ad880ee57914f04a772bdd3b688087c505a66cfa59f7643e1134f5731eb0ef67f3e42b6f80a700
6
+ metadata.gz: ac4ce9b1192051ce0d748d13c1ecdc834935bc503513fb101486460cfc478046749475d5eaa7cfff32252ba34e2be65efad10f3a9aecd6d8b1bdf2aa0c10cd33
7
+ data.tar.gz: 24afb9c9f9af9f481be0df59afeb94546e49546033d40190cee639fb17989b90dc5011f1c7d911daea9d21be1d7c3707e4180cb4be719220aad029a91c4268d1
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Brainiac
4
+ module Plugins
5
+ module Fizzy
6
+ # Derives the public URL of an ephemeral Belt environment from its
7
+ # terraform.tfvars. Kept separate from Helpers so the pure URL/HCL logic
8
+ # stays small and independently testable.
9
+ module EnvUrl
10
+ module_function
11
+
12
+ # URL of the ephemeral Belt environment for a card, if one is active.
13
+ # The env name is `fizzy-<card_number>`; the public URL is derived from the
14
+ # env's terraform.tfvars, matching the frontend_urls convention in the Belt
15
+ # infra module:
16
+ # parent set -> https://<env>.<parent>.<domain>
17
+ # prod -> https://<domain>
18
+ # otherwise -> https://<env>.<domain>
19
+ def for_card(card_number)
20
+ return nil unless defined?(BeltConfig)
21
+
22
+ env_name = BeltConfig.ephemeral_env_for_card(card_number)
23
+ return nil unless BeltConfig.ephemeral_env?(env_name)
24
+
25
+ info = BeltConfig.respond_to?(:ephemeral_env_info) ? BeltConfig.ephemeral_env_info(env_name) : nil
26
+ worktree = info && info["worktree"]
27
+ return nil unless worktree
28
+
29
+ tfvars = File.join(worktree, "infrastructure", env_name.to_s, "terraform.tfvars")
30
+ return nil unless File.exist?(tfvars)
31
+
32
+ from_tfvars(tfvars)
33
+ rescue StandardError => e
34
+ LOG.warn "[Fizzy] Could not detect env URL for card ##{card_number}: #{e.message}" if defined?(LOG)
35
+ nil
36
+ end
37
+
38
+ def from_tfvars(tfvars_path)
39
+ vars = parse(File.read(tfvars_path))
40
+ domain = vars["domain"].to_s
41
+ return nil if domain.empty?
42
+
43
+ environment = vars["environment"].to_s
44
+ parent = vars["parent_environment"].to_s
45
+
46
+ host =
47
+ if !parent.empty?
48
+ "#{environment}.#{parent}.#{domain}"
49
+ elsif environment == "prod"
50
+ domain
51
+ else
52
+ "#{environment}.#{domain}"
53
+ end
54
+
55
+ "https://#{host}"
56
+ end
57
+
58
+ # Minimal HCL parser for `key = "value"` lines in terraform.tfvars.
59
+ def parse(contents)
60
+ vars = {}
61
+ contents.each_line do |line|
62
+ stripped = line.strip
63
+ next if stripped.empty? || stripped.start_with?("#")
64
+
65
+ match = stripped.match(/\A(\w+)\s*=\s*"([^"]*)"/)
66
+ vars[match[1]] = match[2] if match
67
+ end
68
+ vars
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -173,14 +173,10 @@ module Brainiac
173
173
 
174
174
  # Append footer if applicable
175
175
  body = last_agent_comment.dig("body", "html") || ""
176
- unless body.include?("<em>Branch:")
176
+ unless footer_already_present?(body)
177
177
  branch = detect_branch_from_comment(body, card_number)
178
178
  if branch
179
- pr_url = detect_pr_url(branch, project_config)
180
- footer = "<p><em>Branch: <code>#{branch}</code>"
181
- footer += " | <a href=\"#{pr_url}\">PR</a>" if pr_url
182
- footer += "</em></p>"
183
-
179
+ footer = build_comment_footer(branch, card_number, project_config)
184
180
  updated_body = body + footer
185
181
  run_cmd("fizzy", "comment", "update", last_agent_comment["id"], "--card", card_number.to_s,
186
182
  "--body", updated_body, chdir: repo_path, env: env)
@@ -331,8 +327,51 @@ module Brainiac
331
327
  repo = project_config["github_repo"]
332
328
  return nil unless repo
333
329
 
330
+ # Prefer the existing open PR for this branch. `pull/new/<branch>` only
331
+ # opens the "create PR" page, which is wrong once a PR already exists.
332
+ existing = existing_pr_url(branch, repo, project_config["repo_path"])
333
+ return existing if existing
334
+
334
335
  "https://github.com/#{repo}/pull/new/#{branch}"
335
336
  end
337
+
338
+ # Look up the URL of an already-open PR for `branch` via the gh CLI.
339
+ # Returns nil if gh fails or no PR exists (caller falls back to new-PR link).
340
+ def existing_pr_url(branch, repo, repo_path)
341
+ output = run_cmd("gh", "pr", "view", branch, "--repo", repo, "--json", "url",
342
+ "--jq", ".url", chdir: repo_path)
343
+ url = output.to_s.strip
344
+ url.empty? ? nil : url
345
+ rescue StandardError
346
+ nil
347
+ end
348
+
349
+ # True if the comment body already carries a "Branch:" footer/label, in
350
+ # either the footer format (`<em>Branch:`) or the agent-authored format
351
+ # (`<strong>Branch:</strong>`). Prevents appending a duplicate footer.
352
+ def footer_already_present?(body)
353
+ body.include?("<em>Branch:") ||
354
+ body.match?(%r{<strong>\s*Branch:\s*</strong>}i) ||
355
+ body.match?(/(^|>)\s*Branch:\s*</)
356
+ end
357
+
358
+ # Build the "<em>Branch: <code>...</code> | PR | Env</em>" footer.
359
+ # Includes the ephemeral env link when the card has an active env.
360
+ def build_comment_footer(branch, card_number, project_config)
361
+ pr_url = detect_pr_url(branch, project_config)
362
+ env_url = detect_env_url(card_number)
363
+
364
+ footer = "<p><em>Branch: <code>#{branch}</code>"
365
+ footer += " | <a href=\"#{pr_url}\">PR</a>" if pr_url
366
+ footer += " | <a href=\"#{env_url}\">Env</a>" if env_url
367
+ footer += "</em></p>"
368
+ footer
369
+ end
370
+
371
+ # URL of the ephemeral Belt environment for this card, if one is active.
372
+ def detect_env_url(card_number)
373
+ EnvUrl.for_card(card_number)
374
+ end
336
375
  end
337
376
  end
338
377
  end
@@ -3,7 +3,7 @@
3
3
  module Brainiac
4
4
  module Plugins
5
5
  module Fizzy
6
- VERSION = "0.0.26"
6
+ VERSION = "0.0.27"
7
7
  end
8
8
  end
9
9
  end
@@ -3,6 +3,7 @@
3
3
  require_relative "fizzy/version"
4
4
  require_relative "fizzy/metadata"
5
5
  require_relative "fizzy/config"
6
+ require_relative "fizzy/env_url"
6
7
  require_relative "fizzy/helpers"
7
8
  require_relative "fizzy/prompts"
8
9
  require_relative "fizzy/planning"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brainiac-fizzy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.26
4
+ version: 0.0.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Davis
@@ -92,6 +92,7 @@ files:
92
92
  - lib/brainiac/plugins/fizzy/cli.rb
93
93
  - lib/brainiac/plugins/fizzy/config.rb
94
94
  - lib/brainiac/plugins/fizzy/delegators.rb
95
+ - lib/brainiac/plugins/fizzy/env_url.rb
95
96
  - lib/brainiac/plugins/fizzy/handlers/assignment.rb
96
97
  - lib/brainiac/plugins/fizzy/handlers/card_index.rb
97
98
  - lib/brainiac/plugins/fizzy/handlers/comments.rb