Skip to content

Commit

Permalink
Fix fetch_owner_info when API returns null
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed May 7, 2020
1 parent 2e693fe commit 397ea30
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
38 changes: 38 additions & 0 deletions spec/fetchers/github_api_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,42 @@ describe Shardbox::GitHubAPI do
end
end
end

describe "#fetch_repository_owner" do
it "queries from GraphQL" do
WebMock.wrap do
WebMock.stub(:post, "https://api.github.com/graphql").to_return(<<-JSON, status: 200)
{
"data": {
"repositoryOwner": {
"description": "foo"
}
}
}
JSON

api = Shardbox::GitHubAPI.new("")

api.fetch_owner_info("foo").should eq({
"description" => JSON::Any.new("foo"),
})
end
end

it "handles repositoryOwner: null" do
WebMock.wrap do
WebMock.stub(:post, "https://api.github.com/graphql").to_return(<<-JSON, status: 200)
{
"data": {
"repositoryOwner": null
}
}
JSON

api = Shardbox::GitHubAPI.new("")

api.fetch_owner_info("foo").should be_nil
end
end
end
end
12 changes: 6 additions & 6 deletions src/fetchers/github_api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ struct Shardbox::GitHubAPI
metadata
end

def fetch_owner_info(login : String) : Hash(String, JSON::Any)
def fetch_owner_info(login : String) : Hash(String, JSON::Any)?
response = query(query_owner_info, {login: login})

data = parse_github_graphql(response, "repositoryOwner") do |pull|
Hash(String, JSON::Any).new(pull)
end

raise FetchError.new("Invalid response") unless data

data
end

Expand All @@ -60,8 +58,10 @@ struct Shardbox::GitHubAPI
when "data"
pull.read_object do |key|
if key == expected_key
return pull.read_null_or do
yield pull
if pull.kind.null?
return nil
else
return yield pull
end
else
pull.skip
Expand All @@ -80,7 +80,7 @@ struct Shardbox::GitHubAPI
end
end

nil
raise FetchError.new("Invalid response")
rescue exc : JSON::ParseException
raise FetchError.new("Invalid response", cause: exc)
end
Expand Down
14 changes: 14 additions & 0 deletions src/service/create_owner.cr
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ struct Service::CreateOwner

def self.fetch_owner_info_github(owner, github_api)
data = github_api.fetch_owner_info(owner.slug)
unless data
# Skip if data could not be determined (for example GitHub API returns null when the owner was renamed)

Raven.send_event Raven::Event.new(
level: :info,
message: "GitHub API returned null for owner",
tags: {
owner: owner.slug,
}
)

return
end

data.each do |key, value|
case key
when "bio", "description"
Expand Down

0 comments on commit 397ea30

Please sign in to comment.