Class: Watson::Remote::GitLab

Inherits:
Object
  • Object
show all
Extended by:
Watson
Defined in:
lib/watson/gitlab.rb

Overview

GitLab remote access class Contains all necessary methods to obtain access to, get issue list, and post issues to GitLab

Constant Summary collapse

DEBUG =

Debug printing for this class

false

Constants included from Watson

BLUE, BOLD, CYAN, GLOBAL_DEBUG_OFF, GLOBAL_DEBUG_ON, GRAY, GREEN, MAGENTA, Watson::RED, Watson::RESET, UNDERLINE, VERSION, WHITE, YELLOW

Class Method Summary collapse

Methods included from Watson

check_less, debug_print

Class Method Details

.get_issues(config) ⇒ Object

Get all remote GitLab issues and store into Config container class



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
# File 'lib/watson/gitlab.rb', line 147

def get_issues(config)

  # Identify method entry
  debug_print "#{ self.class } : #{ __method__ }\n"

  # Set up formatter for printing errors
  # config.output_format should be set based on less status by now
  formatter = Printer.new(config).build_formatter

  # Only attempt to get issues if API is specified
  if config.gitlab_api.empty?
    debug_print "No API found, this shouldn't be called...\n"
    return false
  end


  # GitLab API doesn't allow you to filter based on state (or anything for that matter...)
  # Grab all the issues and then sort through them based on state

  # Get all issues 
  # Create options hash to pass to Remote::http_call
  # Use URI.escape so config file is readable
  opts = {
    :url        => "#{ config.gitlab_endpoint }/api/v3/projects/#{ URI.escape(config.gitlab_repo, "/") }/issues",
    :ssl        => config.gitlab_endpoint.match(/^https/) ? true : false,
    :method     => "GET",
    :headers    => [ { :field => "PRIVATE-TOKEN", :value => config.gitlab_api } ],
    :verbose    => false
  }

  _json, _resp  = Watson::Remote.http_call(opts)

  # Check response to validate repo access
  if _resp.code != "200"
    formatter.print_status "x", RED
    print BOLD + "Unable to access remote #{ config.gitlab_repo }, GitLab API may be invalid\n" + RESET
    print "      Consider running --remote (-r) option to regenerate key\n\n"
    print "      Status: #{ _resp.code } - #{ _resp.message }\n"

    debug_print "GitLab invalid, setting config var\n"
    config.gitlab_valid = false
    return false
  end



  # Create hash entry from each returned issue
  # MD5 of issue serves as hash key
  # Hash value is another hash of info we will use
  _json.each do |issue|

    # Skip this issue if it doesn't have watson md5 tag
    next if (_md5 = issue["description"].match(/__md5__ : (\w+)/)).nil?

    # If it does, use md5 as hash key and populate values with our info
    config.gitlab_issues[_md5[1]] = {
      :title => issue["title"],
      :id    => issue["iid"],
      :state => issue["state"]
    }
  end

  config.gitlab_valid = true
end

.post_issue(issue, config) ⇒ Object

Post given issue to remote GitLab repo



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
# File 'lib/watson/gitlab.rb', line 215

def post_issue(issue, config)
# [todo] - Better way to identify/compare remote->local issues than md5
#        Current md5 based on some things that easily can change, need better ident

  # Identify method entry
  debug_print "#{ self.class } : #{ __method__ }\n"


  # Set up formatter for printing errors
  # config.output_format should be set based on less status by now
  formatter = Printer.new(config).build_formatter


  # Only attempt to get issues if API is specified
  if config.gitlab_api.empty?
    debug_print "No API found, this shouldn't be called...\n"
    return false
  end



  return false if config.gitlab_issues.key?(issue[:md5])
  debug_print "#{issue[:md5]} not found in remote issues, posting\n"


  # We didn't find the md5 for this issue in the open or closed issues, so safe to post

  # Create the body text for the issue here, too long to fit nicely into opts hash
  # [review] - Only give relative path for privacy when posted
  _body =
    "__filename__ : #{ issue[:path] }\n" +
    "__line #__ : #{ issue[:line_number] }\n" +
    "__tag__ : #{ issue[:tag] }\n" +
    "__md5__ : #{ issue[:md5] }\n\n" +
    "#{ issue[:context].join }\n"



  # Create option hash to pass to Remote::http_call
  # Issues URL for GitLab
  opts = {
    :url        => "#{ config.gitlab_endpoint }/api/v3/projects/#{ URI.escape(config.gitlab_repo, "/") }/issues",
    :ssl        => config.gitlab_endpoint.match(/^https/) ? true : false,
    :method     => "POST",
    :headers    => [ { :field => "PRIVATE-TOKEN", :value => config.gitlab_api } ],
    :data       => [{ "title"  => issue[:title] + " [#{ issue[:path] }]",
                     "labels" => "#{issue[:tag]}, watson",
                     "description" => _body }],
    :verbose    => false
  }

  _json, _resp  = Watson::Remote.http_call(opts)


  # Check response to validate repo access
  # Shouldn't be necessary if we passed the last check but just to be safe
  if _resp.code != "201"
    formatter.print_status "x", RED
    print BOLD + "Post unsuccessful. \n" + RESET
    print "      Since the open issues were obtained earlier, something is probably wrong and you should let someone know...\n"
    print "      Status: #{ _resp.code } - #{ _resp.message }\n"
    return false
  end

  # Parse response and append issue hash so we are up to date
  config.gitlab_issues[issue[:md5]] = {
    :title => _json["title"],
    :id    => _json["iid"],
    :state => _json["state"]
  }


  return true
end

.setup(config) ⇒ Object

Setup remote access to GitLab Get Username, Repo, and PW and perform necessary HTTP calls to check validity



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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/watson/gitlab.rb', line 21

def setup(config)

  # Identify method entry
  debug_print "#{ self.class } : #{ __method__ }\n"

  formatter = Printer.new(config).build_formatter
  formatter.print_status "+", GREEN
  print BOLD + "Obtaining OAuth Token for GitLab...\n" + RESET

  # Check config to make sure no previous API exists
  unless config.gitlab_api.empty? && config.gitlab_repo.empty? && config.gitlab_endpoint.empty?
    formatter.print_status "!", RED
    print BOLD + "Previous GitLab API + Repo is in RC, are you sure you want to overwrite?\n" + RESET
    print "      (Y)es/(N)o: "

    # Get user input
    _overwrite = $stdin.gets.chomp
    if ["no", "n"].include?(_overwrite.downcase)
      print "\n\n"
      formatter.print_status "x", RED
      print BOLD + "Not overwriting current GitLab API + repo info\n" + RESET
      return false
    end
  end


  formatter.print_status "!", YELLOW
  print BOLD + "Access to your GitLab account required to make/update issues\n" + RESET
  print "      See help or README for more details on GitHub/Bitbucket/GitLab access\n\n"

  print BOLD + "GitLab API Endpoint: " + RESET
  _endpoint = $stdin.gets.chomp.chomp('/')
  if _endpoint.empty?
    formatter.print_status "x", RED
    print BOLD + "Input blank. Please enter your API endpoint!\n\n" + RESET
    return false
  end
  
  # Make sure we have the http(s)://
  if !_endpoint.match(/(http|https):\/\//)
    _endpoint = "http://#{_endpoint}"
  end
 
   print "\n"

  # GitLab only requires private token to ID, can't get it with basic auth though
  print BOLD + "GitLab Private Token: " + RESET
  _token = $stdin.gets.chomp
  if _token.empty?
    formatter.print_status "x", RED
    print BOLD + "Input blank. Please enter your private token!\n\n" + RESET
    return false
  end

  
  # Get project to be synced against 
  print BOLD + "GitLab Project (ID or Namespace/Name): " + RESET
  _repo = $stdin.gets.chomp
  if _repo.empty?
    formatter.print_status "x", RED
    print BOLD + "Input blank. Please enter project!\n\n" + RESET
    return false
  end

  # Format project to GitLab specs (i.e. if Namespace/Name, need to URL encode /)
  if !_repo.match(/\d+/)
    _repo = URI.escape(_repo, "/") 
  end

  # HTTP Request to make sure we have access to project 
  # GitLab API - http://api.gitlab.org/

  # Create options hash to pass to Remote::http_call
  # Project URL + SSL
  
  opts = {
    :url        => "#{ _endpoint }/api/v3/projects/#{ _repo }",
    :ssl        =>  _endpoint.match(/^https/) ? true : false,
    :method     => "GET",
    :headers    => [ { :field => "PRIVATE-TOKEN", :value => _token } ],
    :verbose    => false
  }

  _json, _resp  = Watson::Remote.http_call(opts)

  # Check response to validate authorization
  if _resp.code == "200"
    formatter.print_status "o", GREEN
    print BOLD + "Succsesfully accessed GitLab with specified settings\n\n" + RESET
  else
    formatter.print_status "x", RED
    print BOLD + "Unable to access GitLab with specified settings\n" + RESET
    print "      Status: #{ _resp.code } - #{ _resp.message }\n\n"
    return false
  end

  # Store endpoint and API key obtained from POST to @config.gitlab_api
  config.gitlab_endpoint = _endpoint
  config.gitlab_api = _token
  config.gitlab_repo = URI.unescape(_repo)    # Unescape for config readability
  debug_print "Config GitLab API Endpoint updated to: #{ config.gitlab_endpoint }\n"
  debug_print "Config GitLab API Key updated to:      #{ config.gitlab_api }\n"
  debug_print "Config GitLab Repo updated to:         #{ config.gitlab_repo }\n"



  # All setup has been completed, need to update RC
  # Call config updater/writer from @config to write config
  debug_print "Updating config with new GitLab info\n"
  config.update_conf("gitlab_api", "gitlab_repo", "gitlab_endpoint")

  # Give user some info
  print "\n"
  formatter.print_status "o", GREEN
  print BOLD + "GitLab successfully setup\n" + RESET
  print "      Issues will now automatically be retrieved from GitLab by default\n"
  print "      Use -u, --update to post issues to GitLab\n"
  print "      See help or README for more details on GitHub/Bitbucket/GitLab access\n\n"

  return true

end