Class: Providers::GithubManager

Inherits:
BaseManager show all
Defined in:
app/models/providers/github_manager.rb

Constant Summary

Constants inherited from BaseManager

BaseManager::SPIN_SCHEMA

Instance Attribute Summary collapse

Attributes inherited from BaseManager

#identifier, #provider

Instance Method Summary collapse

Methods inherited from BaseManager

#get_connector, #validate_provider

Constructor Details

#initialize(provider) ⇒ GithubManager

Returns a new instance of GithubManager


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/models/providers/github_manager.rb', line 5

def initialize(provider)
  Rails.logger.info 'Generating new connection to GitHub'

  # github_url = provider[:enterprise] ? URI::HTTPS.build(host: provider[:server],path: "api/#{provider[:version]}") : URI::HTTPS.build(host: provider[:server])
  github_url = URI::HTTPS.build(host: provider[:server])

  opts = {
      api_endpoint:       github_url.to_s,
      connection_options: { ssl: { verify: provider[:verify] } },
      client_id:          provider[:id_application],
      client_secret:      provider[:secret],
      scope:              'user:email',
      access_token:       @provider_token
  }

  @github_access ||= Octokit::Client.new opts
  @server_type = 'GitHub'
end

Instance Attribute Details

#github_accessObject (readonly)

Returns the value of attribute github_access


3
4
5
# File 'app/models/providers/github_manager.rb', line 3

def github_access
  @github_access
end

#server_typeObject (readonly)

Returns the value of attribute server_type


3
4
5
# File 'app/models/providers/github_manager.rb', line 3

def server_type
  @server_type
end

Instance Method Details

#candidate_spin?(full_name) ⇒ Boolean

Returns if the repo is a candidate to be a spin

Parameters:

  • full_name (String)

    Full name of repo

Returns:

  • (Boolean)

Raises:

  • (Octokit::NotFound)

110
111
112
113
114
115
116
117
118
# File 'app/models/providers/github_manager.rb', line 110

def candidate_spin?(full_name)
  raise Octokit::NotFound if @github_access.nil?
  begin
    # GitHub returns false when there is no content, or the content otherwise
    not (@github_access.contents(full_name, path: '/.manageiq-spin') == false)
  rescue Octokit::NotFound, Octokit::InvalidRepository
    return false
  end
end

#exchange_code_for_token!(code) ⇒ String

Accepts a code and return a token hash

Parameters:

  • code (String)

Returns:

  • (String)

30
31
32
33
34
# File 'app/models/providers/github_manager.rb', line 30

def exchange_code_for_token!(code)
  github_token = @github_access.exchange_code_for_token(code)
  @github_access.access_token = github_token[:access_token] unless github_token[:error] || github_token.nil?
  github_token
end

#metadata(full_name) ⇒ metadata_raw, metadata_json

Returns metadata from the repo or nil

Parameters:

  • full_name (String)

    Full name of repo

Returns:

  • (metadata_raw, metadata_json)

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/models/providers/github_manager.rb', line 49

def (full_name)
  begin
     = @github_access.contents(full_name, path: '/metadata.yml', accept: 'application/vnd.github.raw')
  rescue Octokit::NotFound
    return ErrorExchange.new(:spin_get_metadata_from_provider, nil, {})
  end

  begin
     = JSON.parse(JSON.dump(YAML.safe_load()))
    unless .nil?
      JSON::Validator.validate!(SPIN_SCHEMA, )
      return [, ]
    end
    return ErrorExchange.new(:spin_metadata_to_json, nil, {})
  rescue TypeError, JSON::ParserError, JSON::Schema::ValidationError => e
    return ErrorExchange.new(:spin_error_metadata, nil, {error: e.to_json})
  end
end

#readme(full_name) ⇒ Object

Returns readme decoded

Parameters:

  • full_name (String)

    Full name of repo


84
85
86
87
88
89
# File 'app/models/providers/github_manager.rb', line 84

def readme(full_name)
  raise Octokit::NotFound if @github_access.nil?
  @github_access.readme(full_name, accept: 'application/vnd.github.raw')
rescue Octokit::NotFound => e
  ErrorExchange.new(:github_octokit_not_found, nil, {error: e.to_json})
end

#releases(full_name) ⇒ metadata_raw, metadata_json

Returns releases from the repo or nil

Parameters:

  • full_name (String)

    Full name of repo

Returns:

  • (metadata_raw, metadata_json)

72
73
74
75
76
77
78
79
# File 'app/models/providers/github_manager.rb', line 72

def releases(full_name)
  begin
    raise Octokit::NotFound if @github_access.nil?
    @github_access.releases(full_name)
  rescue Octokit::NotFound => e
    ErrorExchange.new(:github_octokit_not_found, nil, {error: e.to_json})
  end
end

#repo(fullname) ⇒ Object


91
92
93
94
95
96
# File 'app/models/providers/github_manager.rb', line 91

def repo(fullname)
  raise Octokit::NotFound if @github_access.nil?
  @github_access.repository(fullname)
rescue  Octokit::NotFound => e
  ErrorExchange.new(:github_octokit_not_found, nil, {error: e.to_json})
end

#reposObject


98
99
100
101
102
103
# File 'app/models/providers/github_manager.rb', line 98

def repos
  @github_access.access_token ||= @provider_token
  @github_access.repos(@provider_user)
rescue  Octokit::NotFound => e
  ErrorExchange.new(:github_octokit_not_found, nil, {error: e.to_json})
end

#userObject

Returns the user


40
41
42
# File 'app/models/providers/github_manager.rb', line 40

def user
  @github_access.user if @github_access.user_authenticated?
end