Module: Brainiac::Plugins::Fizzy::Config

Defined in:
lib/brainiac/plugins/fizzy/config.rb

Overview

Fizzy configuration — loads ~/.brainiac/fizzy.json. Provides board config, webhook secrets, authorized users, and column IDs.

Constant Summary collapse

FIZZY_CONFIG_FILE =
File.join(
  ENV.fetch("BRAINIAC_DIR", File.join(Dir.home, ".brainiac")),
  "fizzy.json"
)

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.authorized_user_idsObject (readonly)

Returns the value of attribute authorized_user_ids.



19
20
21
# File 'lib/brainiac/plugins/fizzy/config.rb', line 19

def authorized_user_ids
  @authorized_user_ids
end

.boardsObject (readonly)

Returns the value of attribute boards.



19
20
21
# File 'lib/brainiac/plugins/fizzy/config.rb', line 19

def boards
  @boards
end

.configObject (readonly)

Returns the value of attribute config.



19
20
21
# File 'lib/brainiac/plugins/fizzy/config.rb', line 19

def config
  @config
end

Class Method Details

.authorized?(payload) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
# File 'lib/brainiac/plugins/fizzy/config.rb', line 85

def authorized?(payload)
  creator_id = payload.dig("creator", "id")
  @authorized_user_ids.include?(creator_id)
end

.board_column_id(board_key, column_name) ⇒ Object



53
54
55
56
# File 'lib/brainiac/plugins/fizzy/config.rb', line 53

def board_column_id(board_key, column_name)
  config = board_config(board_key)
  config&.dig("columns", column_name.to_s)
end

.board_config(board_key) ⇒ Object



44
45
46
# File 'lib/brainiac/plugins/fizzy/config.rb', line 44

def board_config(board_key)
  @boards[board_key.to_s]
end

.board_key_for_id(board_id) ⇒ Object



58
59
60
61
62
63
# File 'lib/brainiac/plugins/fizzy/config.rb', line 58

def board_key_for_id(board_id)
  @boards.each do |key, config|
    return key if config["board_id"] == board_id
  end
  nil
end

.board_key_for_project(project_config) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/brainiac/plugins/fizzy/config.rb', line 65

def board_key_for_project(project_config)
  # Priority 1: explicit fizzy_board in project config (board key name from fizzy.json)
  board_key = project_config["fizzy_board"]
  return board_key if board_key && @boards.key?(board_key)

  # Priority 2: .fizzy.yaml in the repo directory
  fizzy_yaml = File.join(project_config["repo_path"], ".fizzy.yaml")
  if File.exist?(fizzy_yaml)
    require "yaml"
    data = YAML.safe_load_file(fizzy_yaml)
    board_id = data["board"]
    return board_key_for_id(board_id)
  end

  nil
rescue StandardError => e
  LOG.warn "[Fizzy] Could not resolve board for project: #{e.message}" if defined?(LOG)
  nil
end

.board_webhook_secret(board_key) ⇒ Object



48
49
50
51
# File 'lib/brainiac/plugins/fizzy/config.rb', line 48

def board_webhook_secret(board_key)
  config = board_config(board_key)
  config&.dig("webhook_secret") || ENV.fetch("FIZZY_WEBHOOK_SECRET", nil)
end

.currentObject



40
41
42
# File 'lib/brainiac/plugins/fizzy/config.rb', line 40

def current
  @config
end

.human_mentioned?(user_id) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
# File 'lib/brainiac/plugins/fizzy/config.rb', line 90

def human_mentioned?(user_id)
  user = (@config["authorized_users"] || []).find { |u| u["id"] == user_id }
  user && user["human"]
end

.identify_project_by_tags(tags) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/brainiac/plugins/fizzy/config.rb', line 95

def identify_project_by_tags(tags)
  tag_names = tags.map { |t| t.is_a?(Hash) ? t["name"] : t.to_s }.map(&:downcase)

  PROJECTS.each do |key, config|
    project_tags = (config["tags"] || config["fizzy_tags"] || []).map(&:downcase)
    return [key, config] if tag_names.intersect?(project_tags)
  end

  # Fall back to default project
  default_key = default_project_key
  default_key ? [default_key, PROJECTS[default_key]] : nil
end

.load!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/brainiac/plugins/fizzy/config.rb', line 21

def load!
  @config = if File.exist?(FIZZY_CONFIG_FILE)
              JSON.parse(File.read(FIZZY_CONFIG_FILE))
            else
              {}
            end
  @boards = @config["boards"] || {}
  @authorized_user_ids = (@config["authorized_users"] || []).map { |u| u["id"] }
rescue JSON::ParserError => e
  LOG.error "[Fizzy] Failed to parse fizzy.json: #{e.message}" if defined?(LOG)
  @config = {}
  @boards = {}
  @authorized_user_ids = []
end

.reload!Object



36
37
38
# File 'lib/brainiac/plugins/fizzy/config.rb', line 36

def reload!
  load!
end