Class: User

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.shell_prompt(config) ⇒ Object

Defined as method class in order to call it within context.rb



10
11
12
13
14
15
16
# File 'lib/actions/user.rb', line 10

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

Instance Method Details

#build_cd_syntax(type, name) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/actions/user.rb', line 18

def build_cd_syntax(type, name)
  syntax_map = { 'repo' => "User.new.cd('repo', #{name}, client, env)",
                 'org' => "User.new.cd('org', #{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



117
118
119
120
# File 'lib/actions/user.rb', line 117

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

#cd_org(name, client, enviroment) ⇒ Object



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
# File 'lib/actions/user.rb', line 35

def cd_org(name, client, enviroment)
  if name.class == Regexp
    pattern = Regexp.new(name.source, name.options)
    user_orgs = []
    user_orgs_url = {}
    spinner = custom_spinner("Matching #{client.} organizations :spinner ...")
    spinner.auto_spin
    client.organizations.each do |org|
      if pattern.match((org[:login]).to_s)
        user_orgs << org[:login]
        user_orgs_url[org[:login].to_s] = 'https://github.com/' << org[:login].to_s
      end
    end
    spinner.stop(Rainbow('done!').color(4, 255, 0))
    if user_orgs.empty?
      puts Rainbow("No organization match with #{name.source}").color(WARNING_CODE)
      puts
      return
    else
      prompt = TTY::Prompt.new
      answer = prompt.select('Select desired organization', user_orgs)
      enviroment.config['Org'] = answer
      enviroment.config['org_url'] = user_orgs_url[answer]
      enviroment.deep = Organization
    end
  else
    if client.organization_member?(name.to_s, client..to_s)
      enviroment.config['Org'] = name
      enviroment.config['org_url'] = 'https://github.com/' << name.to_s
      enviroment.deep = Organization
    else
      puts Rainbow("You are not currently #{name} member or #{name} is not an Organization.").color(WARNING_CODE)
      puts
      return
    end
  end
  enviroment
end

#cd_repo(name, client, enviroment) ⇒ Object



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
# File 'lib/actions/user.rb', line 74

def cd_repo(name, client, enviroment)
  if name.class == Regexp
    pattern = Regexp.new(name.source, name.options)
    user_repos = []
    user_repos_url = {}
    spinner = custom_spinner("Matching #{client.} repositories :spinner ...")
    spinner.auto_spin
    client.repositories.each do |repo|
      if pattern.match(repo[:name].to_s)
        user_repos << repo[:name]
        user_repos_url[repo[:name].to_s] = repo[:html_url]
      end
    end
    spinner.stop(Rainbow('done!').color(4, 255, 0))
    if user_repos.empty?
      puts Rainbow("No repository match with \/#{name.source}\/").color(WARNING_CODE)
      return
    else
      prompt = TTY::Prompt.new
      answer = prompt.select('Select desired repository', user_repos)
      enviroment.config['Repo'] = answer
      enviroment.config['repo_url'] = user_repos_url[answer]
      enviroment.deep = User
    end
  else
    if client.repository?("#{client.}/#{name}")
      res = {}
      # client.repository returns array of arrays (in hash format)[ [key1, value1], [key2, value2] ]
      # thats why first we convert the api response to hash
      client.repository("#{client.}/#{name}").each do |key, value|
        res[key] = value
      end
      enviroment.config['Repo'] = name
      enviroment.config['repo_url'] = res[:html_url]
      enviroment.deep = User
    else
      puts Rainbow("Maybe #{name} is not a repository or currently does not exist.").color(WARNING_CODE)
      return
    end
  end
  enviroment
end

#change_to_private_repo(client, _config, params) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/actions/user.rb', line 146

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.repositories.each do |repo|
    repos.push(repo[:name]) if pattern.match(repo[:name])
  end
  repos.each do |i|
    client.set_private("#{client.}/#{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



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/actions/user.rb', line 162

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.repositories.each do |repo|
    repos.push(repo[:name]) if pattern.match(repo[:name])
  end
  repos.each do |i|
    client.set_public("#{client.}/#{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



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/actions/user.rb', line 254

def clone_repository(enviroment, repo_name, custom_path)
  client = enviroment.client
  repos_to_clone = []
  if repo_name.include?('/')
    pattern = build_regexp_from_string(repo_name)
    client.repositories.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("#{client.}/#{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('#cc0000')
  puts
end

#create_issue(config) ⇒ Object



219
220
221
222
223
224
225
226
# File 'lib/actions/user.rb', line 219

def create_issue(config)
  if config['Repo']
    issue_creation_url = "https://github.com/#{config['User']}/#{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



201
202
203
204
205
206
207
208
# File 'lib/actions/user.rb', line 201

def create_repo(enviroment, repo_name, options)
  client = enviroment.client
  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

#open_info(config, _params = nil, _client = nil) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/actions/user.rb', line 27

def open_info(config, _params = nil, _client = nil)
  if config['Repo'].nil?
    open_url(config['user_url'].to_s)
  else
    open_url(config['repo_url'].to_s)
  end
end

#remove_repo(enviroment, repo_name) ⇒ Object



210
211
212
213
214
215
216
217
# File 'lib/actions/user.rb', line 210

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

#show_commits(enviroment, params) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/actions/user.rb', line 281

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.client.}/#{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]}"
    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)
    puts
  end
end

#show_files(client, config, params) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/actions/user.rb', line 237

def show_files(client, config, params)
  if config['Repo']
    options = { path: '' }
    options[:path] = params[0] unless params.empty?
    file_names_and_types = []
    client.contents("#{client.}/#{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 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



228
229
230
231
232
233
234
235
# File 'lib/actions/user.rb', line 228

def show_issues(config)
  if config['Repo']
    issues_url = "https://github.com/#{config['User']}/#{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_organizations(client, params) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/actions/user.rb', line 178

def show_organizations(client, params)
  spinner = custom_spinner("Fetching #{client.} organizations :spinner ...")
  spinner.auto_spin
  user_orgs = []
  client.list_organizations.each do |org|
    user_orgs << org[:login]
  end
  user_orgs.sort_by!(&:downcase)
  spinner.stop(Rainbow('done!').color(4, 255, 0))
  if params.empty?
    user_orgs.each do |org_name|
      puts org_name
    end
    
    puts "\nYou are currently member of #{user_orgs.size} organizations.\n"
  else
    pattern = build_regexp_from_string(params[0])
    occurrences = show_matching_items(user_orgs, pattern)
    puts Rainbow("No organization matched \/#{pattern.source}\/").color(INFO_CODE) if occurrences.zero?
    puts "\nShowing #{occurrences} results."
  end
end

#show_repos(client, _config, params) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/actions/user.rb', line 122

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