Skip to content

Instantly share code, notes, and snippets.

@luizrobertofreitas
Forked from kruszczynski/get_repos.rb
Created January 7, 2022 11:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luizrobertofreitas/255f533099f77bcebed0d79da7d94963 to your computer and use it in GitHub Desktop.
Save luizrobertofreitas/255f533099f77bcebed0d79da7d94963 to your computer and use it in GitHub Desktop.
Github GraphQL query for repos, their topics, and Gemfile
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") }
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
}
}
}
}
}
}
{
"org": "Evilcorp",
"cursor": "ASDASDASD=="
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment