Class: Watson::Remote::Asana

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

Constant Summary collapse

DEBUG =

Debug printing for this class

true

Constants included from Watson

BLUE, BOLD, CYAN, 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 Asana issues and store into Config container class



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
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/watson/asana.rb', line 262

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.asana_api.empty?
    debug_print "No asana API found, this shouldn't be called...\n"
    return false
  end

  _api_key = config.asana_api
  _workspace = config.asana_workspace
  _project = config.asana_project

  task_records = get_tasks(_api_key, _project, _workspace)

  unless task_records
    config.asana_valid = false
    return false
  end

  task_records.each do |issue|
    # Skip this issue if it doesn't have watson md5 tag
    _md5 = issue["notes"].match(/.*__md5__ : (\w+)\s.*/)
    next if (_md5).nil?

    # If it does, use md5 as hash key and populate values with our info
    config.asana_issues[_md5[1]] = {
        :title => issue["name"],
        :id    => issue["id"],
        :state => issue["completed"] # TODO: How to check status?
    }
  end

  config.asana_valid = true

end

.post_issue(issue, config) ⇒ Object

Post given issue to Asana project



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

def post_issue(issue, 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.asana_api.empty?
    debug_print "No asana API found, this shouldn't be called...\n"
    return false
  end

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

  _api_key = config.asana_api
  _workspace = config.asana_workspace
  _project = config.asana_project

  workspace_id = get_workspace_id(_api_key, _workspace)

  unless workspace_id
    @formatter.print_status "x", RED
    print BOLD + "Unable to get workspace info from Asana API\n" + RESET
    return false
  end

  tags = init_tags(config, _api_key, workspace_id)

  unless tags
    @formatter.print_status "x", RED
    print BOLD + "Unable to initialise tags\n" + RESET
    return false
  end

  project_id = get_project_identifier(_api_key, _project, workspace_id)

  unless project_id
    @formatter.print_status "x", RED
    print BOLD + "Unable to get project info from Asana API\n" + RESET
    return false
  end

  tasks_url = "#{ @end_point }/workspaces/#{ workspace_id }/tasks"

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

  opts = {
      :url => tasks_url,
      :ssl => true,
      :method => "POST",
      :basic_auth => [_api_key, ""],
      :data => [{"name" => issue[:title],
                 "notes" => _body,
                 "projects" => project_id}],
      :verbose => false
  }

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

  unless _resp.code == "201"
    @formatter.print_status "x", RED
    print BOLD + "Unable to access Asana API, key may be invalid\n" + RESET
    print "      Consider running --remote (-r) option to regenerate key\n\n"
    print "      Status: #{ _resp.code } - #{ _resp.message }\n"
    return false
  end

  _data = _json["data"]
  new_task_id = _data["id"]

  debug_print "creating file tag"
  file_tag = create_or_get_tag(_api_key,workspace_id,issue[:path])
  debug_print "tagging with file tag <#{ file_tag }>"

  if file_tag
    file_tag_id = file_tag['id']
    unless tag_task(_api_key, new_task_id, file_tag_id)
      @formatter.print_status "!", RED
      print BOLD + "Unable to tag with '#{ issue[:path] }'\n" + RESET
    end
  else
    @formatter.print_status "!", RED
    print BOLD + "Unable create tag '#{ issue[:path] }'\n" + RESET
  end

  debug_print "tagging with issue tag <#{ issue[:tag] }>"
  issue_tag_id = tags[issue[:tag]]['id']
  unless tag_task(_api_key, new_task_id, issue_tag_id)
    @formatter.print_status "!", RED
    print BOLD + "Unable to tag with '#{ issue[:tag] }'\n" + RESET
  end

  debug_print "tagging with watson tag"
  watson_tag_id = tags['watson']['id']
  unless tag_task(_api_key, new_task_id, watson_tag_id)
    @formatter.print_status "!", RED
    print BOLD + "Unable to tag with 'watson'\n" + RESET
  end

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

  true

end

.setup(config) ⇒ Object

Setup remote access to Asana Get API Key, Workspace, Project



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

def setup(config)

  @formatter = Printer.new(config).build_formatter

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

  @formatter.print_status "+", GREEN
  print BOLD + "Setting up Asana...\n" + RESET

  config_exists = config.asana_api.empty? && config.asana_workspace.empty? && config.asana_project.empty?
  unless config_exists
    @formatter.print_status "!", RED
    print BOLD + "Previous Asana API 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 Asana config\n" + RESET
      return false
    end
  end

  @formatter.print_status "!", YELLOW
  print BOLD + "Asana API key required to make/update issues\n" + RESET
  print "      See help or README for more details on Asana access\n\n"

  print "\n"

  print BOLD + "API Key: " + RESET
  _api_key = $stdin.gets.chomp
  if _api_key.empty?
    @formatter.print_status "x", RED
    print BOLD + "Input blank. Please enter your api key!\n\n" + RESET
    return false
  end

  print BOLD + "Workspace: " + RESET
  _workspace = $stdin.gets.chomp
  if _workspace.empty?
    @formatter.print_status "x", RED
    print BOLD + "Input blank. Please enter your workspace!\n\n" + RESET
    return false
  end

  print BOLD + "Project: " + RESET
  _project = $stdin.gets.chomp
  if _project.empty?
    @formatter.print_status "x", RED
    print BOLD + "Input blank. Please enter your project!\n\n" + RESET
    return false
  end

  workspace_dict = get_workspaces(_api_key)

  unless workspace_dict
    @formatter.print_status "x", RED
    print BOLD + "Asana setup failed\n" + RESET
    return false
  end

  unless workspace_dict.include?(_workspace)
    print "\n"
    @formatter.print_status "x", RED
    print BOLD + "Workspace doesn't exist\n" + RESET
    print "      Check that the workspace name is correct\n"
    print "      Possible workspaces: '#{ workspace_dict.keys.join('\', \'') }'\n\n"
    return false
  end

  workspace_id = workspace_dict[_workspace]
  project_dict = get_projects(_api_key, workspace_id)

  unless project_dict
    @formatter.print_status "x", RED
    print BOLD + "Asana API Request failed\n" + RESET
    return false
  end

  unless project_dict.include?(_project)
    print "\n"
    @formatter.print_status "x", RED
    print BOLD + "Project doesn't exist within workspace '#{ _workspace }'\n" + RESET
    print "      Check that the project name is correct\n"
    print "      Possible projects: '#{ project_dict.keys.join('\', \'') }'\n\n"
    return false
  end

  config.asana_api = _api_key
  debug_print "Asana API Key updated to: #{ config.asana_api }\n"
  config.asana_workspace = _workspace
  debug_print "Asana Workspace updated to: #{ config.asana_workspace }\n"
  config.asana_project = _project
  debug_print "Asana Project updated to: #{ config.asana_project }\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 Asana info\n"
  config.update_conf("asana_api", "asana_workspace", "asana_project")

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

  true

end