Skip to content

Commit

Permalink
Refactor to not load schema on Class load
Browse files Browse the repository at this point in the history
  • Loading branch information
naarok committed Sep 8, 2023
1 parent 0d9083e commit 0ad8168
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 44 deletions.
2 changes: 1 addition & 1 deletion lib/library_version_analysis/analyze.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module LibraryVersionAnalysis
class Analyze
def self.go
def self.go(_) # TODO: This will get resolved once we merge in the server rewrite
spreadsheet_id = ENV["VERSION_STATUS_SPREADSHEET_ID"]
results = LibraryVersionAnalysis::CheckVersionStatus.run(spreadsheet_id: spreadsheet_id, online: "true", online_node: "true", mobile: "false")

Expand Down
90 changes: 47 additions & 43 deletions lib/library_version_analysis/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,64 +5,70 @@ module LibraryVersionAnalysis
class Github
URL = "https://api.github.com/graphql".freeze

unless ENV['GITHUB_READ_API_TOKEN'].nil? || ENV['GITHUB_READ_API_TOKEN'].empty?
HTTP_ADAPTER = GraphQL::Client::HTTP.new(URL) do
ALERTS_FRAGMENT = <<-GRAPHQL.freeze
fragment data on RepositoryVulnerabilityAlertConnection {
totalCount
nodes {
securityVulnerability {
package {
ecosystem
name
}
advisory {
databaseId
identifiers {
type
value
}
publishedAt
permalink
}
severity
}
number
createdAt
}
pageInfo {
endCursor
hasNextPage
}
}
GRAPHQL

def initialize
if ENV['GITHUB_READ_API_TOKEN'].nil? || ENV['GITHUB_READ_API_TOKEN'].empty?
raise "GITHUB_READ_API_TOKEN is not set"
end

http_adapter = GraphQL::Client::HTTP.new(URL) do
def headers(_context)
{
"Authorization" => "Bearer #{ENV['GITHUB_READ_API_TOKEN']}",
"User-Agent" => "Ruby",
}
end
end
SCHEMA = GraphQL::Client.load_schema(HTTP_ADAPTER)
CLIENT = GraphQL::Client.new(schema: SCHEMA, execute: HTTP_ADAPTER)

ALERTS_FRAGMENT = <<-GRAPHQL.freeze
fragment data on RepositoryVulnerabilityAlertConnection {
totalCount
nodes {
securityVulnerability {
package {
ecosystem
name
}
advisory {
databaseId
identifiers {
type
value
}
publishedAt
permalink
}
severity
}
number
createdAt
}
pageInfo {
endCursor
hasNextPage
}
}
GRAPHQL

AlertsQuery = Github::CLIENT.parse <<-GRAPHQL
schema = GraphQL::Client.load_schema(http_adapter)
@client = GraphQL::Client.new(schema: schema, execute: http_adapter)
@client.allow_dynamic_queries = true

@alerts_query = @client.parse <<-GRAPHQL
query($name: String!) {
repository(name: $name, owner: "GetJobber") {
vulnerabilityAlerts(first: 100, states: OPEN) {
vulnerabilityAlerts(first: 100, states: OPEN) {
...data
}
}
}
#{ALERTS_FRAGMENT}
GRAPHQL

AlertsQueryNext = Github::CLIENT.parse <<-GRAPHQL
@alerts_query_next = @client.parse <<-GRAPHQL
query($name: String!, $cursor: String!) {
repository(name: $name, owner: "GetJobber") {
vulnerabilityAlerts(first: 100, states: OPEN, after: $cursor) {
vulnerabilityAlerts(first: 100, states: OPEN, after: $cursor) {
...data
}
}
Expand All @@ -71,8 +77,6 @@ def headers(_context)
GRAPHQL
end

def initialize; end

def get_dependabot_findings(parsed_results, meta_data, github_name, ecosystem)
github = LibraryVersionAnalysis::Github.new
alerts = github.find_alerts(github_name, ecosystem)
Expand Down Expand Up @@ -105,7 +109,7 @@ def get_dependabot_findings(parsed_results, meta_data, github_name, ecosystem)
end

def find_alerts(github_name, ecosystem)
response = Github::CLIENT.query(AlertsQuery, variables: { name: github_name })
response = @client.query(@alerts_query, variables: { name: github_name })

alerts = {}

Expand All @@ -114,7 +118,7 @@ def find_alerts(github_name, ecosystem)
else
end_cursor = add_results(response.data.repository.vulnerability_alerts, alerts, ecosystem)
until end_cursor.nil?
response = Github::CLIENT.query(AlertsQueryNext, variables: { name: github_name, cursor: end_cursor })
response = @client.query(@alerts_query_next, variables: { name: github_name, cursor: end_cursor })
end_cursor = add_results(response.data.repository.vulnerability_alerts, alerts, ecosystem)
end
end
Expand Down

0 comments on commit 0ad8168

Please sign in to comment.