Class: KExt::Github::Api
- Inherits:
-
Object
- Object
- KExt::Github::Api
- 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
-
#all_repositories ⇒ Object
list of ALL repositories for this user.
-
#auth(token: nil, login: nil, password: nil) ⇒ Object
Authenticate against GitHub with username and password or token.
-
#create_repository(repository_name, **options) ⇒ Object
create repository.
-
#delete_repository(repository_name, **options) ⇒ Object
delete repository.
-
#hooks(repository_name) ⇒ Object
list of hooks for repository.
-
#initialize(token: nil, login: nil, password: nil) ⇒ Api
constructor
Create API for communicating with GitHub.
-
#organization_repositories(org_name) ⇒ Object
list of repositories for organization.
-
#remove_hook(repository_name, id) ⇒ Object
remove hook from repository by id.
-
#remove_hooks(repository_name) ⇒ Object
remove all hooks in a repository.
-
#repositories(per_page: 100, page: 1) ⇒ Object
list of repositories for this user.
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
33 34 35 |
# File 'lib/k_ext/github/api.rb', line 33 def initialize(token: nil, login: nil, password: nil) auth(token: token, login: login, password: password) if token || login || password end |
Class Method Details
Instance Method Details
#all_repositories ⇒ Object
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
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? && (login.nil? || password.nil?) self.client = if token.nil? Octokit::Client.new(login: 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
94 95 96 |
# File 'lib/k_ext/github/api.rb', line 94 def create_repository(repository_name, **) @client.create_repository(repository_name, ) end |
#delete_repository(repository_name, **options) ⇒ Object
delete repository
101 102 103 |
# File 'lib/k_ext/github/api.rb', line 101 def delete_repository(repository_name, **) @client.delete_repository(repository_name, **) end |
#hooks(repository_name) ⇒ Object
list of hooks for repository
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
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
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
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 |