brainiac-fizzy 0.0.43 → 0.0.45

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: 7a31ac01cdee77c3025a92097cece2c0f86d1fea3c479abcdeac306b09d7ef23
4
- data.tar.gz: 8efaa4aa4338bbc2f731986507a305850b507883fb8169b78fdc8333c97589f1
3
+ metadata.gz: b17e1dace17022716de710400df7e96a81e6162e3f24ef6de18d29e8b4f44708
4
+ data.tar.gz: 1797a9113aeb6bf305f7405f7b8f0e29c7a51ac2f777e7f1e72019f09a228ea4
5
5
  SHA512:
6
- metadata.gz: e3fced8d93c21f0c9600619fdb9f1fa996bbc98fb393695d1d0c6b39c66ba6a74a1f4c032f575e4416ffe5cfa0f1fd80f9be4df7bf237794cbbdd53c6f56d862
7
- data.tar.gz: c69760d16a0f0039a1eaab9daa1e30fd8346f43ce824ee031d6a1b2229ea636d3c01b46a3af987c0bd24ed6034bbddf0ea9ecc2a1afc312d5e75d36a4de68c7e
6
+ metadata.gz: 54fe307c208de599fcc2cfdd63d13d8490521c18d78e21b4d7ca3203ca998e9707cdc332d26fed23f8d624b44d5b5e54fe5192a1aab460b0fa71af645301d2af
7
+ data.tar.gz: 389eb3f9420e9cc34a2154f082fe99218b03b8dd78c860e642cf2aefcf3bbbc6e5c9ecd0fe59a7a3b1bc70bf98ee838c4e065987f262d971210e4e30f7f49b4e
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "fileutils"
4
+ require "json"
5
+ require "open3"
6
+
7
+ module Brainiac
8
+ module Plugins
9
+ module Fizzy
10
+ # Provisions a Fizzy user for a new brainiac agent (`brainiac agent create`
11
+ # / `brainiac agent setup`).
12
+ #
13
+ # Fizzy has no "create user" API and only issues access tokens in the web
14
+ # UI, so the manual part is: open the account's join link, join as the
15
+ # agent's email, and copy a personal access token. Given that token, the
16
+ # rest is automated: user id lookup, name + avatar, authorized_users, and
17
+ # FIZZY_TOKEN in agents.json.
18
+ module AgentSetup
19
+ class << self
20
+ def run(agent_key, entry)
21
+ display_name = entry["display_name"] || agent_key.capitalize
22
+ config = fizzy_config
23
+ return unless config
24
+
25
+ token = registry.dig(agent_key, "env", "FIZZY_TOKEN")
26
+ if token && authorized?(config, display_name)
27
+ puts " [Fizzy] ✓ #{display_name} already has a Fizzy user (skipping)"
28
+ return
29
+ end
30
+
31
+ token ||= prompt_for_token(display_name, entry["email"], config)
32
+ return unless token
33
+
34
+ user = identity_user(token)
35
+ unless user
36
+ puts " [Fizzy] ✗ Fizzy rejected that token — re-run: brainiac agent setup #{agent_key}"
37
+ return
38
+ end
39
+
40
+ update_profile(user["id"], display_name, entry["avatar_path"], token, config["admin_token"])
41
+ save_token(agent_key, token)
42
+ authorize(display_name, user["id"])
43
+ puts " [Fizzy] ✓ Saved FIZZY_TOKEN and added #{display_name} (#{user["id"]}) to authorized_users"
44
+ end
45
+
46
+ private
47
+
48
+ def prompt_for_token(display_name, email, config)
49
+ return nil unless $stdin.tty?
50
+
51
+ join = fizzy_json("account", "join-code-show", token: config["admin_token"])
52
+ puts ""
53
+ puts " [Fizzy] Create a Fizzy user for #{display_name}:"
54
+ puts " 1. Open in a private window: #{join&.dig("url") || "(couldn't fetch — Account settings → Invite link)"}"
55
+ puts " 2. Join as #{email || "the agent's email"}, then click the magic link from that inbox"
56
+ puts " 3. Profile → Personal access tokens → new token with write access → copy it"
57
+ puts " Name, avatar and brainiac config are set automatically."
58
+ print " Access token (blank to skip): "
59
+ token = read_secret
60
+ puts ""
61
+ token.nil? || token.empty? ? nil : token
62
+ end
63
+
64
+ def read_secret
65
+ require "io/console"
66
+ $stdin.noecho(&:gets)&.strip
67
+ rescue Errno::ENOTTY, NoMethodError
68
+ $stdin.gets&.strip
69
+ end
70
+
71
+ # The token owner's user record in the configured account.
72
+ def identity_user(token)
73
+ accounts = fizzy_json("identity", "show", token: token)&.dig("accounts") || []
74
+ account = fizzy_json("config", "show")&.dig("account")
75
+ match = accounts.find { |a| a["slug"].to_s.delete_prefix("/") == account.to_s } || accounts.first
76
+ match&.dig("user")
77
+ end
78
+
79
+ # Users can't always edit themselves (members vs admins), so fall back
80
+ # to the admin token from fizzy.json.
81
+ def update_profile(user_id, display_name, avatar_path, token, admin_token)
82
+ args = ["user", "update", user_id, "--name", display_name]
83
+ args += ["--avatar", avatar_path] if avatar_path && File.file?(avatar_path)
84
+ what = args.include?("--avatar") ? "name + avatar" : "name"
85
+
86
+ if run_fizzy(*args, token: token) || (admin_token && run_fizzy(*args, token: admin_token))
87
+ puts " [Fizzy] ✓ Set #{what}"
88
+ else
89
+ puts " [Fizzy] ⚠ Couldn't set #{what} — update the profile in Fizzy"
90
+ end
91
+ end
92
+
93
+ def fizzy_json(*, token: nil)
94
+ output = run_fizzy(*, "--quiet", token: token)
95
+ output && JSON.parse(output)
96
+ rescue JSON::ParserError
97
+ nil
98
+ end
99
+
100
+ def run_fizzy(*args, token: nil)
101
+ args += ["--token", token] if token
102
+ output, status = Open3.capture2("fizzy", *args, err: File::NULL)
103
+ status.success? ? output : nil
104
+ rescue Errno::ENOENT
105
+ nil
106
+ end
107
+
108
+ def brainiac_dir
109
+ ENV.fetch("BRAINIAC_DIR", File.join(Dir.home, ".brainiac"))
110
+ end
111
+
112
+ def registry_file = File.join(brainiac_dir, "agents.json")
113
+ def config_file = File.join(brainiac_dir, "fizzy.json")
114
+
115
+ def registry
116
+ File.exist?(registry_file) ? JSON.parse(File.read(registry_file)) : {}
117
+ end
118
+
119
+ def fizzy_config
120
+ File.exist?(config_file) ? JSON.parse(File.read(config_file)) : nil
121
+ end
122
+
123
+ def authorized?(config, display_name)
124
+ (config["authorized_users"] || []).any? { |u| u["name"]&.downcase == display_name.downcase }
125
+ end
126
+
127
+ def save_token(agent_key, token)
128
+ reg = registry
129
+ reg[agent_key] ||= {}
130
+ reg[agent_key]["env"] ||= {}
131
+ reg[agent_key]["env"]["FIZZY_TOKEN"] = token
132
+ FileUtils.mkdir_p(brainiac_dir)
133
+ File.write(registry_file, JSON.pretty_generate(reg))
134
+ end
135
+
136
+ def authorize(display_name, user_id)
137
+ config = fizzy_config
138
+ config["authorized_users"] ||= []
139
+ config["authorized_users"].reject! { |u| u["name"]&.downcase == display_name.downcase }
140
+ config["authorized_users"] << { "id" => user_id, "name" => display_name, "human" => false }
141
+ File.write(config_file, JSON.pretty_generate(config))
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end
147
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
+ require_relative "agent_setup"
4
5
 
5
6
  module Brainiac
6
7
  module Plugins
@@ -792,32 +793,10 @@ module Brainiac
792
793
  []
793
794
  end
794
795
 
795
- # Called by brainiac CLI after `agent create` — prompts for Fizzy user ID.
796
+ # Called by brainiac CLI after `agent create` / `agent setup` — provisions
797
+ # the agent's Fizzy user (see AgentSetup). Safe to re-run.
796
798
  def self.on_agent_created(agent_key, entry)
797
- return unless $stdin.tty?
798
-
799
- config_file = File.join(ENV.fetch("BRAINIAC_DIR", File.join(Dir.home, ".brainiac")), "fizzy.json")
800
- return unless File.exist?(config_file)
801
-
802
- display_name = entry["display_name"] || agent_key.capitalize
803
- puts ""
804
- puts " [Fizzy] Configure Fizzy for #{display_name}?"
805
- print " Fizzy user ID (or blank to skip): "
806
- user_id = $stdin.gets&.chomp
807
- return if user_id.nil? || user_id.empty?
808
-
809
- config = JSON.parse(File.read(config_file))
810
- config["authorized_users"] ||= []
811
-
812
- existing = config["authorized_users"].find { |u| u["id"] == user_id || u["name"]&.downcase == display_name.downcase }
813
- if existing
814
- puts " ✓ #{display_name} already in authorized_users (id: #{existing["id"]})"
815
- return
816
- end
817
-
818
- config["authorized_users"] << { "id" => user_id, "name" => display_name, "human" => false }
819
- File.write(config_file, JSON.pretty_generate(config))
820
- puts " ✓ Added #{display_name} (#{user_id}) to fizzy.json authorized_users"
799
+ AgentSetup.run(agent_key, entry)
821
800
  rescue JSON::ParserError => e
822
801
  puts " ⚠ Could not update fizzy.json: #{e.message}"
823
802
  end
@@ -245,22 +245,24 @@ module Brainiac
245
245
  tags = ctx[:tags] || []
246
246
  assign_to = ctx[:assign_to]
247
247
 
248
- # Resolve tag IDs
249
248
  agent_env = agent_env_for("Threepio")
250
249
  spawn_env = agent_env.empty? ? {} : agent_env
251
- tag_ids = resolve_tag_ids(tags, spawn_env)
252
250
 
253
- cmd = ["fizzy", "card", "create", "--board", board_id, "--title", title, "--description", description]
254
- cmd.push("--tag-ids", tag_ids.join(",")) unless tag_ids.empty?
255
-
256
- output, status = Open3.capture2e(spawn_env, *cmd)
257
- next nil unless status.success?
251
+ # fizzy 4 has no tag flag on create, so tags go on afterwards
252
+ output, status = Open3.capture2e(spawn_env, "fizzy", "card", "create", "--board", board_id,
253
+ "--title", title, "--description", description)
254
+ unless status.success?
255
+ LOG.error "[Fizzy] fizzy card create failed: #{output.strip[0, 300]}" if defined?(LOG)
256
+ next nil
257
+ end
258
258
 
259
259
  card_data = JSON.parse(output)
260
260
  card_number = card_data.dig("data", "number")
261
261
 
262
- # Assign if requested
263
- assign_card(card_number, assign_to, spawn_env) if card_number && assign_to
262
+ if card_number
263
+ tag_card(card_number, tags, spawn_env)
264
+ assign_card(card_number, assign_to, spawn_env) if assign_to
265
+ end
264
266
 
265
267
  { number: card_number, url: card_data.dig("data", "url"), title: title }
266
268
  rescue StandardError => e
@@ -416,17 +418,12 @@ module Brainiac
416
418
  closed
417
419
  end
418
420
 
419
- def resolve_tag_ids(tag_names, spawn_env)
420
- output, status = Open3.capture2e(spawn_env, "fizzy", "tag", "list", "--all")
421
- return [] unless status.success?
422
-
423
- all_tags = JSON.parse(output)["data"] || []
424
- tag_names.filter_map do |name|
425
- tag = all_tags.find { |t| t["title"].downcase == name.downcase }
426
- tag&.dig("id")
421
+ # `fizzy card tag` toggles, which is safe on a card that was just created.
422
+ def tag_card(card_number, tag_names, spawn_env)
423
+ tag_names.each do |name|
424
+ output, status = Open3.capture2e(spawn_env, "fizzy", "card", "tag", card_number.to_s, "--tag", name)
425
+ LOG.warn "[Fizzy] Couldn't tag card ##{card_number} #{name}: #{output.strip[0, 200]}" if !status.success? && defined?(LOG)
427
426
  end
428
- rescue StandardError
429
- []
430
427
  end
431
428
 
432
429
  def assign_card(card_number, agent_name, spawn_env)
@@ -3,7 +3,7 @@
3
3
  module Brainiac
4
4
  module Plugins
5
5
  module Fizzy
6
- VERSION = "0.0.43"
6
+ VERSION = "0.0.45"
7
7
  end
8
8
  end
9
9
  end
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.43
4
+ version: 0.0.45
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Davis
@@ -89,6 +89,7 @@ extra_rdoc_files: []
89
89
  files:
90
90
  - README.md
91
91
  - lib/brainiac/plugins/fizzy.rb
92
+ - lib/brainiac/plugins/fizzy/agent_setup.rb
92
93
  - lib/brainiac/plugins/fizzy/cli.rb
93
94
  - lib/brainiac/plugins/fizzy/config.rb
94
95
  - lib/brainiac/plugins/fizzy/delegators.rb