Class: Redmine::Cli::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/redmine-cli/cli.rb

Instance Method Summary collapse

Instance Method Details

#edit(ticket) ⇒ Object



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
# File 'lib/redmine-cli/cli.rb', line 118

def edit(ticket)
  issue = Issue.find(ticket)

  if options.empty?
    say "You must specify at least one field to edit", :red
    exit
  end
  
  data = {}
  options.each { | key, val |
    # Set the initial value to one of:
    #   The direct string representation of the issue's key
    #   The value of the issue's key's "name" member
    #   A multi-line string initialized to a message appropriate to the value (most useful for "notes" and "description")
    initialval = issue.attributes.has_key?(key) && ! issue.attributes[key].nil? ? case issue.attributes[key]
      when String then issue.attributes[key].to_s
      else issue.attributes[key].name.to_s
    end : "Enter your #{key} here.\n\n"

    data[key] = initialval.clone.ed

    if data[key] == initialval
      # If the user didn't actually edit the field, then don't submit it for update.
      say "You did not change the value, so the edit to #{key} was ignored.", :red 
      data.delete(key)
    end
  }

  unless data.empty?
    update_ticket(ticket, data.with_indifferent_access)
  else
    say "There was no new data to submit, so #{ticket} was not updated.", :red
  end


rescue ActiveResource::ResourceNotFound
  say "No ticket with number: #{ticket}", :red
end

#install(url = "localhost:3000", username = "", fieldcsv = "") ⇒ Object



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/redmine-cli/cli.rb', line 215

def install(url = "localhost:3000", username = "", fieldcsv="")
  url = "http://#{url}" unless url =~ /\Ahttp/

  if username.blank?
    username = ask("Username?")
  end

  password = ask_password("Password?")

  if fieldcsv.blank?
    fieldcsv = ask("\nWhat fields should be displayed in \"redmine list\"?\n\nPossible values are: [" + fields.keys.join(", ") + "]\n\nEnter a list of comma-separated fields: ")
  end

  list_fields = fieldcsv.split(",")

  arguments = [url, username, password, list_fields]
  arguments.concat(["--test"]) if options.test

  Redmine::Cli::Generators::Install.start(arguments)
end

#listObject



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
# File 'lib/redmine-cli/cli.rb', line 27

def list
  params = {}

  params[:assigned_to_id] = map_user(options.assigned_to) if options.assigned_to
  params[:status_id] = map_status(options.status) if options.status
  params[:tracker_id] = map_tracker(options.tracker) if options.tracker
  params[:project_id] = map_project(options.project) if options.project
  params[:query_id] = map_query(options.query) if options.query
  params[:sort] = options[:sort] if options[:sort]

  collection = Issue.fetch_all(params)

  selected_fields = Redmine::Cli::config.list_fields || ["url", "subject", "status"]

  unless options.std_output
    # collection.sort! {|i,j| i.status.id <=> j.status.id }
    collection.sort! {|i,j| i.priority.id <=> j.priority.id }
    
    # Retrieve the list of issue fields in selected_fields
    issues = collection.collect { |issue| selected_fields.collect {| key |

      assignee = ""
      assignee = issue.assigned_to.name if issue.respond_to?(:assigned_to)
      version = issue.fixed_version.name if issue.respond_to?(:fixed_version)

      # Hack, because I don't feel like spending much time on this
      next unless version == options.version

      begin
        # If this is a built-in field for which we have a title, ref, and display method, use that.
        field = fields.fetch(key)
        if field.display
          value = issue.attributes.fetch(field.ref)
          field.display.call(value)
        else
          f = fields.fetch(key).ref
          issue.attributes.fetch(f)
        end
      rescue IndexError
        # Otherwise, let's look for a custom field by that name.
        if issue.attributes[:custom_fields].present?
          issue.attributes[:custom_fields].collect { | field | 
            if field.attributes.fetch("name") == key
              field.attributes.fetch("value")
            end
          }
        end
        ""
        #TODO: If the custom field doesn't exist, then we end up returning a blank value (not an error). I guess that's OK?
      end

    }}

    if issues.any?
      issues.insert(0, selected_fields.collect {| key |
        begin
          fields.fetch(key).title
        rescue IndexError
          key
        end
      })

      print_table(issues)
      say "#{collection.count} issues - #{link_to_project(params[:project_id])}", :yellow
    end
    
    # Clean up after ourselves
    issues.compact!
  else
    say collection.collect(&:id).join(" ")
  end
end

#new(subject, description = "") ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/redmine-cli/cli.rb', line 173

def new(subject, description="")
  params =
    Thor::CoreExt::HashWithIndifferentAccess.new(:subject => subject,
                                                 :description => description,
                                                 :project => Redmine::Cli::config.default_project_id)
  params.merge!(options)

  unless params.project
    raise "No default project specified"
  end

  issue = Issue.create(ticket_attributes(params))

  say "Created ticket: #{link_to_issue(issue.id)}", :green
rescue ActiveResource::ResourceNotFound
  say "Could not create ticket with: #{options.inspect}", :red
rescue RuntimeError => e
  say e.message, :red
end

#projectsObject



101
102
103
104
105
106
107
108
# File 'lib/redmine-cli/cli.rb', line 101

def projects
  projects = Project.fetch_all.sort {|i,j| i.name <=> j.name}.collect { |project| [ project.id, project.identifier, project.name ] }
  if projects.any?
    projects.insert(0, ["Id", "Key", "Name"])
    print_table(projects)
    say "#{projects.count-1} projects - #{link_to_project}", :yellow
  end
end

#show(ticket) ⇒ Object



158
159
160
161
162
163
164
165
166
167
# File 'lib/redmine-cli/cli.rb', line 158

def show(ticket)
  params = {}
  params[:params] = {:include => "journals,changesets"}

  issue = Issue.find(ticket, params)

  display_issue(issue)
rescue ActiveResource::ResourceNotFound
  say "No ticket with number: #{ticket}", :red
end

#update(*tickets) ⇒ Object



202
203
204
205
206
207
208
209
210
211
# File 'lib/redmine-cli/cli.rb', line 202

def update(*tickets)
  tickets = options.tickets if tickets.blank? && options.tickets.present?

  if tickets.empty?
    say "No tickets to update", :red
    exit 1
  end

  tickets.collect { |ticket| Thread.new { update_ticket(ticket, options) } }.each(&:join)
end