Skip to content

Instantly share code, notes, and snippets.

@kruszczynski
Forked from jbarber/get_repos.rb
Last active June 14, 2023 15:35
Show Gist options
  • Select an option

  • Save kruszczynski/ac20a4a5d6e81d23bffbdfdc6f311569 to your computer and use it in GitHub Desktop.

Select an option

Save kruszczynski/ac20a4a5d6e81d23bffbdfdc6f311569 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=="
}
@kruszczynski
Copy link
Copy Markdown
Author

I think you might need to change the user agent and set the token up in lines 14 and 15

@PratikVerma007
Copy link
Copy Markdown

Works but Evilcorp has enables login only through oAuth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment