Class: KExt::Github::Api

Inherits:
Object
  • Object
show all
Includes:
KLog::Logging
Defined in:
lib/k_ext/github/api.rb

Overview

The API provides a limited set of service methods for working against GitHub

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token: nil, login: nil, password: nil) ⇒ Api

Create API for communicating with GitHub

Provide token OR login/password

Create a token here github.com/settings/tokens/new

Parameters:

  • token (String) (defaults to: nil)
  • login (String) (defaults to: nil)
  • password (String) (defaults to: nil)


33
34
35
# File 'lib/k_ext/github/api.rb', line 33

def initialize(token: nil, login: nil, password: nil)
  auth(token: token, login: , password: password) if token ||  || password
end

Class Method Details

.instance(access_token) ⇒ Object



19
20
21
# File 'lib/k_ext/github/api.rb', line 19

def self.instance(access_token)
  Api.new(token: access_token)
end

Instance Method Details

#all_repositoriesObject

list of ALL repositories for this user



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/k_ext/github/api.rb', line 73

def all_repositories
  # NOTE: Does not seam to work
  # items = @client.all_repositories({})

  items = []
  page = 1
  keep_going = true

  while keep_going
    paged_items = repositories(page: page)
    items += paged_items
    page += 1
    keep_going = paged_items.length.positive? && page <= 50
  end

  items.map { |item| KExt::Github::Models::Repository.new(item) }
end

#auth(token: nil, login: nil, password: nil) ⇒ Object

Authenticate against GitHub with username and password or token

Provide token OR login/password

Create a token here github.com/settings/tokens/new

Parameters:

  • token (String) (defaults to: nil)
  • login (String) (defaults to: nil)
  • password (String) (defaults to: nil)


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/k_ext/github/api.rb', line 46

def auth(token: nil, login: nil, password: nil)
  raise 'Provide credentials. Token or username/password' if token.nil? && (.nil? || password.nil?)

  self.client = if token.nil?
                  Octokit::Client.new(login: , password: password)
                else
                  Octokit::Client.new(access_token: token)
                end

  # Fetch the current user
  log.kv 'GitHub User', client.user.name
end

#create_repository(repository_name, **options) ⇒ Object

create repository

Parameters:

  • repository_name (String)

    e.g. klueless-io/z-test-aerial.com



94
95
96
# File 'lib/k_ext/github/api.rb', line 94

def create_repository(repository_name, **options)
  @client.create_repository(repository_name, options)
end

#delete_repository(repository_name, **options) ⇒ Object

delete repository

Parameters:

  • repository_name (String)

    e.g. klueless-io/z-test-aerial.com



101
102
103
# File 'lib/k_ext/github/api.rb', line 101

def delete_repository(repository_name, **options)
  @client.delete_repository(repository_name, **options)
end

#hooks(repository_name) ⇒ Object

list of hooks for repository

Parameters:

  • repository_name (String)

    e.g. klueless-io/z-test-aerial.com



108
109
110
111
112
# File 'lib/k_ext/github/api.rb', line 108

def hooks(repository_name)
  items = @client.hooks(repository_name)

  items.map { |item| KExt::Github::Models::Hook.new(item) }
end

#organization_repositories(org_name) ⇒ Object

list of repositories for organization

Parameters:

  • org_name (String)

    Organization name



117
118
119
120
121
# File 'lib/k_ext/github/api.rb', line 117

def organization_repositories(org_name)
  items = @client.repositories(org_name, query: { per_page: 100 })

  items.map { |item| KExt::Github::Models::Repository.new(item) }
end

#remove_hook(repository_name, id) ⇒ Object

remove hook from repository by id

Parameters:

  • repository_name (String)

    e.g. klueless-io/z-test-aerial.com

  • id (Integer)

    hook ID



128
129
130
# File 'lib/k_ext/github/api.rb', line 128

def remove_hook(repository_name, id)
  @client.remove_hook(repository_name, id)
end

#remove_hooks(repository_name) ⇒ Object

remove all hooks in a repository

Parameters:

  • repository_name (String)

    e.g. klueless-io/z-test-aerial.com



135
136
137
138
139
140
141
# File 'lib/k_ext/github/api.rb', line 135

def remove_hooks(repository_name)
  hooks = hooks(repository_name)

  hooks.each do |hook|
    @client.remove_hook(repository_name, hook.id)
  end
end

#repositories(per_page: 100, page: 1) ⇒ Object

list of repositories for this user



65
66
67
68
69
# File 'lib/k_ext/github/api.rb', line 65

def repositories(per_page: 100, page: 1)
  items = @client.repositories({}, query: { per_page: per_page, page: page })

  items.map { |item| KExt::Github::Models::Repository.new(item) }
end