Skip to content

Commit

Permalink
Refactor GitHubAPI#fetch_repo_metadata to use generic #parse_github_g…
Browse files Browse the repository at this point in the history
…raphql
  • Loading branch information
straight-shoota committed May 7, 2020
1 parent 397ea30 commit 650dea4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 108 deletions.
128 changes: 59 additions & 69 deletions spec/fixtures/repo_metadata-github-graphql-response.json
Original file line number Diff line number Diff line change
@@ -1,75 +1,65 @@
{
"data": {
"repository": {
"nameWithOwner": "crystal-lang/crystal",
"forks": {
"totalCount": 964
"nameWithOwner": "crystal-lang/crystal",
"forks": {
"totalCount": 964
},
"stargazers": {
"totalCount": 13060
},
"watchers": {
"totalCount": 479
},
"createdAt": "2012-11-27T17:32:32Z",
"description": "The Crystal Programming Language",
"hasIssuesEnabled": true,
"hasWikiEnabled": true,
"homepageUrl": "https://crystal-lang.org",
"isArchived": false,
"isFork": false,
"isMirror": false,
"licenseInfo": {
"key": "other"
},
"primaryLanguage": {
"name": "Crystal"
},
"pushedAt": "2019-04-08T23:53:17Z",
"closedIssues": {
"totalCount": 3587
},
"openIssues": {
"totalCount": 717
},
"closedPullRequests": {
"totalCount": 673
},
"openPullRequests": {
"totalCount": 154
},
"mergedPullRequests": {
"totalCount": 2408
},
"repositoryTopics": {
"nodes": [
{
"topic": {
"name": "crystal"
}
},
"stargazers": {
"totalCount": 13060
{
"topic": {
"name": "language"
}
},
"watchers": {
"totalCount": 479
},
"createdAt": "2012-11-27T17:32:32Z",
"description": "The Crystal Programming Language",
"hasIssuesEnabled": true,
"hasWikiEnabled": true,
"homepageUrl": "https://crystal-lang.org",
"isArchived": false,
"isFork": false,
"isMirror": false,
"licenseInfo": {
"key": "other"
},
"primaryLanguage": {
"name": "Crystal"
},
"pushedAt": "2019-04-08T23:53:17Z",
"closedIssues": {
"totalCount": 3587
},
"openIssues": {
"totalCount": 717
},
"closedPullRequests": {
"totalCount": 673
},
"openPullRequests": {
"totalCount": 154
},
"mergedPullRequests": {
"totalCount": 2408
},
"repositoryTopics": {
"nodes": [
{
"topic": {
"name": "crystal"
}
},
{
"topic": {
"name": "language"
}
},
{
"topic": {
"name": "efficiency"
}
}
]
},
"codeOfConduct": {
"name": "Contributor Covenant",
"url": "https://github.com/crystal-lang/crystal/blob/master/CODE_OF_CONDUCT.md"
{
"topic": {
"name": "efficiency"
}
}
},
"rateLimit": {
"limit": 5000,
"cost": 1,
"remaining": 4998,
"resetAt": "2019-04-09T12:06:33Z"
}
]
},
"codeOfConduct": {
"name": "Contributor Covenant",
"url": "https://github.com/crystal-lang/crystal/blob/master/CODE_OF_CONDUCT.md"
}
}
2 changes: 1 addition & 1 deletion spec/repo_metadata_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe Repo::Metadata do
describe "for Github graphql" do
it ".from_github_graphql" do
File.open("#{__DIR__}/fixtures/repo_metadata-github-graphql-response.json", "r") do |file|
metadata = Repo::Metadata.from_github_graphql(file)
metadata = Repo::Metadata.from_json(file)
metadata.should eq Repo::Metadata.new(
forks_count: 964,
stargazers_count: 13060,
Expand Down
41 changes: 3 additions & 38 deletions src/fetchers/github_api.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ struct Shardbox::GitHubAPI
response.body
end

def fetch_repo_metadata(owner : String, name : String)
def fetch_repo_metadata(owner : String, name : String) : Repo::Metadata?
response = query(query_repo_metadata, {owner: owner, name: name})

begin
metadata = Repo::Metadata.from_github_graphql(response)
rescue exc : JSON::ParseException
raise FetchError.new("Invalid response", cause: exc)
metadata = parse_github_graphql(response, "repository") do |pull|
Repo::Metadata.new(pull)
end

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

metadata
end

Expand Down Expand Up @@ -87,37 +83,6 @@ struct Shardbox::GitHubAPI
end

struct Repo::Metadata
def self.from_github_graphql(string)
pull = JSON::PullParser.new(string)
metadata = nil
pull.read_object do |key|
case key
when "data"
pull.read_object do |key|
if key == "repository"
pull.read_null_or do
metadata = Repo::Metadata.new(github_pull: pull)
end
else
pull.skip
end
end
when "errors"
errors = [] of String
pull.read_array do
pull.on_key!("message") do
errors << pull.read_string
end
end
raise Shardbox::FetchError.new("Repository error: #{errors.join(", ")}")
else
pull.skip
end
end

metadata
end

def initialize(github_pull pull : JSON::PullParser)
pull.read_object do |key|
case key
Expand Down

0 comments on commit 650dea4

Please sign in to comment.