Class: Dependagrab::GithubClient

Inherits:
Object
  • Object
show all
Defined in:
lib/dependagrab/github_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ GithubClient

Returns a new instance of GithubClient.



6
7
8
9
10
# File 'lib/dependagrab/github_client.rb', line 6

def initialize(options={})
  @repo = options.fetch(:repo)
  @owner = options.fetch(:owner)
  @token = options[:token] || default_github_token
end

Instance Attribute Details

#ownerObject (readonly)

Returns the value of attribute owner.



4
5
6
# File 'lib/dependagrab/github_client.rb', line 4

def owner
  @owner
end

#repoObject (readonly)

Returns the value of attribute repo.



4
5
6
# File 'lib/dependagrab/github_client.rb', line 4

def repo
  @repo
end

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/dependagrab/github_client.rb', line 4

def token
  @token
end

Instance Method Details

#grabObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dependagrab/github_client.rb', line 12

def grab
  result = {
    alerts: []
  }
  query_variables = { repo: repo, owner: owner }

  has_next_page = true # always run query at least once
  while(has_next_page)
    response = GHAPI::Client.query(GHAPI::Query,
                                  variables: query_variables,
                                  context: { api_token: token }
                                 )
    validate_response!(response)

    response.original_hash['data']['repository']['vulnerabilityAlerts']['nodes'].each do |alert|
      result[:alerts].append(
        parse_alert(alert)
      )
    end

    # Sets the last position for pagination
    query_variables[:after_cursor] = response.data.repository.vulnerability_alerts.page_info.end_cursor

    has_next_page = response.data.repository.vulnerability_alerts.page_info.has_next_page
  end

  result
end