Class: Tokite::Repository

Inherits:
ApplicationRecord show all
Defined in:
app/models/tokite/repository.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_hook(octokit_client, repo_name) ⇒ Object



14
15
16
# File 'app/models/tokite/repository.rb', line 14

def self.find_hook(octokit_client, repo_name)
  octokit_client.hooks(repo_name).find {|hook| hook.config.url == hooks_url }
end

.hook!(octokit_client, github_repo) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'app/models/tokite/repository.rb', line 3

def self.hook!(octokit_client, github_repo)
  repo_name = github_repo.full_name
  existing_hook = find_hook(octokit_client, repo_name)
  if existing_hook
    octokit_client.edit_hook(repo_name, existing_hook.id, "web", hook_config, hook_options)
  else
    octokit_client.create_hook(repo_name, "web", hook_config, hook_options)
  end
  create!(name: repo_name, url: github_repo.html_url)
end

.hook_configObject



18
19
20
21
22
23
# File 'app/models/tokite/repository.rb', line 18

def self.hook_config
  {
    content_type: "json",
    url: hooks_url,
  }
end

.hook_optionsObject



25
26
27
28
29
# File 'app/models/tokite/repository.rb', line 25

def self.hook_options
  {
    events: ["*"]
  }
end

.hooks_urlObject



31
32
33
# File 'app/models/tokite/repository.rb', line 31

def self.hooks_url
  Tokite::Engine.routes.url_helpers.hooks_url
end

Instance Method Details

#unhook!(octokit_client) ⇒ Object



35
36
37
38
39
# File 'app/models/tokite/repository.rb', line 35

def unhook!(octokit_client)
  hook = self.class.find_hook(octokit_client, name)
  octokit_client.remove_hook(name, hook.id) if hook
  destroy!
end