Class: YouTrack::Client::ApplyIssueCommand

Inherits:
Request
  • Object
show all
Includes:
ParameterRequest
Defined in:
lib/you_track/client/requests/apply_issue_command.rb

Instance Attribute Summary

Attributes included from ParameterRequest

#params

Instance Method Summary collapse

Methods included from ParameterRequest

#_mock, #_real, #setup

Methods inherited from Request

#find, #ms_time, #require_parameters

Instance Method Details

#identityObject



4
5
6
# File 'lib/you_track/client/requests/apply_issue_command.rb', line 4

def identity
  params.fetch("id")
end

#mockObject



16
17
18
19
20
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
# File 'lib/you_track/client/requests/apply_issue_command.rb', line 16

def mock
  issue      = find(:issues, identity)
  comment_id = "#{Cistern::Mock.random_numbers(2)}-#{Cistern::Mock.random_numbers(5)}"

  if comment = params["comment"]
    service.data[:comments][comment_id] = {
      "id"             => comment_id,
      "author"         => service.username,
      "deleted"        => false,
      "text"           => comment,
      "shownForIssuer" => false,
      "created"        => ms_time,
      "issueId"        => identity,
    }
  end

  if command = params["command"]
    words = command.scan(/[^\s]+/) # ["State", "In", "Progress", "Assignee", "jlane"]
    acceptable_commands = find(:project_custom_fields, issue["projectShortName"]).map { |cf| cf["name"] }

    command_map = Hash.new { |h,k| h[k] = [] }

    current_command = words.shift
    words.each do |word|
      unless acceptable_commands.include?(word)
        command_map[current_command] << word
      else
        # @todo validate command on closer
        current_command = word
      end
    end

    commands = command_map.inject({}) { |r,(k,v)| r.merge(k => v.join(" ")) }

    commands.each do |field, value|
      prototype = service.data[:custom_fields].fetch(field)

      bundle_value = if bundle = service.data[:bundles][prototype["defaultBundle"]]
                       bundle["values"].find { |v| v["value"] == value }
                     else # @fixme explode
                       {}
                     end

      if bundle_value["resolved"] == "true"
        issue["resolved"] = ms_time(Time.now)
      end

      issue["custom_fields"].find { |name, _| name == field }[1] = value
    end
  end

  service.response
end

#realObject



8
9
10
11
12
13
14
# File 'lib/you_track/client/requests/apply_issue_command.rb', line 8

def real
  service.request(
    :path   => "/issue/#{identity}/execute",
    :query  => params,
    :method => :post,
  )
end