ghedsh 1.1.40 → 2.3.8

Sign up to get free protection for your applications and to get access to all the features.
data/lib/actions/teams.rb CHANGED
@@ -2,245 +2,31 @@ require 'readline'
2
2
  require 'octokit'
3
3
  require 'json'
4
4
  require 'require_all'
5
- require_rel '.'
6
5
 
7
- class Teams
6
+ class Team
8
7
  attr_accessor :teamlist; :groupsteams
9
-
10
- def initialize
11
- @teamlist=Hash.new
12
- @groupsteams=Hash.new
13
- end
14
-
15
- def add_to_team(client,config,path)
16
- client.add_team_member(config["TeamID"],path)
17
- end
18
-
19
- def read_teamlist(client,config)
20
- @teamlist=Hash.new
21
- mem=client.organization_teams(config["Org"])
22
- mem.each do |i|
23
- @teamlist[i.name]=i[:id]
24
- end
25
- return @teamlist
26
- end
27
-
28
- def get_teamlist()
29
- return @teamlist
30
- end
31
-
32
- def clean_groupsteams() #metodo para limpiar la cache en cd ..
33
- @groupsteams=Hash.new
34
- end
35
-
36
- def create_team(client,config,name)
37
- begin
38
- client.create_team(config["Org"],{:name=>name,:permission=>'push'})
39
- rescue
40
- puts "Already exists a team with that name"
41
- end
42
- end
43
-
44
- def create_team_with_members(client,config,name,members)
45
- t=self.create_team(client,config,name)
46
- if t!=nil
47
- config["TeamID"]=t[:id]
48
-
49
- for i in 0..members.size
50
- if client.organization_member?(config["Org"],members[i])
51
- self.add_to_team(client,config,members[i])
52
- end
53
- end
54
- end
55
- end
56
-
57
- def add_to_team(client,config,path)
58
- client.add_team_member(config["TeamID"],path)
59
- end
60
-
61
- def delete_team(client,name)
62
- client.delete_team(name)
63
- end
64
-
65
- def show_teams_bs(client,config)
66
- print "\n"
67
- mem=client.organization_teams(config["Org"])
68
- mem.each do |i|
69
- puts i.name
70
- end
71
- print "\n"
72
- end
73
-
74
- def show_team_members_bs(client,config)
75
- print "\n"
76
- memberlist=[]
77
- mem=client.team_members(config["TeamID"])
78
- mem.each do |i|
79
- m=eval(i.inspect)
80
- puts m[:login]
81
- memberlist.push(m[:login])
82
- end
83
- print "\n"
84
- return memberlist
85
- print "\n"
86
- end
87
-
88
- def get_team_members(client,config,team)
89
- memberlist=[]
90
- if @teamlist.empty?
91
- self.read_teamlist(client,config)
92
- end
93
-
94
- if @teamlist["#{team}"]!=nil
95
- mem=client.team_members(@teamlist["#{team}"])
96
- mem.each do |i|
97
- m=eval(i.inspect)
98
- memberlist.push(m[:login])
99
- end
100
- end
101
- return memberlist
102
- end
103
-
104
- def list_groups(client,config)
105
- sys=Sys.new()
106
- list=sys.load_groups("#{ENV['HOME']}/.ghedsh")
107
- groups=list["orgs"].detect{|aux| aux["name"]==config["Org"]}
108
- if groups!=nil
109
- if groups["groups"].empty?
110
- puts "No groups are available yet"
111
- else
112
- puts "\nGroup\tTeams\tMembers"
113
- groups["groups"].each do |i|
114
- puts "\n"
115
- puts i["name_group"]
116
- i["teams"].each do |j|
117
- puts "\t#{j}"
118
- if @groupsteams["#{j}"]==nil
119
- @groupsteams["#{j}"]=Array.new
120
- self.get_team_members(client,config,j).each do |k|
121
- puts "\t\t#{k}"
122
- @groupsteams["#{j}"].push(k)
123
- end
124
- else
125
- @groupsteams["#{j}"].each do |k|
126
- puts "\t\t#{k}"
127
- end
128
- end
129
- end
130
- end
131
- end
8
+
9
+ def self.shell_prompt(config)
10
+ if config['Repo'].nil?
11
+ Rainbow("#{config['User']}> ").aqua << Rainbow("#{config['Org']}> ").magenta << Rainbow("#{config['Team']}> ").color('#eeff41')
132
12
  else
133
- puts "No groups are available yet"
134
- list["orgs"].push({"name"=>config["Org"],"groups"=>[]})
135
- sys.save_groups("#{ENV['HOME']}/.ghedsh",list)
136
- end
137
- end
138
-
139
- def get_groupslist(config)
140
- sys=Sys.new()
141
- grouplist=[]
142
- list=sys.load_groups("#{ENV['HOME']}/.ghedsh")
143
- groups=list["orgs"].detect{|aux| aux["name"]==config["Org"]}
144
- groups["groups"].each do |i|
145
- grouplist.push(i["name_group"])
13
+ Rainbow("#{config['User']}> ").aqua + Rainbow("#{config['Org']}> ").magenta << Rainbow("#{config['Team']}> ").color('#eeff41') << Rainbow("#{config['Repo']}> ").color(236, 151, 21)
146
14
  end
147
- return grouplist
148
15
  end
149
16
 
150
- def get_single_group(config,wanted)
151
- sys=Sys.new()
152
- list=sys.load_groups("#{ENV['HOME']}/.ghedsh")
153
- w=list["orgs"].detect{|aux| aux["name"]==config["Org"]}
154
- w=w["groups"].detect{|aux| aux["name_group"]==wanted}
155
- if w!=nil
156
- return w["teams"]
157
- else
158
- return nil
17
+ def build_cd_syntax(type, name)
18
+ syntax_map = { 'repo' => "Team.new.cd('repo', #{name}, client, env)" }
19
+ unless syntax_map.key?(type)
20
+ raise Rainbow("cd #{type} currently not supported.").color('#cc0000')
159
21
  end
22
+ syntax_map[type]
160
23
  end
161
24
 
162
- def new_group_file(client,config,name,file)
163
- sys=Sys.new()
164
- list=sys.loadfile(file)
165
- if list!=nil
166
- self.new_group(client,config,name,list)
25
+ def open_info(config, params = nil, client = nil)
26
+ if config['Repo'].nil?
27
+ open_url(config['team_url'].to_s)
167
28
  else
168
- puts "The file doesn't exist or It's empty."
29
+ open_url(config['repo_url'].to_s)
169
30
  end
170
31
  end
171
-
172
- def new_group(client,config,name,listgroups)
173
- sys=Sys.new()
174
- list=sys.load_groups("#{ENV['HOME']}/.ghedsh")
175
- groups=list["orgs"].detect{|aux| aux["name"]==config["Org"]}
176
-
177
- if groups==nil
178
- list["orgs"].push({"name"=>config["Org"],"groups"=>[]})
179
- sys.save_groups("#{ENV['HOME']}/.ghedsh",list)
180
- end
181
-
182
- if @teamlist.empty?
183
- self.read_teamlist(client,config)
184
- end
185
-
186
- listgroups.each do |item|
187
- if @teamlist["#{item}"]==nil
188
- listgroups.delete(item)
189
- puts "#{item} is not a team available."
190
- end
191
- end
192
-
193
- if listgroups.empty? == false
194
- begin
195
- list["orgs"][list["orgs"].index{|aux| aux["name"]==config["Org"]}]["groups"].push({"name_group"=>name,"teams"=>listgroups})
196
- rescue Exception => e
197
- puts e
198
- end
199
- sys.save_groups("#{ENV['HOME']}/.ghedsh",list)
200
- end
201
- end
202
-
203
- def delete_group(config,name)
204
- sys=Sys.new()
205
- list=sys.load_groups("#{ENV['HOME']}/.ghedsh")
206
- groups=list["orgs"].detect{|aux| aux["name"]==config["Org"]}
207
-
208
- if groups!=nil
209
- if groups["groups"].empty?
210
- puts "No groups are available yet"
211
- else
212
- del=groups["groups"].detect{|aux| aux["name_group"]==name}
213
- if del==nil
214
- puts "Group not found"
215
- else
216
- puts "Group #{name} will be deleted Are your sure? (Press y to confirm)"
217
- op=gets.chomp
218
- if op=="y"
219
- list["orgs"].detect{|aux| aux["name"]==config["Org"]}["groups"].delete(groups["groups"].detect{|aux2| aux2["name_group"]==name})
220
- sys.save_groups("#{ENV['HOME']}/.ghedsh",list)
221
- end
222
- end
223
- end
224
- end
225
- end
226
-
227
- def open_team_repos(config)
228
- case
229
- when RUBY_PLATFORM.downcase.include?("darwin")
230
- system("open https://github.com/orgs/#{config["Org"]}/teams/#{config["Team"]}")
231
- when RUBY_PLATFORM.downcase.include?("linux")
232
- system("xdg-open https://github.com/orgs/#{config["Org"]}/teams/#{config["Team"]}")
233
- end
234
- end
235
-
236
- def change_group_repos_privacity()
237
- end
238
-
239
- def add_to_group(config,name)
240
- end
241
-
242
- def del_of_group(config,name)
243
- #list=sys.load_groups("#{ENV['HOME']}/.ghedsh")
244
- #list["orgs"].detect{|aux| aux["name"]==config["Org"]}["groups"].delete(groups["groups"].detect{|aux2| aux2["name_group"]==name})
245
- end
246
32
  end
data/lib/actions/user.rb CHANGED
@@ -1,19 +1,311 @@
1
- require 'readline'
2
- require 'octokit'
3
- require 'json'
4
1
  require 'require_all'
5
- require_rel '.'
2
+ require 'rainbow'
3
+ require 'tty-spinner'
4
+ require 'tty-prompt'
5
+ require_relative '../helpers'
6
+ require 'ostruct'
6
7
 
7
8
  class User
8
- def info(client)
9
- mem=client.user(client.login)
10
- puts m[:login]
11
- puts m[:name]
12
- puts m[:email]
9
+ # Defined as method class in order to call it within context.rb
10
+ def self.shell_prompt(config)
11
+ if config['Repo'].nil?
12
+ Rainbow("#{config['User']}> ").aqua
13
+ else
14
+ Rainbow("#{config['User']}> ").aqua + Rainbow("#{config['Repo']}> ").color(236, 151, 21)
15
+ end
13
16
  end
14
17
 
15
- def open_user(client)
16
- mem=client.user(client.login)
17
- Sys.new.open_url(mem[:html_url])
18
+ def build_cd_syntax(type, name)
19
+ syntax_map = { 'repo' => "User.new.cd('repo', #{name}, client, env)",
20
+ 'org' => "User.new.cd('org', #{name}, client, env)" }
21
+ unless syntax_map.key?(type)
22
+ raise Rainbow("cd #{type} currently not supported.").color(ERROR_CODE)
23
+ end
24
+ syntax_map[type]
25
+ end
26
+
27
+ def open_info(config, _params = nil, _client = nil)
28
+ if config['Repo'].nil?
29
+ open_url(config['user_url'].to_s)
30
+ else
31
+ open_url(config['repo_url'].to_s)
32
+ end
33
+ end
34
+
35
+ def cd_org(name, client, enviroment)
36
+ if name.class == Regexp
37
+ pattern = Regexp.new(name.source, name.options)
38
+ user_orgs = []
39
+ user_orgs_url = {}
40
+ spinner = custom_spinner("Matching #{client.login} organizations :spinner ...")
41
+ spinner.auto_spin
42
+ client.organizations.each do |org|
43
+ if pattern.match((org[:login]).to_s)
44
+ user_orgs << org[:login]
45
+ user_orgs_url[org[:login].to_s] = 'https://github.com/' << org[:login].to_s
46
+ end
47
+ end
48
+ spinner.stop(Rainbow('done!').color(4, 255, 0))
49
+ if user_orgs.empty?
50
+ puts Rainbow("No organization match with #{name.source}").color(WARNING_CODE)
51
+ puts
52
+ return
53
+ else
54
+ prompt = TTY::Prompt.new
55
+ answer = prompt.select('Select desired organization', user_orgs)
56
+ enviroment.config['Org'] = answer
57
+ enviroment.config['org_url'] = user_orgs_url[answer]
58
+ enviroment.deep = Organization
59
+ end
60
+ else
61
+ if client.organization_member?(name.to_s, client.login.to_s)
62
+ enviroment.config['Org'] = name
63
+ enviroment.config['org_url'] = 'https://github.com/' << name.to_s
64
+ enviroment.deep = Organization
65
+ else
66
+ puts Rainbow("You are not currently #{name} member or #{name} is not an Organization.").color(WARNING_CODE)
67
+ puts
68
+ return
69
+ end
70
+ end
71
+ enviroment
72
+ end
73
+
74
+ def cd_repo(name, client, enviroment)
75
+ if name.class == Regexp
76
+ pattern = Regexp.new(name.source, name.options)
77
+ user_repos = []
78
+ user_repos_url = {}
79
+ spinner = custom_spinner("Matching #{client.login} repositories :spinner ...")
80
+ spinner.auto_spin
81
+ client.repositories.each do |repo|
82
+ if pattern.match(repo[:name].to_s)
83
+ user_repos << repo[:name]
84
+ user_repos_url[repo[:name].to_s] = repo[:html_url]
85
+ end
86
+ end
87
+ spinner.stop(Rainbow('done!').color(4, 255, 0))
88
+ if user_repos.empty?
89
+ puts Rainbow("No repository match with \/#{name.source}\/").color(WARNING_CODE)
90
+ return
91
+ else
92
+ prompt = TTY::Prompt.new
93
+ answer = prompt.select('Select desired repository', user_repos)
94
+ enviroment.config['Repo'] = answer
95
+ enviroment.config['repo_url'] = user_repos_url[answer]
96
+ enviroment.deep = User
97
+ end
98
+ else
99
+ if client.repository?("#{client.login}/#{name}")
100
+ res = {}
101
+ # client.repository returns array of arrays (in hash format)[ [key1, value1], [key2, value2] ]
102
+ # thats why first we convert the api response to hash
103
+ client.repository("#{client.login}/#{name}").each do |key, value|
104
+ res[key] = value
105
+ end
106
+ enviroment.config['Repo'] = name
107
+ enviroment.config['repo_url'] = res[:html_url]
108
+ enviroment.deep = User
109
+ else
110
+ puts Rainbow("Maybe #{name} is not a repository or currently does not exist.").color(WARNING_CODE)
111
+ return
112
+ end
113
+ end
114
+ enviroment
115
+ end
116
+
117
+ def cd(type, name, client, enviroment)
118
+ cd_scopes = { 'org' => method(:cd_org), 'repo' => method(:cd_repo) }
119
+ cd_scopes[type].call(name, client, enviroment)
120
+ end
121
+
122
+ def show_repos(client, _config, params)
123
+ spinner = custom_spinner("Fetching #{client.login} repositories :spinner ...")
124
+ spinner.auto_spin
125
+ user_repos = []
126
+ client.repositories.each do |repo|
127
+ user_repos << repo[:name]
128
+ end
129
+ user_repos.sort_by!(&:downcase)
130
+ spinner.stop(Rainbow('done!').color(4, 255, 0))
131
+ if params.nil?
132
+ item_counter = 0
133
+ user_repos.each do |repo_name|
134
+ puts repo_name
135
+ item_counter += 1
136
+ end
137
+ puts "\n#{item_counter} user repositories listed."
138
+ else
139
+ pattern = build_regexp_from_string(params)
140
+ occurrences = show_matching_items(user_repos, pattern)
141
+ puts Rainbow("No repository matched \/#{pattern.source}\/").color(INFO_CODE) if occurrences.zero?
142
+ puts "\n#{occurrences} user repositories listed."
143
+ end
144
+ end
145
+
146
+ def change_to_private_repo(client, _config, params)
147
+ pattern = build_regexp_from_string(params)
148
+ spinner = custom_spinner('Setting private repos :spinner ...')
149
+ spinner.auto_spin
150
+ repos = []
151
+ client.repositories.each do |repo|
152
+ repos.push(repo[:name]) if pattern.match(repo[:name])
153
+ end
154
+ repos.each do |i|
155
+ client.set_private("#{client.login}/#{i}")
156
+ end
157
+ spinner.stop(Rainbow('done!').color(4, 255, 0))
158
+ rescue StandardError => exception
159
+ puts Rainbow(exception.message.to_s).color(ERROR_CODE)
160
+ end
161
+
162
+ def change_to_public_repo(client, _config, params)
163
+ pattern = build_regexp_from_string(params)
164
+ spinner = custom_spinner('Setting public repos :spinner ...')
165
+ spinner.auto_spin
166
+ repos = []
167
+ client.repositories.each do |repo|
168
+ repos.push(repo[:name]) if pattern.match(repo[:name])
169
+ end
170
+ repos.each do |i|
171
+ client.set_public("#{client.login}/#{i}")
172
+ end
173
+ spinner.stop(Rainbow('done!').color(4, 255, 0))
174
+ rescue StandardError => exception
175
+ puts Rainbow(exception.message.to_s).color(ERROR_CODE)
176
+ end
177
+
178
+ def show_organizations(client, params)
179
+ spinner = custom_spinner("Fetching #{client.login} organizations :spinner ...")
180
+ spinner.auto_spin
181
+ user_orgs = []
182
+ client.list_organizations.each do |org|
183
+ user_orgs << org[:login]
184
+ end
185
+ user_orgs.sort_by!(&:downcase)
186
+ spinner.stop(Rainbow('done!').color(4, 255, 0))
187
+ if params.empty?
188
+ user_orgs.each do |org_name|
189
+ puts org_name
190
+ end
191
+
192
+ puts "\nYou are currently member of #{user_orgs.size} organizations.\n"
193
+ else
194
+ pattern = build_regexp_from_string(params[0])
195
+ occurrences = show_matching_items(user_orgs, pattern)
196
+ puts Rainbow("No organization matched \/#{pattern.source}\/").color(INFO_CODE) if occurrences.zero?
197
+ puts "\nShowing #{occurrences} results."
198
+ end
199
+ end
200
+
201
+ def create_repo(enviroment, repo_name, options)
202
+ client = enviroment.client
203
+ client.create_repository(repo_name, options)
204
+ puts Rainbow('Repository created correctly!').color(79, 138, 16)
205
+ rescue StandardError => exception
206
+ puts Rainbow(exception.message.to_s).color(ERROR_CODE)
207
+ puts
208
+ end
209
+
210
+ def remove_repo(enviroment, repo_name)
211
+ client = enviroment.client
212
+ client.delete_repository("#{client.login}/#{repo_name}")
213
+ puts Rainbow('Repository deleted.').color(INFO_CODE)
214
+ rescue StandardError => exception
215
+ puts
216
+ puts Rainbow(exception.message.to_s).color('#cc0000')
217
+ end
218
+
219
+ def create_issue(config)
220
+ if config['Repo']
221
+ issue_creation_url = "https://github.com/#{config['User']}/#{config['Repo']}/issues/new"
222
+ open_url(issue_creation_url)
223
+ else
224
+ puts Rainbow('Change to repo in order to create an issue.').color(INFO_CODE)
225
+ end
226
+ end
227
+
228
+ def show_issues(config)
229
+ if config['Repo']
230
+ issues_url = "https://github.com/#{config['User']}/#{config['Repo']}/issues"
231
+ open_url(issues_url)
232
+ else
233
+ puts Rainbow('Change to repo in order to view all issues').color(INFO_CODE)
234
+ end
235
+ end
236
+
237
+ def show_files(client, config, params)
238
+ if config['Repo']
239
+ options = { path: '' }
240
+ options[:path] = params[0] unless params.empty?
241
+ file_names_and_types = []
242
+ client.contents("#{client.login}/#{config['Repo']}", options).each do |i|
243
+ file_names_and_types << "#{i[:name]} (#{i[:type]})"
244
+ end
245
+ file_names_and_types.sort_by!(&:downcase)
246
+ puts file_names_and_types
247
+ else
248
+ puts Rainbow('Please change to repository to see its files.').color(INFO_CODE)
249
+ end
250
+ rescue StandardError => e
251
+ puts Rainbow(e.message.to_s).color(ERROR_CODE)
252
+ end
253
+
254
+ def clone_repository(enviroment, repo_name, custom_path)
255
+ client = enviroment.client
256
+ repos_to_clone = []
257
+ if repo_name.include?('/')
258
+ pattern = build_regexp_from_string(repo_name)
259
+ client.repositories.each do |repo|
260
+ repos_to_clone << { name: repo[:name], ssh_url: repo[:clone_url] } if pattern.match(repo[:name])
261
+ end
262
+ puts Rainbow("No repository matched \/#{pattern.source}\/").color(INFO_CODE) if repos_to_clone.empty?
263
+ else
264
+ repo = client.repository("#{client.login}/#{repo_name}")
265
+ repos_to_clone << { name: repo[:name], ssh_url: repo[:clone_url] }
266
+ end
267
+ unless repos_to_clone.empty?
268
+ perform_git_clone(repos_to_clone, custom_path)
269
+ if custom_path.nil?
270
+ puts Rainbow("Cloned into #{Dir.pwd}").color(INFO_CODE).underline
271
+ else
272
+ puts Rainbow("Cloned into #{Dir.home}#{custom_path}").color(INFO_CODE).underline
273
+ end
274
+ puts
275
+ end
276
+ rescue StandardError => exception
277
+ puts Rainbow(exception.message.to_s).color('#cc0000')
278
+ puts
279
+ end
280
+
281
+ def show_commits(enviroment, params)
282
+ options = {}
283
+ if !enviroment.config['Repo'].nil?
284
+ repo = enviroment.config['Repo']
285
+ options[:sha] = if params.empty?
286
+ 'master'
287
+ else
288
+ params[0]
289
+ end
290
+ else
291
+ repo = params[0]
292
+ options[:sha] = if params[1].nil?
293
+ 'master'
294
+ else
295
+ params[1]
296
+ end
297
+ end
298
+ begin
299
+ enviroment.client.commits("#{enviroment.client.login}/#{repo}", options).each do |i|
300
+ puts "\tSHA: #{i[:sha]}"
301
+ puts "\t\t Commit date: #{i[:commit][:author][:date]}"
302
+ puts "\t\t Commit author: #{i[:commit][:author][:name]}"
303
+ puts "\t\t\t Commit message: #{i[:commit][:message]}"
304
+ end
305
+ rescue StandardError => exception
306
+ puts exception
307
+ puts Rainbow("If you are not currently on a repo, USAGE TIP: `commits <repo_name> [branch_name]` (default: 'master')").color(INFO_CODE)
308
+ puts
309
+ end
18
310
  end
19
311
  end