Module: Authentication

Included in:
IssueDB
Defined in:
lib/issue_db/authentication.rb

Class Method Summary collapse

Class Method Details

.login(client = nil, log = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/issue_db/authentication.rb', line 9

def self.(client = nil, log = nil)
  # if the client is not nil, use the pre-provided client
  unless client.nil?
    log.debug("using pre-provided client") if log
    return client
  end

  # if the client is nil, check for GitHub App env vars first
  # first, check if all three of the following env vars are set and have values
  # ISSUE_DB_GITHUB_APP_ID, ISSUE_DB_GITHUB_APP_INSTALLATION_ID, ISSUE_DB_GITHUB_APP_KEY
  if ENV.fetch("ISSUE_DB_GITHUB_APP_ID", nil) && ENV.fetch("ISSUE_DB_GITHUB_APP_INSTALLATION_ID", nil) && ENV.fetch("ISSUE_DB_GITHUB_APP_KEY", nil)
    log.debug("using github app authentication") if log
    return GitHubApp.new
  end

  # if the client is nil and no GitHub App env vars were found, check for the ISSUE_DB_GITHUB_TOKEN
  token = ENV.fetch("ISSUE_DB_GITHUB_TOKEN", nil)
  if token
    log.debug("using github token authentication") if log
    octokit = Octokit::Client.new(access_token: token, page_size: 100)
    octokit.auto_paginate = true
    return octokit
  end

  # if we make it here, no valid auth method succeeded
  raise AuthenticationError, "No valid GitHub authentication method was provided"
end