Class: CollavreGithub::WebhookProvisioner

Inherits:
Object
  • Object
show all
Defined in:
app/services/collavre_github/webhook_provisioner.rb

Constant Summary collapse

EVENTS =
%w[pull_request].freeze
EVENTS_WITH_PUSH =
%w[pull_request push].freeze
CONTENT_TYPE =
"json".freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account:, webhook_url:, client: CollavreGithub::Client.new(account)) ⇒ WebhookProvisioner

Returns a new instance of WebhookProvisioner.



15
16
17
18
# File 'app/services/collavre_github/webhook_provisioner.rb', line 15

def initialize(account:, webhook_url:, client: CollavreGithub::Client.new())
  @client = client
  @webhook_url = webhook_url
end

Class Method Details



7
8
9
# File 'app/services/collavre_github/webhook_provisioner.rb', line 7

def self.ensure_for_links(account:, links:, webhook_url:)
  new(account: , webhook_url: webhook_url).ensure_for_links(Array(links))
end

.remove_for_repositories(account:, repositories:, webhook_url:) ⇒ Object



11
12
13
# File 'app/services/collavre_github/webhook_provisioner.rb', line 11

def self.remove_for_repositories(account:, repositories:, webhook_url:)
  new(account: , webhook_url: webhook_url).remove_for_repositories(Array(repositories))
end

Instance Method Details



20
21
22
23
24
# File 'app/services/collavre_github/webhook_provisioner.rb', line 20

def ensure_for_links(links)
  links.each do |link|
    ensure_webhook(link)
  end
end

#remove_for_repositories(repositories) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/collavre_github/webhook_provisioner.rb', line 26

def remove_for_repositories(repositories)
  # Batch-query to avoid N+1: find all repo names that still have links
  linked_names = CollavreGithub::RepositoryLink
    .where(repository_full_name: repositories)
    .distinct
    .pluck(:repository_full_name)
    .to_set

  repositories.each do |repository_full_name|
    next if linked_names.include?(repository_full_name)

    remove_webhook(repository_full_name)
  end
end