Class: Github::Repos

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

Class Method Summary collapse

Class Method Details

.create(name, description) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hubmaster/repo.rb', line 39

def self.create(name, description)
  if name.nil?
    print "Name of repository: "
    name = STDIN.gets.chomp
  end

  if description.nil?
    print "Description of repository: "
    description = STDIN.gets.chomp
  end

  jsonHash = {"name" => name, "description" => description}.to_json
  request = Github.makePostRequest("/user/repos", jsonHash)
  response = JSON.parse(request)

  if response["errors"].nil? && response["message"].nil?
    puts "Repository \"#{name}\" succesfully created! Hosted at: #{JSON.parse(request)["ssh_url"]}"
  elsif !response["errors"].nil?
    puts "ERROR: #{response['errors'][0]['message']}"
  elsif !response["message"].nil?
    puts "ERROR: #{response["message"]}"
  end
  puts ""
end

.delete(name) ⇒ Object



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
# File 'lib/hubmaster/repo.rb', line 93

def self.delete(name)
  if name.nil?
    print "Name of repository: "
    name = STDIN.gets.chomp
  end

  puts "This well permenantly delete the repository '#{name}' from your github account. Are you sure you want to do this?"
  print "If so, type the name of the repository (enter to cancle): "
  confirm = STDIN.gets.chomp

  if confirm == name
    request = Github.makeDeleteRequest("/repos/#{Github.user}/#{name}")
    if request.nil?
      response = nil
    else
      response = JSON.parse(request)
    end

    if response.nil?
      puts "Repository '#{name}' has been deleted!"
    elsif !response["errors"].nil?
      puts "ERROR: #{response['errors'][0]['message']}"
    elsif !response["message"].nil?
      puts "ERROR: #{response["message"]}"
    end
  else
    puts "Name entered was incorrect and request has been cancled."
  end
  puts ""
end

.edit(operation, name, op) ⇒ Object



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
# File 'lib/hubmaster/repo.rb', line 64

def self.edit(operation, name, op)
  if name.nil?
    print "Name of repository you wish to edit: "
    name = STDIN.gets.chomp
  end

  case operation
  when :description
    if op.nil?
      print "Intended description of repository: "
      op = STDIN.gets.chomp
    end

    jsonHash = {"name" => name, "description" => op}.to_json
    request = Github.makeEditRequest("/repos/#{Github.user}/#{name}", jsonHash)
  end
  
  response = JSON.parse(request)

  if response["errors"].nil? && response["message"].nil?
    puts "Repository \"#{name}\" succesfully edited! See updates at: #{JSON.parse(request)["html_url"]}"
  elsif !response["errors"].nil?
    puts "ERROR: #{response['errors'][0]['message']}"
  elsif !response["message"].nil?
    puts "ERROR: #{response["message"]}"
  end
  puts ""
end

.get(owner, name, operator, branch = nil) ⇒ Object



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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
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
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/hubmaster/repo.rb', line 124

def self.get(owner, name, operator, branch = nil)
  owner = Github.user if owner == "self"

  toput = []

  if owner.nil?
    print "Name of owner: "
    owner = STDIN.gets.chomp
  end
  
  if name.nil? 
    print "Name of repository: "
    name = STDIN.gets.chomp
  end

  case operator
  when :contributors
    request = Github.makeGetRequest("/repos/#{owner}/#{name}/contributors")  
    response = JSON.parse(request)

    if response.kind_of?(Array)
      response.each do |contributer|
        toput << "User #{contributer["login"]}"
        toput << " - URL: #{contributer["url"]}"
        toput << " - Contributions #{contributer["contributions"]}"
        toput << ""
      end
    elsif !response["errors"].nil?
      puts "ERROR: #{response['errors'][0]['message']}"
      puts ""
    elsif !response["message"].nil?
      puts "ERROR: #{response["message"]}"
      puts ""
    end
  when :languages
    request = Github.makeGetRequest("/repos/#{owner}/#{name}/languages")  
    response = JSON.parse(request)

    if response["errors"].nil? && response["message"].nil?
      response.each do |language, bytes|
        toput << "#{bytes} bytes written in #{language}."
        toput << ""
      end
    elsif !response["errors"].nil?
      puts "ERROR: #{response['errors'][0]['message']}"
      puts ""
    elsif !response["message"].nil?
      puts "ERROR: #{response["message"]}"
      puts ""
    end
  when :teams #UNTESTED METHOD BECAUSE I DONT KNOW ANY EXAMPLES
    request = Github.makeGetRequest("/repos/#{owner}/#{name}/teams")  
    response = JSON.parse(request)

    if response.kind_of?(Array)
      response.each do |team|
        toput << "Team: #{team["name"]}"
        toput << " - URL: #{team["url"]}"
        puts ""
      end
    elsif !response["errors"].nil?
      puts "ERROR: #{response['errors'][0]['message']}"
      puts ""
    elsif !response["message"].nil?
      puts "ERROR: #{response["message"]}"
      puts ""
    end
  when :tags 
    request = Github.makeGetRequest("/repos/#{owner}/#{name}/tags")  
    response = JSON.parse(request)

    if response.kind_of?(Array)
      response.each do |tag|
        toput << "Tag Name: #{tag["name"]}"
        toput << " - Commit URL: #{tag["commit"]["url"]}"
        toput << " - Commit SHA: #{tag["commit"]["sha"]}"
        toput << ""
      end
    elsif !response["errors"].nil?
      puts "ERROR: #{response['errors'][0]['message']}"
      puts ""
    elsif !response["message"].nil?
      puts "ERROR: #{response["message"]}"
      puts ""
    end
  when :branches 
    request = Github.makeGetRequest("/repos/#{owner}/#{name}/branches")  
    response = JSON.parse(request)

    if response.kind_of?(Array)
      response.each do |branch|
        toput << "Branch Name: #{branch["name"]}"
        toput << " - Commit URL: #{branch["commit"]["url"]}"
        toput << " - Commit SHA: #{branch["commit"]["sha"]}"
        puts ""
      end
    elsif !response["errors"].nil?
      puts "ERROR: #{response['errors'][0]['message']}"
      puts ""
    elsif !response["message"].nil?
      puts "ERROR: #{response["message"]}"
      puts ""
    end
  when :branch
    if branch.nil?
      print "Branch to view: "
      branch = STDIN.gets.chomp
    end

    request = Github.makeGetRequest("/repos/#{owner}/#{name}/branches/#{branch}")  
    response = JSON.parse(request)
    
    if response["errors"].nil? && response["message"].nil?
      puts "Branch Name: #{response['name']}"  
      puts " - Commit SHA: #{response["commit"]["sha"]}"
      puts " - Message: #{response["commit"]["commit"]["message"]}"
      puts " - Author: #{response["commit"]["author"]["login"]}"
      puts "   - Author Name: #{response["commit"]["commit"]["author"]["name"]}"
      puts "   - Author Email: #{response["commit"]["commit"]["author"]["email"]}"
      
      if response["commit"]["committer"]["login"] != response["commit"]["author"]["login"]
        puts " - Commiter: #{response["commit"]["committer"]["login"]}"
        puts "   - Commiter Name: #{response["commit"]["commit"]["committer"]["name"]}"
        puts "   - Commiter Email: #{response["commit"]["commit"]["committer"]["email"]}"
      end

      puts " - Parents:"
      response["commit"]["parents"].each do |parent|
        puts "   - Parent SHA: #{parent["sha"]}"
      end
    elsif !response["errors"].nil?
      puts "ERROR: #{response['errors'][0]['message']}"
    elsif !response["message"].nil?
      puts "ERROR: #{response["message"]}"
    end
    puts ""
  when :repository
    request = Github.makeGetRequest("/repos/#{owner}/#{name}")  
    response = JSON.parse(request)
    
    if response["errors"].nil? && response["message"].nil?
      puts "#{response['name']} (#{response['ssh_url']})"  
      puts " - Owner: #{response['owner']['login']}"
      puts " - Description: #{response['description']}"
      puts " - Private: #{response['private']}"
      puts " - Fork: #{response['fork']}"
      puts " - Language: #{response['language']}"
      puts " - Forks: #{response['forks']}"
      puts " - Watchers: #{response['watchers']}"
      puts " - Master Branch: #{response['master_branch']}"
      puts " - Open Issues: #{response['open_issues']}"
      created_at = response['created_at'].split("T")
      created_at_date = created_at[0].split("-")
      created_at_date = "#{created_at_date[1]}/#{created_at_date[2]}/#{created_at_date[0]}" 
      puts " - Created At: #{created_at[1]} on #{created_at_date}"
      updated_at = response['updated_at'].split("T")
      updated_at_date = updated_at[0].split("-")
      updated_at_date = "#{updated_at_date[1]}/#{updated_at_date[2]}/#{updated_at_date[0]}" 
      puts " - Last Updated: #{updated_at[1]} on #{updated_at_date}"
    elsif !response["errors"].nil?
      puts "ERROR: #{response['errors'][0]['message']}"
    elsif !response["message"].nil?
      puts "ERROR: #{response["message"]}"
    end
    puts ""
  end 
  
  putLess toput unless toput == []   
end

.list(user = nil) ⇒ Object



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
# File 'lib/hubmaster/repo.rb', line 3

def self.list(user = nil)

  toput = []
  if user.nil?
    request = Github.makeGetRequest("/user/repos?sort=pushed")  
  else
    request = Github.makeGetRequest("/users/#{user}/repos?sort=pushed")  
  end

  response = JSON.parse(request)

  if response.kind_of?(Array)
    response.each do |repo|
      toput << "#{repo['name']} (#{repo['ssh_url']})"  
      toput << " - Description: #{repo['description']}"
      toput << " - Owner: #{repo['owner']['login']}"
      toput << " - Language: #{repo['language']}"
      created_at = repo['created_at'].split("T")
      created_at_date = created_at[0].split("-")
      created_at_date = "#{created_at_date[1]}/#{created_at_date[2]}/#{created_at_date[0]}" 
      toput << " - Created At: #{created_at[1]} on #{created_at_date}"
      updated_at = repo['updated_at'].split("T")
      updated_at_date = updated_at[0].split("-")
      updated_at_date = "#{updated_at_date[1]}/#{updated_at_date[2]}/#{updated_at_date[0]}" 
      toput << " - Last Updated: #{updated_at[1]} on #{updated_at_date}"
      toput << ""
    end
  elsif !response["errors"].nil?
    puts "ERROR: #{response['errors'][0]['message']}"
  elsif !response["message"].nil?
    puts "ERROR: #{response["message"]}"
  end
  
  putLess toput unless toput == []
end