Module: Legion::Extensions::Github::Absorbers::WebhookSetup

Defined in:
lib/legion/extensions/github/absorbers/webhook_setup.rb

Overview

Mixin for auto-registering GitHub webhooks and fleet labels on a repo. Used by ‘legionio fleet add github` to wire up the absorber source.

Include this module in a class that also includes the GitHub runners (RepositoryWebhooks, Labels).

Constant Summary collapse

FLEET_WEBHOOK_EVENTS =
%w[issues pull_request].freeze
FLEET_LABELS =
[
  { name: 'fleet:received', color: '6f42c1', description: 'Fleet pipeline has received this issue' },
  { name: 'fleet:implementing', color: '0e8a16', description: 'Fleet is implementing a fix' },
  { name: 'fleet:pr-open', color: '1d76db', description: 'Fleet has opened a PR for this issue' },
  { name: 'fleet:escalated', color: 'e4e669', description: 'Fleet escalated this issue to a human' }
].freeze

Instance Method Summary collapse

Instance Method Details

#fleet_label_definitionsObject



77
78
79
# File 'lib/legion/extensions/github/absorbers/webhook_setup.rb', line 77

def fleet_label_definitions
  FLEET_LABELS
end

#setup_fleet_webhook(owner:, repo:, webhook_url:) ⇒ Hash

Set up fleet webhook and labels on a GitHub repo.

Parameters:

  • owner (String)

    Repository owner/org

  • repo (String)

    Repository name

  • webhook_url (String)

    Callback URL for webhook delivery

Returns:

  • (Hash)

    { success:, webhook_id:, labels_created: }



29
30
31
32
33
34
35
36
37
38
39
40
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
70
71
72
73
74
75
# File 'lib/legion/extensions/github/absorbers/webhook_setup.rb', line 29

def setup_fleet_webhook(owner:, repo:, webhook_url:, **)
  # Check if webhook already exists
  existing = list_webhooks(owner: owner, repo: repo)
  existing_hook = (existing[:result] || []).find do |hook|
    url = hook.is_a?(Hash) ? (hook.dig('config', 'url') || hook.dig(:config, :url)) : nil
    url == webhook_url
  end

  if existing_hook
    hook_id = existing_hook['id'] || existing_hook[:id]
    labels = ensure_fleet_labels(owner: owner, repo: repo)
    return { success: true, existing: true, webhook_id: hook_id, labels_created: labels }
  end

  # Create webhook
  config = {
    url:          webhook_url,
    content_type: 'json',
    insecure_ssl: '0'
  }

  result = create_webhook(
    owner:  owner,
    repo:   repo,
    config: config,
    events: FLEET_WEBHOOK_EVENTS,
    active: true
  )

  webhook_data = result[:result] || {}
  webhook_id = webhook_data['id'] || webhook_data[:id]

  return { success: false, error: 'webhook creation returned no id' } if webhook_id.nil?

  # Create fleet labels
  labels = ensure_fleet_labels(owner: owner, repo: repo)

  {
    success:        true,
    existing:       false,
    webhook_id:     webhook_id,
    webhook_url:    webhook_url,
    labels_created: labels
  }
rescue StandardError => e
  { success: false, error: e.message }
end