Class: Organization

Inherits:
Object
  • Object
show all
Defined in:
lib/actions/orgs.rb

Overview

Class containing actions available inside an organization

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.shell_prompt(config) ⇒ Object

CLI prompt, info about the current (CLI) scope

Parameters:

  • config (Hash)

    user configuration tracking current org, repo, etc.



16
17
18
19
20
21
22
# File 'lib/actions/orgs.rb', line 16

def self.shell_prompt(config)
  if config['Repo'].nil?
    Rainbow("#{config['User']}> ").aqua << Rainbow("#{config['Org']}> ").magenta
  else
    Rainbow("#{config['User']}> ").aqua << Rainbow("#{config['Org']}> ").magenta << Rainbow("#{config['Repo']}> ").color(236, 151, 21)
  end
end

Instance Method Details

#add_members(client, config, members) ⇒ Object

Add members typing them separately

Examples:

add two members to current org (separated with commas)

User > Org > invite_member member1, member2

add two members to current org (separated with blanks)

User > Org > invite_member member1 member2

add members to current org (commas and blanks combined)

User > Org > invite_member member1, member2 member4, member5

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/actions/orgs.rb', line 326

def add_members(client, config, members)
  if members.empty?
    puts Rainbow('Please type each member you would like to add.').color(INFO_CODE)
  else
    people = split_members(members)
    spinner = custom_spinner('Adding member(s) :spinner ...')
    people.each do |i|
      options = { role: 'member', user: i.to_s }
      client.update_organization_membership(config['Org'].to_s, options)
    end
    spinner.stop(Rainbow('done!').color(4, 255, 0))
  end
rescue StandardError => exception
  puts Rainbow(exception.message.to_s).color(ERROR_CODE)
end

#add_members_from_file(client, config, path) ⇒ Object

Add members from file (JSON). File must be located somewhere in HOME.

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.



346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/actions/orgs.rb', line 346

def add_members_from_file(client, config, path)
  file_path = "#{Dir.home}#{path}"
  members_json = File.read(file_path)
  members_file = JSON.parse(members_json)
  spinner = custom_spinner('Adding members from file :spinner ...')
  members_file['members'].each do |member|
    options = { role: 'member', user: member['id'].to_s }
    client.update_organization_membership(config['Org'].to_s, options)
  end
  spinner.stop(Rainbow('done!').color(4, 255, 0))
rescue Errno::ENOENT
  puts Rainbow('Could not open file, check file location.').color(ERROR_CODE)
rescue StandardError => exception
  puts Rainbow(exception.message.to_s).color(ERROR_CODE)
end

#build_cd_syntax(type, name) ⇒ Object

Builds final cd syntax. Transforms user’s CLI input to valid Ruby expression doing some parsing.

Parameters:

  • type (String)

    scope of cd command

  • name (String, Regexp)

    name of repo, org, team etc. inside current organization



28
29
30
31
32
33
34
35
# File 'lib/actions/orgs.rb', line 28

def build_cd_syntax(type, name)
  syntax_map = { 'repo' => "Organization.new.cd('repo', #{name}, client, env)",
                 'team' => "Organization.new.cd('team', #{name}, client, env)" }
  unless syntax_map.key?(type)
    raise Rainbow("cd #{type} currently not supported.").color(ERROR_CODE)
  end
  syntax_map[type]
end

#cd(type, name, client, enviroment) ⇒ Object

cd method contains a hash representing where user can navigate depending on context. In this case

inside an organization user is allowed to change to a org repo and org team.
In order to add new 'cd types' see example below.

Examples:

add ‘chuchu’ cd scope

add to cd_scopes = {'chuchu => method(:cd_chuchu)}
call it with (name, client, enviroment) parameters

Parameters:

  • type (String)

    name of the navigating option (repo, org, team, etc.)

  • name (String, Regexp)

    String or Regexp to find the repository.



615
616
617
618
# File 'lib/actions/orgs.rb', line 615

def cd(type, name, client, enviroment)
  cd_scopes = { 'repo' => method(:cd_repo), 'team' => method(:cd_team) }
  cd_scopes[type].call(name, client, enviroment)
end

#cd_repo(name, client, enviroment) ⇒ ShellContext

perform cd to repo scope, if ‘name’ is a Regexp, matches are stored and then we let the user select one

if is not a Regexp, check that the string provided is a vale repository name

Parameters:

  • name (String, Regexp)

    repo name

  • client (Object)

    Octokit client object

  • enviroment (ShellContext)

    contains the shell context, including Octokit client and user config

Returns:

  • (ShellContext)

    changed context is returned if there is not an error during process.



514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/actions/orgs.rb', line 514

def cd_repo(name, client, enviroment)
  if name.class == Regexp
    pattern = Regexp.new(name.source, name.options)
    org_repos = []
    org_repos_url = {}
    spinner = custom_spinner("Matching #{enviroment.config['Org']} repositories :spinner ...")
    spinner.auto_spin
    client.organization_repositories(enviroment.config['Org'].to_s).each do |org_repo|
      if pattern.match(org_repo[:name].to_s)
        org_repos << org_repo[:name]
        org_repos_url[org_repo[:name].to_s] = org_repo[:html_url]
      end
    end
    spinner.stop(Rainbow('done!').color(4, 255, 0))
    if org_repos.empty?
      puts Rainbow("No repository matched with #{name.source} inside organization #{enviroment.config['Org']}").color(WARNING_CODE)
      puts
      return
    else
      prompt = TTY::Prompt.new
      answer = prompt.select('Select desired organization repository', org_repos)
      enviroment.config['Repo'] = answer
      enviroment.config['repo_url'] = org_repos_url[answer]
      enviroment.deep = Organization
    end
  else
    if client.repository?("#{enviroment.config['Org']}/#{name}")
      org_repo_url = 'https://github.com/' << enviroment.config['Org'].to_s << '/' << name.to_s
      enviroment.config['Repo'] = name
      enviroment.config['repo_url'] = org_repo_url
      enviroment.deep = Organization
    else
      puts Rainbow("Maybe #{name} is not an organizaton or currently does not exist.").color(WARNING_CODE)
      return
    end
  end
  enviroment
end

#cd_team(name, client, enviroment) ⇒ ShellContext

perform cd to team scope, first we retrieve all user’s orgs, then we check ‘name’

if its a Regexp then matches are displayed on screen and user selects one (if no match warning
is shown and we return nil)
if 'name' is not a Regexp then it must be the full team's name so we check that is inside org_teams
Last option is showing a warning and return nil (so we dont push to the stack_context)

Parameters:

  • name (String, Regexp)

    team name

  • client (Object)

    Octokit client object

  • enviroment (ShellContext)

    contains the shell context, including Octokit client and user config

Returns:

  • (ShellContext)

    changed context is returned if there is not an error during process.



563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/actions/orgs.rb', line 563

def cd_team(name, client, enviroment)
  org_teams = []
  org_teams_id = {}
  spinner = custom_spinner("Fetching #{enviroment.config['Org']} teams :spinner ...")
  spinner.auto_spin
  client.organization_teams(enviroment.config['Org'].to_s).each do |team|
    org_teams << team[:name]
    org_teams_id[team[:name].to_s] = team[:id]
  end
  spinner.stop(Rainbow('done!').color(4, 255, 0))
  if name.class == Regexp
    pattern = Regexp.new(name.source, name.options)
    name_matches = []
    org_teams.each do |team_name|
      name_matches << team_name if pattern.match(team_name.to_s)
    end
    if name_matches.empty?
      puts Rainbow("No team matched with \/#{name.source}\/ inside organization #{enviroment.config['Org']}").color(WARNING_CODE)
      puts
      return
    else
      prompt = TTY::Prompt.new
      answer = prompt.select('Select desired organization', name_matches)
      enviroment.config['Team'] = answer
      enviroment.config['TeamID'] = org_teams_id[answer]
      enviroment.config['team_url'] = 'https://github.com/orgs/' << enviroment.config['Org'] << '/teams/' << enviroment.config['Team']
      enviroment.deep = Team
    end
  else
    if org_teams.include?(name)
      enviroment.config['Team'] = name
      enviroment.config['TeamID'] = org_teams_id[name]
      enviroment.config['team_url'] = 'https://github.com/orgs/' << enviroment.config['Org'] << '/teams/' << enviroment.config['Team']
      enviroment.deep = Team
    else
      puts Rainbow("Maybe #{name} is not a #{enviroment.config['Org']} team or currently does not exist.").color(WARNING_CODE)
      puts
      return
    end
  end
  enviroment
end

#change_to_private_repo(client, config, params) ⇒ Object

Set organization repos privacy to private. You need a paid plan to set private repos inside an organization

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • params (Regexp)

    regexp to change privacy of matching repos.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/actions/orgs.rb', line 101

def change_to_private_repo(client, config, params)
  pattern = build_regexp_from_string(params)
  spinner = custom_spinner('Setting private repos :spinner ...')
  spinner.auto_spin
  repos = []
  client.organization_repositories(config['Org'].to_s).each do |repo|
    repos.push(repo[:name]) if pattern.match(repo[:name])
  end
  repos.each do |i|
    client.set_private("#{config['Org']}/#{i}")
  end
  spinner.stop(Rainbow('done!').color(4, 255, 0))
rescue StandardError => exception
  puts Rainbow(exception.message.to_s).color(ERROR_CODE)
end

#change_to_public_repo(client, config, params) ⇒ Object

Set organization repos privacy to public.

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • params (Regexp)

    regexp to change privacy of matching repos.



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/actions/orgs.rb', line 122

def change_to_public_repo(client, config, params)
  pattern = build_regexp_from_string(params)
  spinner = custom_spinner('Setting public repos :spinner ...')
  spinner.auto_spin
  repos = []
  client.organization_repositories(config['Org'].to_s).each do |repo|
    repos.push(repo[:name]) if pattern.match(repo[:name])
  end
  repos.each do |i|
    client.set_public("#{config['Org']}/#{i}")
  end
  spinner.stop(Rainbow('done!').color(4, 255, 0))
rescue StandardError => exception
  puts Rainbow(exception.message.to_s).color(ERROR_CODE)
end

#clone_repository(enviroment, repo_name, custom_path) ⇒ Object

Clone a repository, if repo_name is Regexp it will clone all org repositories matching pattern

if repo_name is String searches for that repo and clones it

Parameters:

  • enviroment (ShellContext)

    contains the shell context, including Octokit client and user config

  • repo_name (Regexp, String)

    pattern or name of repo

  • custom_path (String)

    if is not provided default path is HOME/ghedsh-cloned else is some path under HOME (already existing or not)



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/actions/orgs.rb', line 274

def clone_repository(enviroment, repo_name, custom_path)
  client = enviroment.client
  config = enviroment.config
  repos_to_clone = []
  if repo_name.include?('/')
    pattern = build_regexp_from_string(repo_name)
    client.organization_repositories(config['Org'].to_s).each do |repo|
      repos_to_clone << { name: repo[:name], ssh_url: repo[:clone_url] } if pattern.match(repo[:name])
    end
    puts Rainbow("No repository matched \/#{pattern.source}\/").color(INFO_CODE) if repos_to_clone.empty?
  else
    repo = client.repository("#{config['Org']}/#{repo_name}")
    repos_to_clone << { name: repo[:name], ssh_url: repo[:clone_url] }
  end
  unless repos_to_clone.empty?
    perform_git_clone(repos_to_clone, custom_path)
    if custom_path.nil?
      puts Rainbow("Cloned into #{Dir.pwd}").color(INFO_CODE).underline
    else
      puts Rainbow("Cloned into #{Dir.home}#{custom_path}").color(INFO_CODE).underline
    end
    puts
  end
rescue StandardError => exception
  puts Rainbow(exception.message.to_s).color(ERROR_CODE)
  puts
end

#create_issue(config) ⇒ Object

Open default browser ready to create an issue with GitHub form.

Examples:

create issue

User > Org > Repo > new_issue

Parameters:

  • config (Hash)

    user configuration tracking current org, repo, etc.



307
308
309
310
311
312
313
314
# File 'lib/actions/orgs.rb', line 307

def create_issue(config)
  if config['Repo']
    issue_creation_url = "https://github.com/#{config['Org']}/#{config['Repo']}/issues/new"
    open_url(issue_creation_url)
  else
    puts Rainbow('Change to repo in order to create an issue.').color(INFO_CODE)
  end
end

#create_repo(enviroment, repo_name, options) ⇒ Object

Repo creation: creates new repo inside current org

Parameters:

  • enviroment (ShellContext)

    contains the shell context, including Octokit client and user config

  • repo_name (String)

    repository name

  • options (Hash)

    repository options (repo organization, visibility, etc)



661
662
663
664
665
666
667
668
669
# File 'lib/actions/orgs.rb', line 661

def create_repo(enviroment, repo_name, options)
  client = enviroment.client
  options[:organization] = enviroment.config['Org'].to_s
  client.create_repository(repo_name, options)
  puts Rainbow('Repository created correctly!').color(79, 138, 16)
rescue StandardError => exception
  puts Rainbow(exception.message.to_s).color(ERROR_CODE)
  puts
end

#create_team(client, config, params) ⇒ Object

Team creation: opens team creation page on default browser when calling new_team

with no options. If option provided, it must be the directory and name of the JSON file
somewhere in your HOME directory for bulk creation of teams and members.

Examples:

Open current org URL and create new team

User > Org > new_team

Create multiple teams with its members inside current org

User > Org > new_team (HOME)/path/to/file/creation.json

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • params (Array<String>)

    user specified parameters, if not nil must be path to teams JSON file.



700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'lib/actions/orgs.rb', line 700

def create_team(client, config, params)
  if params.nil?
    team_creation_url = "https://github.com/orgs/#{config['Org']}/new-team"
    open_url(team_creation_url)
  else
    members_not_added = []
    begin
      file_path = "#{Dir.home}#{params}"
      teams_json = File.read(file_path)
      teams_file = JSON.parse(teams_json)
      spinner = custom_spinner("Creating teams in #{config['Orgs']} :spinner ...")
      teams_file['teams'].each do |team|
        # assigned to 'created_team' to grab the ID (and use it for adding members)
        # of the newly created team
        created_team = client.create_team(config['Org'].to_s,
                                          name: team['name'].to_s,
                                          privacy: team['privacy'])
        team['members'].each do |member|
          member_addition = client.add_team_member(created_team[:id], member)
          members_not_added.push(member) if member_addition == false # if !!member_adition
        end
      end
      spinner.stop(Rainbow('done!').color(4, 255, 0))
      puts Rainbow('Teams created correctly!').color(79, 138, 16)
      puts Rainbow("Could not add following members: #{members_not_added}").color(WARNING_CODE) unless members_not_added.empty?
    rescue StandardError => e
      puts Rainbow(e.message.to_s).color(ERROR_CODE)
    end
  end
end

#delete_member(client, config, param) ⇒ Object

Removes a group of members form current organization. It is possible to specify them in a file or with Regexp to match GitHub IDs.

if Regexp will remove matching GitHub IDs from organization

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • param (String, Regexp)

    if string must be file path to remove memebers from file



369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/actions/orgs.rb', line 369

def delete_member(client, config, param)
  permissions = client.organization_membership(config['Org'], opts = { user: client. })
  unless permissions[:role] == 'admin'
    puts Rainbow("You must have Admin permissions on #{config['Org']} to run this command.").underline.color(WARNING_CODE)
    return
  end
  if is_file?("#{Dir.home}#{param}")
    puts 'Removing member/members from file.'
    file_path = "#{Dir.home}#{param}"
    remove_json = File.read(file_path)
    remove_member_file = JSON.parse(remove_json)
    remove_member_file['remove'].each do |member|
      client.remove_organization_member(congif['Org'], member['id'].to_s)
    end
  elsif eval(param).is_a?(Regexp)
    members_to_remove = []
    pattern = build_regexp_from_string(param)
    client.organization_members(config['Org'].to_s).each do |member|
      members_to_remove.push(member[:login]) if pattern.match(member[:login])
    end
    if members_to_remove.empty?
      puts Rainbow("No members to remove matched with \/#{pattern.source}\/").color(WARNING_CODE)
    else
      members_to_remove.each do |i|
        client.remove_organization_member(congif['Org'], i.to_s)
      end
    end
  end
rescue SyntaxError => e
  puts Rainbow('Parameter is not a file and there was a Syntax Error building Regexp.').color(ERROR_CODE)
rescue StandardError => e
  puts Rainbow(e.message.to_s).color(ERROR_CODE)
end

#foreach_eval(client, config, params) ⇒ Object

Evaluate a bash command in each submodule

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • params (Array<String>)

    bash command



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/actions/orgs.rb', line 190

def foreach_eval(client, config, params)
  if config['Repo']
    return unless repo_has_submodules?(client, config)
    command = params.join(' ')
    FileUtils.cd(Dir.pwd) do
      system("git submodule foreach '#{command} || :'")
    end
  else
    puts Rainbow('Please change to an organization repository to run this command.').color(INFO_CODE)
    return
  end
rescue StandardError => e
  puts Rainbow(e.message.to_s).color(ERROR_CODE)
end

#foreach_try(client, config, params) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/actions/orgs.rb', line 205

def foreach_try(client, config, params)
  if config['Repo']
    return unless repo_has_submodules?(client, config)
    command = params.join(' ')
    FileUtils.cd(Dir.pwd) do
      system("git submodule foreach '#{command}'")
    end
  else
    puts Rainbow('Please change to an organization repository to run this command.').color(INFO_CODE)
    return
  end
rescue StandardError => e
  puts Rainbow(e.message.to_s).color(ERROR_CODE)
end

#invite_all_outside_collaborators(client, config, param) ⇒ Object

Invite as members all outside collaborators from organization. This action needs admin permissions on the organization. This method checks first if parameter is an existing file, then checks Regexp, else invites all outside collaborators of current organization.

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • param (String, Regexp)

    file path or Regexp to invite outside collabs



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/actions/orgs.rb', line 411

def invite_all_outside_collaborators(client, config, param)
  permissions = client.organization_membership(config['Org'], opts = { user: client. })
  unless permissions[:role] == 'admin'
    puts Rainbow("You must have Admin permissions on #{config['Org']} to run this command.").underline.color(WARNING_CODE)
    return
  end
  outside_collaborators = []
  spinner = custom_spinner('Sending invitations :spinner ...')
  if is_file?("#{Dir.home}#{param}")
    puts 'Adding outside collaborators from file'
    file_path = "#{Dir.home}#{param}"
    collab_json = File.read(file_path)
    collab_file = JSON.parse(collab_json)
    collab_file['collabs'].each do |collab|
      outside_collaborators.push(collab['id'])
    end
  elsif eval(param).is_a?(Regexp)
    pattern = build_regexp_from_string(param)
    client.outside_collaborators(config['Org']).each do |i|
      outside_collaborators.push(i[:login]) if pattern.match(i[:login])
    end
  else
    begin
      client.outside_collaborators(config['Org']).each do |i|
        outside_collaborators.push(i[:login])
      end
    rescue StandardError => exception
      puts Rainbow('If you entered file path, please ensure that is the correct path.').color(ERROR_CODE)
    end
  end
  outside_collaborators.each do |j|
    options = { role: 'member', user: j.to_s }
    client.update_organization_membership(config['Org'], options)
  end
  spinner.stop(Rainbow('done!').color(4, 255, 0))
rescue SyntaxError => e
  puts Rainbow('Parameter is not a file and there was a Syntax Error building Regexp.').color(ERROR_CODE)
rescue StandardError => exception
  puts Rainbow(exception.message.to_s).color(ERROR_CODE)
end

#new_eval(client, config, params) ⇒ Object

Create a ‘super repo’ containing assignment repositories as submodules

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • params (Array<String>)

    ‘super repo’s’ name and Regexp to match submodules params is the evaluation repository’s name params Regexp of submodules



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/actions/orgs.rb', line 145

def new_eval(client, config, params)
  options = { private: true, organization: config['Org'].to_s, description: 'Evaluation repository created using ghedsh.' }
  repo_name = params[0]
  submodules = []
  evaluation_repo = client.create_repository(repo_name, options)
  pattern = build_regexp_from_string(params[1])
  client.organization_repositories(config['Org'].to_s).each do |repo|
    submodules << repo[:ssh_url] if pattern.match(repo[:name]) && (repo[:name] != repo_name)
  end
  unless submodules.empty?
    local_repo_path = "#{Dir.pwd}/#{repo_name}"
    FileUtils.mkdir_p(local_repo_path)
    FileUtils.cd(local_repo_path) do
      system('git init')
      submodules.each do |i|
        system("git submodule add #{i}")
      end
      system('git add .')
      system('git commit -m "First commit"')
      system("git remote add origin #{evaluation_repo[:ssh_url]}")
      system('git push -u origin master')
    end
  end
  puts Rainbow("No submodule found with /#{pattern.source}/") if submodules.empty?
rescue StandardError => e
  puts Rainbow(e.message.to_s).color(ERROR_CODE)
end

#open_info(config, params, client) ⇒ Object

Open info on default browser.

If ‘params’ is String or Regexp user selects referred organization members

Examples:

open user profile URL with String provided

User > Org > open 'some_user'

open user URL with Regexp provided

User > Org > open /pattern/

Parameters:

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • client (Object)

    Octokit client object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/actions/orgs.rb', line 48

def open_info(config, params, client)
  unless params.nil?
    # looking for org member by regexp
    pattern = build_regexp_from_string(params)
    member_url = select_member(config, pattern, client)
    open_url(member_url.to_s) unless member_url.nil?
    return
  end

  if config['Repo'].nil?
    open_url(config['org_url'].to_s)
  else
    open_url(config['repo_url'].to_s)
  end
rescue StandardError => exception
  puts Rainbow(exception.message.to_s).color(ERROR_CODE)
  puts
end

#read_orgs(client) ⇒ Object



740
741
742
743
744
745
746
747
748
# File 'lib/actions/orgs.rb', line 740

def read_orgs(client)
  orgslist = []
  org = client.organizations
  org.each do |i|
    o = eval(i.inspect)
    orgslist.push(o[:login])
  end
  orgslist
end

#remove_org_memberObject

Removes a group of GitHub users from an organization



687
# File 'lib/actions/orgs.rb', line 687

def remove_org_member; end

#remove_repo(enviroment, repo_name) ⇒ Object

Removes repository by name

Parameters:

  • enviroment (ShellContext)

    contains the shell context, including Octokit client and user config

  • repo_name (String)

    name of the repo to be removed



675
676
677
678
679
680
681
682
# File 'lib/actions/orgs.rb', line 675

def remove_repo(enviroment, repo_name)
  client = enviroment.client
  client.delete_repository("#{enviroment.config['Org']}/#{repo_name}")
  puts Rainbow('Repository deleted.').color(INFO_CODE)
rescue StandardError => exception
  puts Rainbow(exception.message.to_s).color(ERROR_CODE)
  puts
end

#remove_team(config) ⇒ Object

Open org’s team list and delete manually (faster than checking teams IDs and delete it)

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.



735
736
737
738
# File 'lib/actions/orgs.rb', line 735

def remove_team(config)
  teams_url = "https://github.com/orgs/#{config['Org']}/teams"
  open_url(teams_url)
end

#repo_has_submodules?(client, config) ⇒ Boolean

Returns:

  • (Boolean)


173
174
175
176
177
178
179
180
181
182
183
# File 'lib/actions/orgs.rb', line 173

def repo_has_submodules?(client, config)
  repo_content = []
  client.contents("#{config['Org']}/#{config['Repo']}").each do |i|
    repo_content << i[:name]
  end
  unless repo_content.include?('.gitmodules')
    puts Rainbow('Current repo does not include .gitmodules and command will not work.').color(WARNING_CODE)
    return false
  end
  true
end

#show_commits(enviroment, params) ⇒ Object

Show commits of current repo. If user is not in a repo, repository name for commit showing must be provided.

Parameters:

  • enviroment (ShellContext)

    contains the shell context, including Octokit client and user config

  • params (Array<String>)

    if user is not on a repo then params is repo name. Optionally, branch can be specified (value is in params), if not provided default branch is master



625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
# File 'lib/actions/orgs.rb', line 625

def show_commits(enviroment, params)
  options = {}
  if !enviroment.config['Repo'].nil?
    repo = enviroment.config['Repo']
    options[:sha] = if params.empty?
                      'master'
                    else
                      params[0]
                    end
  else
    repo = params[0]
    options[:sha] = if params[1].nil?
                      'master'
                    else
                      params[1]
                    end
  end
  begin
    enviroment.client.commits("#{enviroment.config['Org']}/#{repo}", options).each do |i|
      puts "\tSHA: #{i[:sha]}"
      puts "\t\t Commit date: #{i[:commit][:author][:date]}"
      puts "\t\t Commit author: #{i[:commit][:author][:name]}"
      puts "\t\t\t Commit message: #{i[:commit][:message]}"
      puts
    end
  rescue StandardError => exception
    puts exception
    puts Rainbow("If you are not currently on a repo, USAGE TIP: `commits <repo_name> [branch_name]` (default: 'master')").color(INFO_CODE)
  end
end

#show_files(client, config, params) ⇒ Object

Display files and directories within a repository

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/actions/orgs.rb', line 225

def show_files(client, config, params)
  if config['Repo']
    options = { path: '' }
    options[:path] = params[0] unless params.empty?
    file_names_and_types = []
    client.contents("#{config['Org']}/#{config['Repo']}", options).each do |i|
      file_names_and_types << "#{i[:name]} (#{i[:type]})"
    end
    file_names_and_types.sort_by!(&:downcase)
    puts file_names_and_types
  else
    puts Rainbow('Please change to organization repository to see its files.').color(INFO_CODE)
  end
rescue StandardError => e
  puts Rainbow(e.message.to_s).color(ERROR_CODE)
end

#show_issues(config) ⇒ Object

Open default browser and shows open issues

Examples:

open issue’s list

User > Org > Repo > issues

Parameters:

  • config (Hash)

    user configuration tracking current org, repo, etc.



457
458
459
460
461
462
463
464
# File 'lib/actions/orgs.rb', line 457

def show_issues(config)
  if config['Repo']
    issues_url = "https://github.com/#{config['Org']}/#{config['Repo']}/issues"
    open_url(issues_url)
  else
    puts Rainbow('Change to repo in order to view all issues').color(INFO_CODE)
  end
end

#show_people(client, config, params) ⇒ Object

Display organization people. It shows all members and if the authenticated user is org admin

also displays outside collaborators.

Examples:

Display organization people

User > Org > people

Display people matching Regexp

User > Org > people /alu/

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • params (Regexp)

    if provided, it must be a Regexp



476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
# File 'lib/actions/orgs.rb', line 476

def show_people(client, config, params)
  spinner = custom_spinner("Fetching #{config['Org']} people :spinner ...")
  spinner.auto_spin
  org_members = []
  client.organization_members(config['Org'].to_s).each do |member|
    org_members << [member[:login], 'member']
  end
  membership = {}
  client.organization_membership(config['Org'].to_s).each do |key, value|
    membership[key] = value
  end
  if membership[:role] == 'admin'
    client.outside_collaborators(config['Org'].to_s).each do |collab|
      org_members << [collab[:login], 'outside collaborator']
    end
  end
  spinner.stop(Rainbow('done!').color(4, 255, 0))
  if params.nil?
    table = Terminal::Table.new headings: ['Github ID', 'Role'], rows: org_members
    puts table
  else
    unless params.include?('/')
      raise Rainbow('Parameter must be a Regexp. Example: /pattern/').color(ERROR_CODE)
      return
    end
    pattern = build_regexp_from_string(params)
    occurrences = build_item_table(org_members, pattern)
    puts Rainbow("No member inside #{config['Org']} matched  \/#{pattern.source}\/").color(INFO_CODE) if occurrences.zero?
  end
end

#show_repos(client, config, params) ⇒ Object

Display organization repos

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • params (Regexp)

    regexp to check repo name



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/actions/orgs.rb', line 72

def show_repos(client, config, params)
  spinner = custom_spinner("Fetching #{config['Org']} repositories :spinner ...")
  spinner.auto_spin
  org_repos = []
  client.organization_repositories(config['Org'].to_s).each do |repo|
    org_repos << repo[:name]
  end
  org_repos.sort_by!(&:downcase)
  spinner.stop(Rainbow('done!').color(4, 255, 0))
  if params.nil?
    item_counter = 0
    org_repos.each do |repo_name|
      puts repo_name
      item_counter += 1
    end
    puts "\n#{item_counter} #{config['Org']} repositories listed."
  else
    pattern = build_regexp_from_string(params)
    occurrences = show_matching_items(org_repos, pattern)
    puts Rainbow("No repository inside #{config['Org']} matched  \/#{pattern.source}\/").color(INFO_CODE) if occurrences.zero?
    puts "\n#{occurrences} #{config['Org']} repositories listed."
  end
end

#show_teams(client, config, params) ⇒ Object

Display organization teams

Parameters:

  • client (Object)

    Octokit client object

  • config (Hash)

    user configuration tracking current org, repo, etc.

  • params (Regexp)

    regexp to check team name



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/actions/orgs.rb', line 247

def show_teams(client, config, params)
  org_teams = []
  spinner = custom_spinner("Fetching #{config['Org']} teams :spinner ...")
  spinner.auto_spin
  client.organization_teams(config['Org'].to_s).each do |team|
    org_teams << team[:name]
  end
  org_teams.sort_by!(&:downcase)
  spinner.stop(Rainbow('done!').color(4, 255, 0))
  if params.nil?
    org_teams.each do |name|
      puts name
    end
  else
    pattern = build_regexp_from_string(params)
    occurrences = show_matching_items(org_teams, pattern)
    puts Rainbow("No team inside #{config['Org']} matched  \/#{pattern.source}\/").color(INFO_CODE) if occurrences.zero?
  end
end