Class: SSHKeyHub::Provider::GitHub

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_key_hub/provider/github.rb

Overview

GitHub SSH public key provider

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GitHub

Returns a new instance of GitHub

Parameters:

  • options (Hash) (defaults to: {})

    GitHub credentials for Octokit::Client

Options Hash (options):

  • :access_token (String)

    OAuth2 access token for authentication

  • :api_endpoint (String)

    Base URL for API requests. default: api.github.com/

See Also:



13
14
15
16
17
# File 'lib/ssh_key_hub/provider/github.rb', line 13

def initialize(options = {})
  default_options = { auto_paginate: true }
  default_options.merge(access_token: ENV['OCTOKIT_ACCESS_TOKEN']) unless ENV['OCTOKIT_ACCESS_TOKEN'].nil?
  @client = Octokit::Client.new(default_options.merge(options))
end

Instance Method Details

#keys_for(organization, team = nil) ⇒ Hash

Returns Hash with keys by username with SortedSets

Parameters:

  • GitHub (String)

    team, if not given defaults to all

  • organization (String)

    GitHub organization

Returns:

  • (Hash)

    Hash with keys by username with SortedSets



38
39
40
41
42
43
44
# File 'lib/ssh_key_hub/provider/github.rb', line 38

def keys_for(organization, team = nil)
  if team.nil?
    keys_for_whole_org(organization)
  else
    keys_for_org_team(organization, team)
  end
end

#keys_for_org_team(organization, team) ⇒ Hash

Returns Hash with keys by username with SortedSets

Parameters:

  • GitHub (String)

    team

  • organization (String)

    GitHub organization

Returns:

  • (Hash)

    Hash with keys by username with SortedSets



28
29
30
31
32
33
# File 'lib/ssh_key_hub/provider/github.rb', line 28

def keys_for_org_team(organization, team)
  teams = @client.org_teams(organization).select { |t| t.name == team }
  raise 'Incorrect team' unless teams.size == 1
  team_id = teams.first.id
  keys_for_members(@client.team_members(team_id))
end

#keys_for_whole_org(organization) ⇒ Hash

Returns Hash with keys by username with SortedSets

Parameters:

  • organization (String)

    GitHub organization

Returns:

  • (Hash)

    Hash with keys by username with SortedSets



21
22
23
# File 'lib/ssh_key_hub/provider/github.rb', line 21

def keys_for_whole_org(organization)
  keys_for_members(@client.org_members(organization))
end