Skip to content

Instantly share code, notes, and snippets.

@pocke
Last active December 4, 2019 10:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pocke/27155765598bc912059f5b629f25664d to your computer and use it in GitHub Desktop.
Save pocke/27155765598bc912059f5b629f25664d to your computer and use it in GitHub Desktop.
require 'net/http'
require 'json'
class RequestError < StandardError
def initialize(errors)
@errors = errors
end
def message
@errors.map{|e| e[:message]}.join("\n")
end
end
def token
ENV['GITHUB_ACCESS_TOKEN']
end
def req(query)
http = Net::HTTP.new('api.github.com', 443)
http.use_ssl = true
header = {
"Authorization" => "Bearer #{token}",
'Content-Type' => 'application/json',
}
resp = http.request_post('/graphql', JSON.generate(query), header)
JSON.parse(resp.body, symbolize_names: true).tap do |content|
raise RequestError.new(content[:errors]) if content[:errors]
end
end
def rich_format(users)
users = users.select{|n| n[:sponsorsListing]}
users.map do |user|
<<~SECTION
https://github.com/sponsors/#{user[:login]}
==============
#{user[:sponsorsListing][:tiers][:nodes].map{|tier| "#{tier[:name]}\n--------\n\n#{tier[:description]}"}.join("\n\n")}
SECTION
end.join("\n\n\n")
end
def simple_format(users)
users = users.select{|n| n[:sponsorsListing]}
users.map do |user|
tiers = "(#{user[:sponsorsListing][:tiers][:nodes].map{|t| "$#{t[:monthlyPriceInDollars]}"}.join(", ")})"
"* https://github.com/sponsors/#{user[:login]}\t\t#{tiers}"
end.join("\n")
end
def fetch_users(query:)
after = nil
has_next_page = true
users = []
while has_next_page
resp = req(
query: query,
variables: {
after: after,
},
)
following = resp[:data][:viewer][:following]
users.concat(following[:nodes])
has_next_page = following[:pageInfo][:hasNextPage]
after = following[:pageInfo][:endCursor]
end
users
end
RICH_QUERY = <<~QUERY
query($after: String) {
viewer {
following(first: 100, after: $after) {
nodes {
login
sponsorsListing {
tiers(first: 10) {
nodes {
name
description
}
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
QUERY
SIMPLE_QUERY = <<~QUERY
query($after: String) {
viewer {
following(first: 100, after: $after) {
nodes {
login
sponsorsListing {
tiers(first: 10) {
nodes {
monthlyPriceInDollars
}
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
}
QUERY
if ENV['RICH']
users = fetch_users(query: RICH_QUERY)
puts rich_format(users)
else
users = fetch_users(query: SIMPLE_QUERY)
puts simple_format(users)
end
@pocke
Copy link
Author

pocke commented Dec 4, 2019

Find users who enable GitHub Sponsors from your following.

Usage

Get your personal access token from https://github.com/settings/tokens.
It needs user scope.

191204184857

$ GITHUB_ACCESS_TOEKN=<YOUR PERSONAL ACCESS TOKEN> ruby gh-sponsors.rb

# WIth rich format
$ RICH=1 GITHUB_ACCESS_TOEKN=<YOUR PERSONAL ACCESS TOKEN> ruby gh-sponsors.rb

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