Skip to content

Instantly share code, notes, and snippets.

@defeated
Created May 21, 2018 21:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save defeated/34170cdf665720bcbdc844412192a48e to your computer and use it in GitHub Desktop.
Save defeated/34170cdf665720bcbdc844412192a48e to your computer and use it in GitHub Desktop.
GitHub API GraphQL - list stale branches
# https://github.com/Shopify/graphql-ruby-client
require 'graphql_client'
require 'net/http'
client = GraphQL::Client.new(Pathname.new('github-schema.json')) do
configure do |c|
c.url = 'https://api.github.com/graphql'
c.username = ENV['GITHUB_USERNAME']
c.password = ENV['GITHUB_ACCESS_TOKEN']
end
end
# query list of branches w/ last updated date & username
result = client.raw_query <<-GRAPHQL
query {
repository(owner: "#{ENV['GITHUB_REPO_OWNER']}", name: "#{ENV['GITHUB_REPO_NAME']}") {
refs(first: 100, refPrefix: "refs/heads/") {
nodes {
name
target {
... on Commit {
committedDate
author { user { login } }
}
}
}
}
}
}
GRAPHQL
# list branches updated more than 1 month ago
require 'date'
branches_by_user = result.repository.refs.nodes.
reject { |b| b.name == 'master' }.
select { |b| Date.parse(b.target.committed_date) < (Date.today << 1) }.
sort_by { |b| b.target.committed_date }.
group_by { |b| b.target.author.user.login }
branches_by_user.each do |u, branches|
puts "#{u} (#{branches.size})"
branches.each do |b|
puts "#{b.name} on #{b.target.committed_date}"
end
puts ''
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment