-
-
Save holysugar/6a490bc855df2db2a60b to your computer and use it in GitHub Desktop.
ght
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'thor' | |
require 'octokit' | |
class GHT < Thor | |
include Thor::Actions | |
class_option :access_token | |
class_option :org, aliases: '-o', desc: 'organization' | |
desc 'members', 'print all members' | |
method_option :verbose, aliases: '-v', type: :boolean, default: false, desc: 'more informations (and uses more rate limit...)' | |
def members | |
all = client.org_members(org) | |
if options[:verbose] | |
all.each do |mem| | |
u = client.user(mem.id) | |
puts [ | |
mem.login, | |
u.email, | |
u.name, | |
u.location, | |
].join("\t") | |
end | |
else | |
all.each{|mem| puts mem.login } | |
end | |
end | |
desc 'collaborators', 'print all collaborators' | |
method_option :verbose, aliases: '-v', type: :boolean, default: false, desc: 'more informations (and uses more rate limit...)' | |
def collaborators | |
repos = client.org_repos(org) | |
member_logins = client.org_members(org).map(&:login) | |
collaborator_table = repos.each_with_object(Hash.new{|hash,k| hash[k]=[] }) do |repo, result| | |
accessors = client.collaborators(repo.id) | |
collaborator_logins = accessors.map(&:login) - member_logins | |
collaborator_logins.each do |login| | |
result[login] << repo.name | |
end | |
end | |
collaborator_table.each do |login, repos| | |
puts [login, repos.join(',')].join("\t") | |
end | |
end | |
desc 'limit', 'print rate limit remaining' | |
def limit | |
lim = client.rate_limit | |
reset_in_minutes = (lim.resets_in / 60) + 1 | |
puts "#{lim.remaining}/#{lim.limit} (resets in #{reset_in_minutes} min)" | |
end | |
private | |
def client | |
@client ||= begin | |
Octokit.auto_paginate = true | |
access_token = options[:access_token] || ENV["ACCESS_TOKEN"] || exit_with_access_token_message! | |
client = Octokit::Client.new(access_token: access_token) | |
client.login | |
client | |
end | |
end | |
def exit_with_access_token_message! | |
warn <<-EOD | |
open https://github.com/settings/tokens | |
then create your token with "repo" and "read:org" scopes. | |
and set your ACCESS_TOKEN environment variable or --access_token command-line option. | |
EOD | |
exit 1 | |
end | |
def org | |
options[:org] || ENV['GHT_ORG'] || client.orgs.first.login | |
end | |
end | |
GHT.start |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gem::Specification.new do |spec| | |
spec.name = "ght" | |
spec.version = "0.0.1" | |
spec.authors = ["holysugar"] | |
spec.email = ["holysugar@gmail.com"] | |
spec.summary = "GitHub Utility Tools" | |
spec.files = ["ght"] | |
spec.bindir = "." | |
spec.executables = ["ght"] | |
spec.require_path = "." | |
spec.add_dependency 'thor' | |
spec.add_dependency 'octokit' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
install
usage
export ACCESS_TOKEN=YOUR_GITHUB_ACCESS_TOKEN ght members -o YOUR_ORG_NAME
about access token
open https://github.com/settings/tokens
then create your token with "repo" and "read:org" scopes.
and set your ACCESS_TOKEN environment variable or --access_token command-line option.