Module: Brainiac::Plugins::Github::Config

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

Constant Summary collapse

CONFIG_FILE =
File.join(ENV.fetch("BRAINIAC_DIR", File.join(Dir.home, ".brainiac")), "github.json")

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/brainiac/plugins/github/config.rb', line 13

def config
  @config
end

Class Method Details

.agent_app_configured?(agent_key) ⇒ Boolean

Returns the agent key that should be used for a given context. If per-agent apps are configured and the agent has an entry, returns that key. Otherwise returns nil (use shared app).

Returns:

  • (Boolean)


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

def agent_app_configured?(agent_key)
  return false unless agent_key

  !!@config.dig("apps", agent_key.to_s.downcase)
end

.app_id(agent_key = nil) ⇒ Object

Per-agent app credentials from the "apps" hash. Falls back to the shared "app" config if no per-agent entry exists.



36
37
38
39
40
# File 'lib/brainiac/plugins/github/config.rb', line 36

def app_id(agent_key = nil)
  per_agent_value(agent_key, "id") ||
    @config.dig("app", "id")&.to_s ||
    ENV.fetch("GITHUB_APP_ID", nil)
end

.installation_id(agent_key = nil, repo_owner: nil) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/brainiac/plugins/github/config.rb', line 52

def installation_id(agent_key = nil, repo_owner: nil)
  # Check per-agent config first
  if agent_key
    normalized = agent_key.to_s.downcase
    agent_conf = @config.dig("apps", normalized)
    if agent_conf
      # Per-agent may have multiple installations keyed by owner
      if repo_owner && agent_conf["installations"]
        return agent_conf.dig("installations", repo_owner)&.to_s || agent_conf["installation_id"]&.to_s
      end

      # Flat installation_id
      return agent_conf["installation_id"]&.to_s if agent_conf["installation_id"]

      # No repo_owner specified but installations hash exists — return first available
      # (used by configured? check where repo_owner isn't known yet)
      return agent_conf["installations"].values.first&.to_s if agent_conf["installations"]&.any?
    end
  end

  # Shared app config — check installations hash first, then flat installation_id
  if repo_owner && @config.dig("app", "installations")
    found = @config.dig("app", "installations", repo_owner)&.to_s
    return found if found
  end

  @config.dig("app", "installation_id")&.to_s ||
    ENV.fetch("GITHUB_APP_INSTALLATION_ID", nil)
end

.load!Object



15
16
17
18
# File 'lib/brainiac/plugins/github/config.rb', line 15

def load!
  @config = load_config
  @last_mtime = File.exist?(CONFIG_FILE) ? File.mtime(CONFIG_FILE) : nil
end

.owner_allowed?(owner) ⇒ Boolean

Check if a repo owner is in the allowed_owners list. If allowed_owners is not configured (empty/missing), all owners are allowed.

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/brainiac/plugins/github/config.rb', line 93

def owner_allowed?(owner)
  allowed = @config["allowed_owners"]
  return true unless allowed.is_a?(Array) && !allowed.empty?

  allowed.include?(owner)
end

.private_key_path(agent_key = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/brainiac/plugins/github/config.rb', line 42

def private_key_path(agent_key = nil)
  path = per_agent_value(agent_key, "private_key_path") ||
         @config.dig("app", "private_key_path") ||
         ENV.fetch("GITHUB_APP_PRIVATE_KEY_PATH", nil)
  return nil unless path

  expanded = File.expand_path(path)
  File.exist?(expanded) ? expanded : nil
end

.reload!Object



20
21
22
23
24
25
26
27
# File 'lib/brainiac/plugins/github/config.rb', line 20

def reload!
  return unless file_changed?

  @config = load_config
  @last_mtime = File.exist?(CONFIG_FILE) ? File.mtime(CONFIG_FILE) : nil
  AppClient.reset! if defined?(AppClient)
  LOG.info "[GitHub] Reloaded configuration"
end

.webhook_secretObject



29
30
31
# File 'lib/brainiac/plugins/github/config.rb', line 29

def webhook_secret
  @config["webhook_secret"] || ENV.fetch("GITHUB_WEBHOOK_SECRET", nil)
end