Skip to content

Instantly share code, notes, and snippets.

@skuroki
Created November 10, 2017 16:06
Show Gist options
  • Save skuroki/2981efb1929cee474cbbb2f2f933cebe to your computer and use it in GitHub Desktop.
Save skuroki/2981efb1929cee474cbbb2f2f933cebe to your computer and use it in GitHub Desktop.
Github API v4にgraphql-client gemを使ってアクセスする ref: http://qiita.com/skuroki@github/items/eecc454edb2ac984be25
source 'https://rubygems.org'
gem 'graphql-client' # 0.12.1
#! /usr/bin/env ruby
require 'graphql/client'
require 'graphql/client/http'
h = GraphQL::Client::HTTP.new('https://api.github.com/graphql') do
def headers(context)
{
"Authorization" => "Bearer #{ENV['ACCESS_TOKEN']}"
}
end
end
s = GraphQL::Client.load_schema(h)
c = GraphQL::Client.new(schema: s, execute: h)
Q = c.parse <<-GraphQL
query($after: String) {
repository(owner:"our_company", name:"awesome_project") {
pullRequests(first: 100, states: [MERGED], orderBy: {field: UPDATED_AT, direction: DESC}, after: $after) {
edges {
cursor
node {
number
title
updatedAt
mergedAt
author {
login
}
}
}
}
}
}
GraphQL
all_nodes = []
after = nil
loop do
r = c.query(Q, variables: { after: after })
nodes = r.data.repository.pull_requests.edges.map(&:node)
nodes_in_time = nodes.select { |n| Time.parse(n.updated_at) > Time.new(2017, 10, 30) }
all_nodes += nodes_in_time.select { |n| %w(alice bob carol).include?(n.author.login) }
break if nodes.size > nodes_in_time.size
after = r.data.repository.pull_requests.edges.last.cursor
end
puts all_nodes.map { |n| "##{n.number}\t#{n.author.login}\t#{n.merged_at}\t#{n.title}" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment