-
Notifications
You must be signed in to change notification settings - Fork 0
/
github-organization-manage.rb
executable file
·123 lines (111 loc) · 4.28 KB
/
github-organization-manage.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env ruby
require 'octokit'
require 'thor'
require 'pp'
require 'yaml'
require 'net/http'
require 'uri'
require 'json'
module GithubTeamManage
class CUI < Thor
desc "get_client", "internal method for this cli program"
def get_client
Octokit.configure do |c|
c.access_token = ENV['GITHUB_ACCESS_TOKEN']
c.auto_paginate = true
end
Octokit::Client.new
end
desc "org_repos", "List organization repositories"
def org_repos
get_client.org_repos(ENV['GITHUB_ORG_NAME']).each do |repo|
puts "#{repo[:full_name]},#{repo[:private]},#{repo[:archived]}"
end
end
desc "org_members", "Get organization members"
def org_members
get_client.org_members(ENV['GITHUB_ORG_NAME'], {"role":"admin"}).each do |member|
puts "#{member[:login]},owner"
end
get_client.org_members(ENV['GITHUB_ORG_NAME'], {"role":"member"}).each do |member|
puts "#{member[:login]},member"
end
end
desc "org_collabs", "Get organization collaborators"
def org_collabs
uri = URI.parse("https://api.github.com/orgs/#{ENV['GITHUB_ORG_NAME']}/outside_collaborators")
request = Net::HTTP::Get.new(uri)
request["Authorization"] = "token #{ENV['GITHUB_ACCESS_TOKEN']}"
req_options = {
use_ssl: uri.scheme == "https",
}
response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
http.request(request)
end
collaborators = JSON.parse(response.body)
collaborators.each do |collaborator|
puts collaborator["login"]
end
end
desc "org_teams", "Get organization teams"
def org_teams
get_client.org_teams(ENV['GITHUB_ORG_NAME']).each do |team|
puts "#{team[:name]},#{team[:id]}"
end
end
desc "team_members", "List team members"
def team_members
client = get_client
client.org_teams(ENV['GITHUB_ORG_NAME']).each do |team|
client.team_members(team[:id]).each do |member|
puts "#{team[:name]},#{member[:login]}"
end
end
end
desc "repos_member_permission", "listup who have permissions to repositories?"
def repos_member_permission
client = get_client
client.org_repos(ENV['GITHUB_ORG_NAME']).each do |repo|
client.collaborators(repo[:full_name]).each do |member|
# 3個のbooleanを持っていて、下記のようにtrue/falseがついている。
# Readl は admin:false, push:false, pull:true
# Write は admin:false, push:true, pull:true
# Admin は admin:true, push:true, pull:true
permission = "Read" if member[:permissions][:pull]
permission = "Triage" if member[:permissions][:triage]
permission = "Write" if member[:permissions][:push]
permission = "Maintain" if member[:permissions][:maintain]
permission = "Admin" if member[:permissions][:admin]
puts "#{repo[:full_name]},#{member[:login]},#{permission}"
end
end
end
desc "repos_team_permission", "listup what team have permissions to repositories?"
def repos_team_permission
client = get_client
client.org_teams(ENV['GITHUB_ORG_NAME']).each do |team|
client.team_repos(team[:id]).each do |repo|
permission = "Read" if repo[:permissions][:pull]
permission = "Triage" if repo[:permissions][:triage]
permission = "Write" if repo[:permissions][:push]
permission = "Maintain" if repo[:permissions][:maintain]
permission = "Admin" if repo[:permissions][:admin]
puts "#{repo[:full_name]},#{team[:name]},#{permission}"
end
end
end
desc "repos_collabs_permission", "listup collaborators have permissions to repositories"
def repos_collabs_permission
client = get_client
client.org_repos(ENV['GITHUB_ORG_NAME']).each do |repo|
client.collaborators(repo[:full_name], {"affiliation":"direct"}).each do |collaborator|
permission = "Read" if collaborator[:permissions][:pull]
permission = "Write" if collaborator[:permissions][:push]
permission = "Admin" if collaborator[:permissions][:admin]
puts "#{repo[:full_name]},#{collaborator[:login]},#{permission}"
end
end
end
end
end
GithubTeamManage::CUI.start