Class: GitIssue::Redmine

Inherits:
Base
  • Object
show all
Defined in:
lib/git_issue/redmine.rb

Constant Summary

Constants inherited from Base

Base::BRANCH_NAME_FORMAT

Constants included from Helper

Helper::CONFIGURE_MESSAGE

Instance Attribute Summary

Attributes inherited from Base

#apikey, #command, #options, #syserr, #sysout, #tickets

Instance Method Summary collapse

Methods inherited from Base

#apply_colors, #cherry, #connection, #current_branch, #err, #execute, #exit_with_message, #find_command, #guess_ticket, #help, #mktmpdir, #mlength, #mljust, #parse_command_and_tickets, #parse_options, #prompt, #publish, #puts, #rebase, #response_success?, #ticket_and_branch, #ticket_branch, #time_ago_in_words, #to_date, #usage

Methods included from Helper

configure_error, configured_value, get_body_from_editor, get_title_and_body_from_editor, #git_editor, global_configured_value, its_klass_of, #open_editor, #read_body, #split_head_and_body, #work_dir

Constructor Details

#initialize(args, options = {}) ⇒ Redmine

Returns a new instance of Redmine.



6
7
8
9
10
11
12
13
14
# File 'lib/git_issue/redmine.rb', line 6

def initialize(args, options = {})
  super(args, options)

  @apikey = options[:apikey] || configured_value('issue.apikey')
  configure_error('apikey', "git config issue.apikey some_api_key") if @apikey.blank?

  @url = options[:url] || configured_value('issue.url')
  configure_error('url', "git config issue.url http://example.com/redmine")  if @url.blank?
end

Instance Method Details

#add(options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/git_issue/redmine.rb', line 95

def add(options = {})
  property_names = [:project_id, :subject, :description, :done_ratio, :status_id, :priority_id, :tracker_id, :assigned_to_id, :category_id, :fixed_version_id, :notes]

  project_id = options[:project_id] || Helper.configured_value('issue.project')
  if options.slice(*property_names).empty?
    issue = read_issue_from_editor({"project" => {"id" => project_id}}, options)
    description = issue.delete(:notes)
    issue[:description] = description
    options.merge!(issue)
  end

  required_properties = [:subject, :description]
  required_properties.each do |name|
    options[name] = prompt(name) unless options[name]
  end

  json = build_issue_json(options, property_names)
  json["issue"][:project_id] ||= Helper.configured_value('issue.project')

  url = to_url('issues')

  json = post_json(url, json, options)
  puts "created issue #{oneline_issue(json["issue"])}"
end

#branch(options = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/git_issue/redmine.rb', line 141

def branch(options = {})
  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket

  branch_name = ticket_branch(ticket)

  if options[:force]
    system "git branch -D #{branch_name}" if options[:force]
    system "git checkout -b #{branch_name}"
  else
    if %x(git branch -l | grep "#{branch_name}").strip.empty?
      system "git checkout -b #{branch_name}"
    else
      system "git checkout #{branch_name}"
    end
  end

  show(options)
end

#commandsObject



20
21
22
23
24
# File 'lib/git_issue/redmine.rb', line 20

def commands
  cl = super
  cl << GitIssue::Command.new(:local, :loc, 'listing local branches tickets')
  cl << GitIssue::Command.new(:project, :pj, 'listing ticket belongs to sspecified project ')
end

#commit(options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/git_issue/redmine.rb', line 79

def commit(options = {})
  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket

  issue = fetch_issue(ticket)

  f = File.open("./commit_msg_#{ticket}", 'w')
  f.write("refs ##{ticket} #{issue['subject']}")
  f.close

  cmd = "git commit --edit #{options[:all] ? '-a' : ''} --file #{f.path}"
  system(cmd)

  File.unlink f.path if f.path
end

#default_cmdObject



16
17
18
# File 'lib/git_issue/redmine.rb', line 16

def default_cmd
  Helper.configured_value('issue.project').blank? ? :list : :project
end

#list(options = {}) ⇒ Object



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

def list(options = {})
  url = to_url('issues')
  max_count = options[:max_count].to_s if options[:max_count]
  params = {"limit" => max_count || "100" }
  params.merge!("assigned_to_id" => "me") if options[:mine]
  params.merge!(Hash[*(options[:query].split("&").map{|s| s.split("=") }.flatten)]) if options[:query]

  param_list = Hash[*params.map{|k,v| [k,v.split(/,/)] }.flatten(1)]
  keys = param_list.keys
  pl,*pls = param_list.values

  jsons = pl.product(*pls).map{|vs| Hash[*keys.zip(vs).flatten]}.map{|p|
    fetch_json(url, p)['issues']
  }.flatten

  known_ids = []
  issues = jsons.reject{|i|
    known = known_ids.include?(i["id"])
    known_ids << i['id'] unless known
    known
  }

  # json = fetch_json(url, params)

  # output_issues(json['issues'])
  output_issues(issues)
end

#local(option = {}) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/git_issue/redmine.rb', line 161

def local(option = {})
  branches = %x(git branch).split(/\n/).select{|b| b.scan(/(\d+)_/).present?}.map{|b| b.gsub(/^(\s+|\*\s+)/, "")}
  branches.each do |b|
    puts b
    issues = b.scan(/(\d+)_/).map{|ticket_id| fetch_issue(ticket_id) rescue nil}.compact
    issues.each do |i|
      puts "  #{oneline_issue(i, options)}"
    end
    puts ""
  end
end

#mine(options = {}) ⇒ Object



75
76
77
# File 'lib/git_issue/redmine.rb', line 75

def mine(options = {})
  list(options.merge(:mine => true))
end

#project(options = {}) ⇒ Object



173
174
175
176
177
178
# File 'lib/git_issue/redmine.rb', line 173

def project(options = {})
  project_id = Helper.configured_value('issue.project')
  project_id = options[:ticket_id] if project_id.blank?
  raise 'project_id is required.' unless project_id
  list(options.merge(:query => "project_id=#{project_id}"))
end

#show(options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/git_issue/redmine.rb', line 26

def show(options = {})
  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket

  issue = fetch_issue(ticket, options)

  if options[:oneline]
    puts oneline_issue(issue, options)
  else
    puts ""
    puts format_issue(issue, options)
  end
end

#update(options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/git_issue/redmine.rb', line 120

def update(options = {})
  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket

  property_names = [:subject, :done_ratio, :status_id, :priority_id, :tracker_id, :assigned_to_id, :category_id, :fixed_version_id, :notes]

  if options.slice(*property_names).empty?
    org_issue = fetch_issue(ticket, options)
    update_attrs = read_issue_from_editor(org_issue, options)
    update_attrs = update_attrs.reject{|k,v| v.present? && org_issue[k] == v}
    options.merge!(update_attrs)
  end

  json = build_issue_json(options, property_names)

  url = to_url('issues', ticket)
  put_json(url, json, options)
  issue = fetch_issue(ticket)
  puts "updated issue #{oneline_issue(issue)}"
end

#view(options = {}) ⇒ Object



40
41
42
43
44
45
# File 'lib/git_issue/redmine.rb', line 40

def view(options = {})
  ticket = options[:ticket_id]
  raise 'ticket_id is required.' unless ticket
  url = to_url('issues', ticket)
  system "git web--browse #{url}"
end