Class: Repositories

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRepositories

scope = 1 -> organization repos scope = 2 -> user repos scope = 3 -> team repos



15
16
17
# File 'lib/actions/repo.rb', line 15

def initialize
  @reposlist=[]
end

Instance Attribute Details

#reposlistObject (readonly)

Returns the value of attribute reposlist.



10
11
12
# File 'lib/actions/repo.rb', line 10

def reposlist
  @reposlist
end

Instance Method Details

#cat_file(client, config, path, scope) ⇒ Object



364
365
366
367
368
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
402
# File 'lib/actions/repo.rb', line 364

def cat_file(client,config,path,scope)
  if path.match(/.\./)!=nil
    case
    when scope==USER_REPO
      if config["Repo"].split("/").size > 1
        begin
          data=Base64.decode64(client.content(config["Repo"],:path=>path).content)
        rescue Exception, Interrupt
          puts "File not found"
        end
      else
        begin
          data=Base64.decode64(client.content(config["User"]+"/"+config["Repo"],:path=>path).content)
        rescue Exception, Interrupt
          puts "File not found"
        end
      end

    when scope==ORGS_REPO
      begin
        data=Base64.decode64(client.content(config["Org"]+"/"+config["Repo"],:path=>path).content)
      rescue Exception, Interrupt
        puts "File not found"
      end
    when scope==TEAM_REPO
      begin
        data=Base64.decode64(client.content(config["Org"]+"/"+config["Repo"],:path=>path).content)
      rescue Exception, Interrupt
        puts "File not found"
      end
    end
    # s=Sys.new()
    # s.createTempFile(data)
    # s.execute_bash("vi -R #{data}")
    puts data
  else
    puts "#{path} is not a file."
  end
end

#clone_repo(client, config, exp, scope) ⇒ Object

clone repositories exp = regular expression



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
# File 'lib/actions/repo.rb', line 304

def clone_repo(client,config,exp,scope)
  web="https://github.com/"
  web2="[email protected]:"

  if scope==USER_REPO || scope==TEAM_REPO || scope==ORGS_REPO
    case
      when scope==USER_REPO
        if config["Repo"].split("/").size == 1
          command = "git clone #{web2}#{config["User"]}/#{config["Repo"]}.git"
        else
          command = "git clone #{web2}#{config["Repo"]}.git"
        end
      when scope==TEAM_REPO
        command = "git clone #{web2}#{config["Org"]}/#{config["Repo"]}.git"
      when scope==ORGS_REPO
        command = "git clone #{web2}#{config["Org"]}/#{config["Repo"]}.git"
    end
      system(command)
  else
    if exp.match(/^\//)
      exps=exp.split('/')
      list=self.get_repos_list(client,config,scope)
      list=Sys.new.search_rexp(list,exps[1])
    else
      list=[]
      list.push(exp)
    end

    if (list.empty?) == false
      case
      when scope==USER
        list.each do |i|
          command = "git clone #{web2}#{config["User"]}/#{i}.git"
          system(command)
        end
      when scope==ORGS
        list.each do |i|
          command = "git clone #{web2}#{config["Org"]}/#{i}.git"
          system(command)
        end
      end
    else
      puts "No repositories found it with the parameters given"
    end
  end
end

#close_issue(client, config, scope, id) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/actions/repo.rb', line 55

def close_issue(client,config,scope,id)
  case
  when scope==USER_REPO
    if config["Repo"].split("/").size == 1
      client.close_issue(config["User"]+"/"+config["Repo"],id)
    else
      client.close_issue(config["Repo"],id)
    end
  when scope==ORGS_REPO || scope==TEAM_REPO
    client.close_issue(config["Org"]+"/"+config["Repo"],id)
  end
end

#create_issue(client, config, scope) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/actions/repo.rb', line 36

def create_issue(client,config,scope)
  puts "Insert Issue tittle: "
  tittle=gets.chomp
  puts "Add a description: "
  desc=gets.chomp

  case
  when scope==USER_REPO
    if config["Repo"].split("/").size == 1
      client.create_issue(config["User"]+"/"+config["Repo"],tittle,desc)
    else
      client.create_issue(config["Repo"],tittle,desc)
    end
  when scope==ORGS_REPO || scope==TEAM_REPO
    client.create_issue(config["Org"]+"/"+config["Repo"],tittle,desc)
  end

end

#create_repository(client, config, repo, scope) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/actions/repo.rb', line 242

def create_repository(client,config,repo,scope)
  options=Hash.new
  options[:auto_init]=true
  case
  when scope==ORGS
    puts "created repository in #{config["Org"]}"
    options[:organization]=config["Org"]
    client.create_repository(repo,options)
  when scope==USER
    puts "created repository in user"
    client.create_repository(repo)
  when scope==TEAM
    puts "created repository in #{config["Org"]} team"
    options[:team_id]=config["TeamID"]
    options[:organization]=config["Org"]
    client.create_repository(repo,options)
  end
end

#create_repository_by_teamlist(client, config, repo, list, list_id) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/actions/repo.rb', line 261

def create_repository_by_teamlist(client,config,repo,list,list_id)
  options=Hash.new
  options[:organization]=config["Org"]
  #puts list_id
  y=0
  list.each do |i|
    options[:team_id]=list_id[y]
    # puts i, list_id[y]
    # puts repo
    # puts options
    # puts "\n"
    client.create_repository(i+"/"+repo,options)
    y=y+1
  end
end

#fork(client, config, repo) ⇒ Object



237
238
239
240
# File 'lib/actions/repo.rb', line 237

def fork(client,config,repo)
  mem=client.fork(repo)
  return mem
end

#get_files(client, config, path, show, scope) ⇒ Object



404
405
406
407
408
409
410
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
# File 'lib/actions/repo.rb', line 404

def get_files(client,config,path,show,scope)
  #show=true
  if path.match(/.\./)==nil
    case
    when scope==USER_REPO
      if config["Repo"].split("/").size > 1
        begin
          list=client.content(config["Repo"],:path=>path)
        rescue Exception, Interrupt => e
          puts "No files found"
          show=false
        end
      else
        begin
          list=client.content(config["User"]+"/"+config["Repo"],:path=>path)
        rescue Exception, Interrupt => e
          puts "No files found"
          show=false
        end
      end

    when scope==ORGS_REPO
      begin
        list=client.content(config["Org"]+"/"+config["Repo"],:path=>path)
      rescue Exception, Interrupt => e
        puts "No files found"
        show=false
      end
    when scope==TEAM_REPO
      begin
        list=client.content(config["Org"]+"/"+config["Repo"],:path=>path)
      rescue Exception, Interrupt => e
        puts "No files found"
        show=false
      end
    end
    if show!=false
      self.show_files(list)
    else
      return list
    end
  else
    puts "#{path} is not a directory. If you want to open a file try to use cat <path>"
  end
end

#get_issues(client, config, scope) ⇒ Object



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

def get_issues(client,config,scope)
  case
  when scope==USER_REPO
    if config["Repo"].split("/").size == 1
      mem=client.list_issues(config["User"]+"/"+config["Repo"],{:state=>"all"})
    else
      mem=client.list_issues(config["Repo"],{:state=>"all"})
    end
  when scope==ORGS_REPO || scope==TEAM_REPO
      mem=client.list_issues(config["Org"]+"/"+config["Repo"],{:state=>"all"})
  end
  return mem
end

#get_repos_list(client, config, scope) ⇒ Object

Gete the repository list from a given scope



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/repo.rb', line 278

def get_repos_list(client,config,scope)
  reposlist=[]
  case
    when scope==USER
      repo=client.repositories
    when scope==ORGS
      repo=client.organization_repositories(config["Org"])
    when scope==TEAM
      repo=client.team_repositories(config["TeamID"])
  end
  repo.each do |i|
    if scope!=USER
      reposlist.push(i.name)
    else
      if i[:owner][:login]==config["User"]
        reposlist.push(i.name)
      else
        reposlist.push(i.full_name)
      end
    end
  end
  return reposlist
end

#open_issue(client, config, scope, id) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/actions/repo.rb', line 68

def open_issue(client,config,scope,id)
  case
  when scope==USER_REPO
    if config["Repo"].split("/").size == 1
      client.reopen_issue(config["User"]+"/"+config["Repo"],id)
    else
      client.reopen_issue(config["Repo"],id)
    end
  when scope==ORGS_REPO || scope==TEAM_REPO
    client.reopen_issue(config["Org"]+"/"+config["Repo"],id)
  end
end

#show_collaborators(client, config, scope) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/actions/repo.rb', line 222

def show_collaborators(client,config,scope)
  print "\n"
  collalist=[]
  case
  when scope==USER
    mem=client.collaborators(config["Org"]+"/"+config["Repo"])
  end
  mem.each do |i|
    puts i[:author][:login]
    collalist.push(i[:author][:login])
  end
  print "\n"
  return collalist
end

#show_commits(client, config, scope) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/actions/repo.rb', line 19

def show_commits(client,config,scope)
  print "\n"
  case
  when scope==USER_REPO
      if config["Repo"].split("/").size == 1
        mem=client.commits(config["User"]+"/"+config["Repo"],"master")
      else
        mem=client.commits(config["Repo"],"master")
      end
    when scope==ORGS_REPO
      mem=client.commits(config["Org"]+"/"+config["Repo"],"master")
  end
  mem.each do |i|
    print i[:sha],"\n",i[:commit][:author][:name],"\n",i[:commit][:author][:date],"\n",i[:commit][:message],"\n\n"
  end
end

#show_files(list) ⇒ Object



351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/actions/repo.rb', line 351

def show_files(list)
  print "\n"

  list.each do |i|
    if i.name.match(/.\./)!=nil
      puts i.name
    else
      puts "\e[33m#{i.name}\e[0m"
    end
  end
  print "\n"
end

#show_forks(client, config, scope) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/actions/repo.rb', line 207

def show_forks(client,config,scope)
  print "\n"
  forklist=[]
  case
    when scope==USER
      mem=client.forks(config["Org"]+"/"+config["Repo"])
  end
  mem.each do |i|
    puts i[:owner][:login]
    forklist.push(i[:owner][:login])
  end
  print "\n"
  return forklist
end

#show_issues(client, config, scope) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/actions/repo.rb', line 81

def show_issues(client,config,scope)
    print "\n"
    case
    when scope==USER_REPO
      if config["Repo"].split("/").size == 1
        mem=client.list_issues(config["User"]+"/"+config["Repo"],{:state=>"all"})
      else
        mem=client.list_issues(config["Repo"],{:state=>"all"})
      end
    when scope==ORGS_REPO || scope==TEAM_REPO
        mem=client.list_issues(config["Org"]+"/"+config["Repo"],{:state=>"all"})
    end
    mem.each do |i|
      #print i[:sha],"\n",i[:commit][:author][:name],"\n",i[:commit][:author][:date],"\n",i[:commit][:message],"\n\n"
      puts "##{i[:number]} state: #{i[:state]} -> #{i[:title]} "
    end
    return mem
    puts "\n"
end

#show_repos(client, config, scope, exp) ⇒ Object

Show repositories and return a list of them exp = regular expression



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/actions/repo.rb', line 116

def show_repos(client,config,scope,exp)
  print "\n"
  rlist=[]
  options=Hash.new
  o=Organizations.new
  regex=false
  force_exit=false

  if exp!=nil
    if exp.match(/^\//)
      regex=true
      sp=exp.split('/')
      exp=Regexp.new(sp[1],sp[2])
    end
  end

  case
    when scope==USER
      repo=client.repositories(options) #config["User"]
      listorgs=o.read_orgs(client)
    when scope==ORGS
      repo=client.organization_repositories(config["Org"])
    when scope==TEAM
      repo=client.team_repositories(config["TeamID"])
  end

  counter=0
  allpages=true

  repo.each do |i|
    if force_exit==false
      if regex==false
        if counter==100 && allpages==true
          op=Readline.readline("\nThere are more results. Show next repositories (press any key), show all repositories (press a) or quit (q): ",true)
          if op=="a"
            allpages=false
          end
          if op=="q"
            force_exit=true
          end
          counter=0
        end
        if scope ==USER
          if i[:owner][:login]==config["User"]
            puts i.name
            rlist.push(i.name)
          else
            puts i.full_name
            rlist.push(i.full_name)
          end
        else
          puts i.name
          rlist.push(i.name)
        end
        counter=counter+1
      else
        if i.name.match(exp)
          if scope ==USER
            puts i.full_name
            rlist.push(i.full_name)
          else
            puts i.name
            rlist.push(i.name)
          end
            counter=counter+1
          end
      end
    end
  end

  if rlist.empty?
    puts "No repository matches with that expression"
  else
    print "\n"
    puts "Repositories found: #{rlist.size}"
  end

  return rlist
end

#show_user_orgs_repos(client, config, listorgs) ⇒ Object



196
197
198
199
200
201
202
203
204
205
# File 'lib/actions/repo.rb', line 196

def show_user_orgs_repos(client,config,listorgs)
  options=Hash.new
  options[:member]=config["User"]
  listorgs.each do |i|
    repo=client.organization_repositories(i,options)
        repo.each do |y|
          puts y.name
        end
  end
end