Module: Kettle::Dev::OpenCollectiveConfig

Defined in:
lib/kettle/dev/open_collective_config.rb

Overview

Shared utility for resolving Open Collective configuration for this repository. Centralizes the logic for locating and reading .opencollective.yml and for deriving the handle from environment or the YAML file.

Class Method Summary collapse

Class Method Details

.disabled?Boolean

Check if Open Collective is disabled via environment variable. Returns true when OPENCOLLECTIVE_HANDLE or FUNDING_ORG is explicitly set to a falsey value.

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
# File 'lib/kettle/dev/open_collective_config.rb', line 16

def disabled?
  oc_handle = ENV["OPENCOLLECTIVE_HANDLE"]
  funding_org = ENV["FUNDING_ORG"]

  [oc_handle, funding_org].any? do |val|
    val && val.to_s.strip.match(Kettle::Dev::ENV_FALSE_RE)
  end
end

.handle(required: false, root: nil, strict: false) ⇒ String?

Determine the Open Collective handle. Precedence:

1) ENV["OPENCOLLECTIVE_HANDLE"] when set and non-empty
2) .opencollective.yml key "collective" (or :collective)

Parameters:

  • required (Boolean) (defaults to: false)

    when true, aborts the process if not found; when false, returns nil

  • root (String, nil) (defaults to: nil)

    optional project root to look for .opencollective.yml

Returns:

  • (String, nil)

    the handle, or nil when not required and not discoverable



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/kettle/dev/open_collective_config.rb', line 41

def handle(required: false, root: nil, strict: false)
  env = ENV["OPENCOLLECTIVE_HANDLE"]
  return env unless env.nil? || env.to_s.strip.empty? || env.match?(/\{KJ\|[^}]+}/)

  ypath = yaml_path(root)
  if strict
    yml = YAML.safe_load(File.read(ypath))
    if yml.is_a?(Hash)
      handle = yml["collective"] || yml[:collective] || yml["org"] || yml[:org]
      return handle.to_s unless handle.nil? || handle.to_s.strip.empty? || handle.to_s.match?(/\{KJ\|[^}]+}/)
    end
  elsif File.file?(ypath)
    begin
      yml = YAML.safe_load(File.read(ypath))
      if yml.is_a?(Hash)
        handle = yml["collective"] || yml[:collective] || yml["org"] || yml[:org]
        return handle.to_s unless handle.nil? || handle.to_s.strip.empty? || handle.to_s.match?(/\{KJ\|[^}]+}/)
      end
    rescue StandardError => e
      Kettle::Dev.debug_error(e, __method__) if Kettle::Dev.respond_to?(:debug_error)
      # fall through to required check
    end
  end

  if required
    Kettle::Dev::ExitAdapter.abort("ERROR: Open Collective handle not provided. Set OPENCOLLECTIVE_HANDLE or add 'collective: <handle>' to .opencollective.yml.")
  end
  nil
end

.yaml_path(root = nil) ⇒ String

Absolute path to a .opencollective.yml

Parameters:

  • root (String, nil) (defaults to: nil)

    optional project root to resolve against; when nil, uses this repo root

Returns:

  • (String)


28
29
30
31
# File 'lib/kettle/dev/open_collective_config.rb', line 28

def yaml_path(root = nil)
  return File.expand_path(".opencollective.yml", root) if root
  File.expand_path("../../../.opencollective.yml", __dir__)
end