-
-
Save luizrobertofreitas/255f533099f77bcebed0d79da7d94963 to your computer and use it in GitHub Desktop.
Github GraphQL query for repos, their topics, and Gemfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'httparty' | |
def get_data | |
query = File.open('repos.graphql', 'r').read | |
token = 'https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/' | |
cursor = nil | |
repos = [] | |
org = 'evil_mega_corp' | |
loop do | |
response = HTTParty.post( | |
'https://api.github.com/graphql', | |
headers: { | |
'User-Agent' => 'https://gist.github.com/kruszczynski/ac20a4a5d6e81d23bffbdfdc6f311569', | |
'Authorization' => "bearer #{token}", | |
}, | |
body: { | |
'query' => query, | |
'variables' => { | |
'org' => org, | |
'cursor' => cursor, | |
} | |
}.to_json | |
) | |
raise "Github returned #{response.code}: #{response.body}" unless response.success? | |
root = response.parsed_response.dig('data', 'organization', 'repositories') | |
repos << root['nodes'] | |
cursor = root.dig('pageInfo', 'endCursor') | |
break unless root.dig('pageInfo', 'hasNextPage') | |
end | |
return repos.flatten | |
end | |
def graphql2objects(repos) | |
repos.map do |repo| | |
OpenStruct.new({ | |
name: repo['name'], | |
gemfile: repo.dig('object', 'text'), | |
topics: repo.dig('repositoryTopics', 'nodes').map(&:values).map(&:first).flat_map(&:values), | |
}) | |
end | |
end | |
parsed_repos = graphql2objects(get_data) | |
parsed_repos.reject { |a| a.topics.include?("dead") } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query ($org: String!, $cursor: String) { | |
organization(login: $org) { | |
repositories(first: 100, after: $cursor) { | |
pageInfo { | |
endCursor | |
hasNextPage | |
} | |
nodes { | |
name | |
repositoryTopics(first: 100) { | |
nodes { | |
topic { | |
name | |
} | |
} | |
} | |
object(expression: "master:Gemfile") { | |
... on Blob { | |
text | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"org": "Evilcorp", | |
"cursor": "ASDASDASD==" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment