Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pepesenaris/e1e3c355662d01a24531d3285f272711 to your computer and use it in GitHub Desktop.
Save pepesenaris/e1e3c355662d01a24531d3285f272711 to your computer and use it in GitHub Desktop.
GitHub API GraphQL - list stale branches
require 'graphlient'
client = Graphlient::Client.new('https://api.github.com/graphql',
headers: {
'Authorization' => "Bearer #{ ENV['GITHUB_API_TOKEN']}"
},
http_options: {
read_timeout: 20,
write_timeout: 30
}
)
repo_owner = ENV['GITHUB_REPO_OWNER']
repo_name = ENV['GITHUB_REPO_NAME']
# query list of branches w/ last updated date & username
result = client.query <<-GRAPHQL
query {
repository(owner: "#{repo_owner}", name: "#{repo_name}") {
refs(first: 100, refPrefix: "refs/heads/") {
nodes {
name
target {
... on Commit {
committedDate
author { user { login } }
}
}
}
}
}
}
GRAPHQL
puts result.data.repository.to_s
# list branches updated more than 12 month ago
require 'date'
branches_by_user = result.data.repository.refs.nodes.
reject { |b| b.name =~ /^master|develop$/ }.
select { |b| Date.parse(b.target.committed_date) < (Date.today << 12) }.
sort_by { |b| b.target.committed_date }.
group_by { |b| if b.target.author.user then b.target.author.user.login else 'no-author' end }
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
@pepesenaris
Copy link
Author

Updated to use a different, not deprecated, graphql client

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