brainiac-basecamp 0.0.28 → 0.0.29

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: 38a2f09f750211e6d3ea71abaf79d5a075f6b1a25156cba5bfbdf6a12371385f
4
- data.tar.gz: 9833d0e7105e4b77b8707a0a1fdc1f412d2fd927fb6fe2384dcedfaa8bf7c110
3
+ metadata.gz: aba30c772551760c5807cf95eee181a38533c40de1eabf8d703951375412fdc0
4
+ data.tar.gz: 3a6591b9f70a7fe5c3f00be8e3f4e94f58928d2ada86d23b09756b5356276c7d
5
5
  SHA512:
6
- metadata.gz: ecb88689edaf3d9c15089a52972141a2042768d304e5ebfaa5ef0d62e76894982e6e24ebed1a62f280328da08eada2d7d8964e188a398b942516bc95d97e3f59
7
- data.tar.gz: 1a58423d2ed68a1c5081acca73c38e01bf12882665a4dd4360401e2aac91f9e4d7c0e6fa82523b6f9cc1cea82d995eed1afc95b4a3027d648841aa97dfcd3959
6
+ metadata.gz: e9fa196da5ab753c2ad343a306ce2d6887e137f14e9e78c98f69ad59a6f1a76808a3f2ecc61d55023bcef993a2237199cd239191601ed257bf685d61454d35e2
7
+ data.tar.gz: c0e98f310cfd494917b8e1cf24de6465ffccd03ecd441e82b80c048a137881b018043898f88befada77c069617c7f2f916fad4666b93b7f4699553037accef91
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "open3"
5
+
6
+ module Brainiac
7
+ module Plugins
8
+ module Basecamp
9
+ # Provisions a Basecamp person for a new brainiac agent (`brainiac agent
10
+ # create` / `brainiac agent setup`): finds or invites the agent by email,
11
+ # grants it access to every mapped project, and adds a bot_accounts entry.
12
+ module AgentSetup
13
+ class << self
14
+ def run(agent_key, entry)
15
+ display_name = entry["display_name"] || agent_key.capitalize
16
+ config = load_config
17
+ return unless config
18
+
19
+ if (config["bot_accounts"] || {}).any? { |_name, bot| bot["default_agent"] == display_name }
20
+ puts " [Basecamp] ✓ #{display_name} already has a bot account (skipping)"
21
+ return
22
+ end
23
+
24
+ project_ids = (config["project_mappings"] || {}).values.filter_map { |m| m["basecamp_project_id"]&.to_s }.uniq
25
+ person_id = find_person(display_name) || invite(display_name, entry["email"], project_ids.first)
26
+ return unless person_id
27
+
28
+ project_ids.each { |pid| basecamp("people", "add", person_id, "--project", pid) }
29
+
30
+ config["bot_accounts"] ||= {}
31
+ config["bot_accounts"][agent_key] = { "person_id" => person_id, "default_agent" => display_name }
32
+ File.write(config_file, JSON.pretty_generate(config))
33
+ puts " [Basecamp] ✓ #{display_name} (person #{person_id}) added to #{project_ids.size} project(s) and bot_accounts"
34
+ end
35
+
36
+ private
37
+
38
+ def find_person(display_name)
39
+ people = basecamp_json("people", "list", "--all") || []
40
+ person = people.find { |p| p["name"]&.casecmp?(display_name) }
41
+ person && person["id"].to_s
42
+ end
43
+
44
+ # Inviting someone new into a project creates the person and emails them.
45
+ def invite(display_name, email, project_id)
46
+ return nil unless $stdin.tty?
47
+
48
+ unless email && project_id
49
+ puts " [Basecamp] Skipping — #{email ? 'no mapped projects' : 'no email (pass --email)'} to invite #{display_name}"
50
+ return nil
51
+ end
52
+
53
+ print " [Basecamp] Invite #{display_name} <#{email}> to Basecamp? [Y/n]: "
54
+ return nil if $stdin.gets.to_s.strip.downcase.start_with?("n")
55
+
56
+ body = { create: [{ name: display_name, email_address: email }] }.to_json
57
+ result = basecamp_json("api", "put", "projects/#{project_id}/people/users.json", "-d", body)
58
+ person = (result&.dig("granted") || []).find { |p| p["name"]&.casecmp?(display_name) }
59
+ unless person
60
+ puts " [Basecamp] ⚠ Invite failed — invite #{email} by hand, then: brainiac agent setup #{display_name.downcase}"
61
+ return nil
62
+ end
63
+
64
+ puts " [Basecamp] ✓ Invited #{email} (accept from that inbox so the account is active)"
65
+ person["id"].to_s
66
+ end
67
+
68
+ def basecamp_json(*)
69
+ output = basecamp(*, "--quiet")
70
+ output && JSON.parse(output)
71
+ rescue JSON::ParserError
72
+ nil
73
+ end
74
+
75
+ def basecamp(*)
76
+ output, status = Open3.capture2("basecamp", *, err: File::NULL)
77
+ status.success? ? output : nil
78
+ rescue Errno::ENOENT
79
+ nil
80
+ end
81
+
82
+ def config_file
83
+ File.join(ENV.fetch("BRAINIAC_DIR", File.join(Dir.home, ".brainiac")), "basecamp.json")
84
+ end
85
+
86
+ def load_config
87
+ File.exist?(config_file) ? JSON.parse(File.read(config_file)) : nil
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "json"
4
4
  require "open3"
5
+ require_relative "agent_setup"
5
6
  require_relative "config"
6
7
  require_relative "epic"
7
8
 
@@ -1536,6 +1537,14 @@ module Brainiac
1536
1537
  Cli.run(args)
1537
1538
  end
1538
1539
 
1540
+ # Called by brainiac CLI after `agent create` / `agent setup` — finds or
1541
+ # invites the agent's Basecamp person (see AgentSetup). Safe to re-run.
1542
+ def self.on_agent_created(agent_key, entry)
1543
+ AgentSetup.run(agent_key, entry)
1544
+ rescue JSON::ParserError => e
1545
+ puts " ⚠ Could not update basecamp.json: #{e.message}"
1546
+ end
1547
+
1539
1548
  def self.completions
1540
1549
  %w[setup config status epics deploy link bot projects set reset scrap webhook]
1541
1550
  end
@@ -3,7 +3,7 @@
3
3
  module Brainiac
4
4
  module Plugins
5
5
  module Basecamp
6
- VERSION = "0.0.28"
6
+ VERSION = "0.0.29"
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-basecamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.28
4
+ version: 0.0.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Davis
@@ -88,6 +88,7 @@ extra_rdoc_files: []
88
88
  files:
89
89
  - README.md
90
90
  - lib/brainiac/plugins/basecamp.rb
91
+ - lib/brainiac/plugins/basecamp/agent_setup.rb
91
92
  - lib/brainiac/plugins/basecamp/cli.rb
92
93
  - lib/brainiac/plugins/basecamp/client.rb
93
94
  - lib/brainiac/plugins/basecamp/comment_responder.rb