Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Max Larionov - Github-team-remover progress(Finished.) #3

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
github-team-remover (0.0.2)
colorize
octokit (~> 3.0)
thor

Expand All @@ -10,12 +11,13 @@ GEM
specs:
addressable (2.3.6)
ast (1.1.0)
colorize (0.7.2)
diff-lcs (1.2.5)
faraday (0.9.0)
multipart-post (>= 1.2, < 3)
json (1.8.1)
multipart-post (2.0.0)
octokit (3.0.0)
octokit (3.1.0)
sawyer (~> 0.5.3)
parser (2.1.7)
ast (~> 1.1)
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

```ruby
github-team-remover \
--organization FlatstackSchool \
--login github-login \
--password github-password
--organization FlatstackSchool, or --o \
--login github-login, or --l \
--password github-password, or --p \
optional
--color color, like blue, or --c \
--verbose or --v
--delete or --d
```

## Install
Expand Down
1 change: 1 addition & 0 deletions bin/github-team-remover
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
require 'bundler/setup'

require 'rubygems'
require 'remover'
Expand Down
1 change: 1 addition & 0 deletions github-team-remover.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Gem::Specification.new do |spec|

spec.add_dependency 'octokit', '~> 3.0'
spec.add_dependency 'thor'
spec.add_dependency 'colorize'

spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'rspec', '~> 3.0.0.beta2'
Expand Down
28 changes: 28 additions & 0 deletions github-team-remover.gemspec~
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

require 'remover/version'

Gem::Specification.new do |spec|
spec.name = "github-team-remover"
spec.version = Remover::VERSION
spec.authors = ["Flatstack School"]
spec.email = ["[email protected]"]
spec.summary = %q{Show empty teams on github}
spec.description = %q{Show empty teams on github. Teams without members or tems without repositories.}
spec.homepage = "http://github.com/FlatstackSchool/github-team-remover"
spec.license = "MIT"

spec.files = `git ls-files -z`.split("\x0")
spec.executables = ['github-team-remover']
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency 'octokit', '~> 3.0'
spec.add_dependency 'thor'
spec.add_dependency 'colorize'

spec.add_development_dependency 'rubocop'
spec.add_development_dependency 'rspec', '~> 3.0.0.beta2'
end
1 change: 1 addition & 0 deletions lib/remover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'remover/github'
require 'remover/team'
require 'remover/list'
require 'remover/reporter'
require 'remover/cli'
require 'remover/configuration'

Expand Down
41 changes: 34 additions & 7 deletions lib/remover/cli.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
require 'thor'
require 'colorize'

module Remover
class CLI < Thor
method_option :organization, required: true
method_option :login, required: true
method_option :password, required: true
method_option :organization, aliases: '--o', required: true
method_option :login, aliases: '--l', required: true
method_option :password, aliases: '--p', required: true
method_option :color, default: :white, aliases: '--c'
method_option :verbose, aliases: '--v'
method_option :delete, aliases: '--d'

desc('list', 'List unused teams')
desc('|Commands:', 'help list for more info')

long_desc <<-LONGDESC
Optional commands:
--c color : will colorize your output.

You can use red, yellow, green, white and maybe others colors

--v => additional info, URL for users and repos

--d => will delete all unused teams
LONGDESC

def list
Remover.configuration.load_from_options!(options)

Remover::List.new(github).unused_teams.each do |team|
puts team.name
puts 'Unused teams:'.colorize(color)
Remover::List.new(github).unused_teams.each do |unused_team|
Remover::Reporter.new(unused_team, color, verbose?, delete?).report_to_cli
end
end

default_task :list

private

def delete?
true if options[:delete]
end

def verbose?
true if options[:verbose]
end

def color
Remover.configuration.color.intern
end

def github
Remover::Github.new(octokit)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/remover/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Remover
class Configuration
OPTIONS = %i(organization login password)
OPTIONS = %i(organization login password color verbose delete)

attr_accessor(*OPTIONS)

Expand Down
63 changes: 63 additions & 0 deletions lib/remover/reporter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
require 'colorize'

module Remover
class Reporter
attr_accessor :unused_team, :color, :verbose, :delete

def initialize(unused_team, color, verbose, delete)
@unused_team, @color, @verbose, @delete = unused_team, color, verbose, delete
end

def report_to_cli
puts ' ------------------------------'.colorize(color_it)
team_name
members_amount
members_url if verbose?
repos_amount
repos_url if verbose?
report_and_delete if delete?
puts ' ------------------------------'.colorize(color_it)
end

private

def verbose?
true if @verbose
end

def delete?
true if @delete
end

def team_name
puts " Team name: #{unused_team.name}".colorize(color_it)
end

def members_amount
puts " Members: #{unused_team.members_amount}".colorize(color_it)
end

def members_url
puts ' Members URL:'.colorize(color_it)
puts " #{unused_team.members_url.colorize(:yellow)}" if verbose?
end

def repos_amount
puts " Repositories: #{unused_team.repositories_amount}".colorize(color_it)
end

def repos_url
puts ' Repositories URL:'.colorize(color)
puts " #{unused_team.repositories_url.colorize(:yellow)}" if verbose?
end

def report_and_delete
puts ' !DELETED!'.colorize(:red) if delete
unused_team.delete_team
end

def color_it
color
end
end
end
38 changes: 38 additions & 0 deletions lib/remover/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,44 @@ def name
github_team.name
end

def delete_team
github_client.delete_team(github_team.id)
end

def members_url
if with_members?
users_hash = Hash(*github_client.team_members(github_team.id))
users_hash[:html_url].to_s
else
'no members'
end
end

def repositories_url
if with_repositories?
repos_hash = Hash(*github_client.team_repositories(github_team.id))
repos_hash[:html_url].to_s
else
'no repositories'
end
end

def members_amount
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please use these new methods in the with_members? and with_repositories? also?

if with_members?
github_client.team_members(github_team.id).size
else
0
end
end

def repositories_amount
if with_repositories?
github_client.team_repositories(github_team.id).size
else
0
end
end

private

def with_members?
Expand Down
78 changes: 78 additions & 0 deletions spec/remover/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
describe Remover::CLI do
let(:options) do
{ 'organization' => 'FlatSchool',
'login' => 'fs-school',
'password' => '123456',
'color' => :blue,
'verbose' => 'true',
'delete' => 'true'
}
end

let(:configuration) { Remover::Configuration.new }

describe '#color' do
before do
configuration.load_from_options!(options)
end

it 'returns true' do
expect(configuration.color).to eq(:blue)
end

context '--color not used' do
let(:options) do
{
'color' => :white
}
end

it 'is default => :white' do
expect(configuration.color).to eq(:white)
end
end
end

describe '#verbose' do
before do
configuration.load_from_options!(options)
end

it 'returns true' do
expect(configuration.verbose).to eq('true')
end

context '--verbose not used' do
let(:options) do
{
'verbose' => 'false'
}
end
it 'returns false' do
expect(configuration.verbose).not_to eq('true')
end
end
end

describe '#delete_team' do
before do
configuration.load_from_options!(options)
end

it 'returns true' do
expect(configuration.delete).to eq('true')
end

context 'if --delete key not entered' do
let(:options) do
{
'delete' => 'false'
}
end

it 'returns false' do
expect(configuration.delete).not_to eq('true')
end
end
end
end
6 changes: 5 additions & 1 deletion spec/remover/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
{
'organization' => 'FlatSchool',
'login' => 'fs-school',
'password' => '123456'
'password' => '123456',
'color' => 'blue',
'verbose' => 'true'
}
end

Expand All @@ -18,6 +20,8 @@
expect(configuration.organization).to eq('FlatSchool')
expect(configuration.login).to eq('fs-school')
expect(configuration.password).to eq('123456')
expect(configuration.color).to eq('blue')
expect(configuration.verbose).to eq('true')
end
end
end
Loading