Class: Lightsaber::Trac

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/lightsaber.rb

Constant Summary collapse

@@cookies =
nil
@@config =
YAML.load_file('config/.cli.yml')

Instance Method Summary collapse

Instance Method Details

#create_ticketObject



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
# File 'lib/lightsaber.rb', line 165

def create_ticket
   if @@cookies.nil?

  page = self.class.get("/newticket", request_options)
  @@token = page.xpath("//input[@name='__FORM_TOKEN']")[0]['value']

  summary = ask "Input Summary:"
  reporter = @@config['USERNAME']
  description = ask "Input Description:"
  type = ask "Choose ticket Type between: defect, enhancement, task, project"
  priority = ask "Choose ticket Priority: Immediate, Very High, High, Medium, Low, Very Low"
  milestone = ask "Input Milestone:"
  component = ask "Input Component:"
  version = ask "Input Version:"
  severity = ask "Chose ticket Severity between: Normal, Blocker, Critical, Major, Minor, Trivial"
  keywords = ask "Input Keywords:"
  cc = ask "Input Cc list:"

  component = "- Select a component" if component.empty?

  options = {
    body: {
      __FORM_TOKEN: @@token,
      field_summary: summary,
      field_reporter: reporter,
      __EDITOR__1: "textarea",
      field_description: description,
      field_type: type,
      field_priority: priority,
      field_milestone: milestone,
      field_component: component,
      field_version: version,
      field_cc: cc,
      field_actualpoints: "",
      field_parent: "",
      field_points: "",
      field_reviewer: "",
      field_sponsor: "",
      field_owner: "<+default+>",
      spf_email: "",
      spfh_mail: "",
      submit: "Create+ticket"
    },
    headers: {
      Cookie: "trac_form_token=#{@@token};#{@@cookies.split(';')[0]}",
      contentType: "application/json",
    },
    follow_redirects: false
  }

  response = self.class.post("/newticket", options)
  if response.code == 303
    url = response.headers['location']
    response = self.class.get(url)
    if response.code == 200
      puts "Ticket created: #{url}"
    else
      return [response.code, response.message]
    end
  else
    error = response.xpath("//div[@class='system-message']").text
    return [response.code, response.message, error]
  end

end

#loginObject



29
30
31
32
33
34
35
36
37
38
# File 'lib/lightsaber.rb', line 29

def 
  page = self.class.get("/login")
  @@token = page.xpath("//input[@name='__FORM_TOKEN']")[0]['value']
  response = self.class.post("/login", )
  if response.code == 303
    (response)
  else
    return [response.code, response.message]
  end
end

#reply_ticket(ticket) ⇒ Object



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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/lightsaber.rb', line 99

def reply_ticket(ticket)
   if @@cookies.nil?

  page = self.class.get("/ticket/#{ticket}", request_options)
  @@token = page.xpath("//input[@name='__FORM_TOKEN']")[0]['value']

  summary = page.xpath("//span[@class='summary']").text
  description = page.xpath("//div[@class='searchable']//p").text
  type = page.xpath("//span[@class='trac-type']//a").text
  priority = page.xpath("//td[@headers='h_priority']//a").text
  severity = page.xpath("//td[@headers='h_severity']//a").text
  milestone = page.xpath("//td[@headers='h_milestone']//a").text
  comment = ask "Type your comment:"
  replyto = ask "Type a reply-to user if any:"

  options = {
    body: {
      __FORM_TOKEN: @@token,
      field_reporter: @@config['USERNAME'],
      field_summary: summary,
      field_description: description,
      field_type: type,
      field_priority: priority,
      field_severity: severity,
      field_milestone: milestone,
      comment: comment,
      __EDITOR__1: "textarea",
      __EDITOR__2: "textarea",
      action: "leave",
      field_actualpoints: "",
      field_parent: "",
      field_points: "",
      field_reviewer: "",
      field_sponsor: "",
      field_owner: "<+default+>",
      spf_email: "",
      spfh_mail: "",
      replyto: replyto,
      start_time: "#{Time.now.strftime('%s')}000000",
      view_time: "#{Time.now.strftime('%s')}000000",
      submit: "Submit+changes"
    },
    headers: {
      Cookie: "trac_form_token=#{@@token};#{@@cookies.split(';')[0]}",
      contentType: "application/json",
    },
    follow_redirects: false
  }

  pp options

  response = self.class.post("/ticket/#{ticket}", options)
  if response.code == 303
    url = response.headers['location']
    response = self.class.get(url)
    if response.code == 200
      puts "Ticket created: #{url}"
    else
      return [response.code, response.message]
    end
  else
    error = response.xpath("//div[@class='system-message']").text
    return [response.code, response.message, error]
  end
end

#ticket(ticket) ⇒ Object



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
# File 'lib/lightsaber.rb', line 40

def ticket(ticket)
   if @@cookies.nil?

  response = self.class.get("/ticket/#{ticket}", request_options)
  if response.code == 200
    parsed_res = Nokogiri::HTML(response.body)
    t = {
      ticket_id: parse_id(parsed_res.xpath("//a[@class='trac-id']")),
      date: parse_date(parsed_res.xpath("//div[@class='date']//p")),
      summary: parsed_res.xpath("//span[@class='summary']").text,
      properties: parse_properties(parsed_res.xpath("//table[@class='properties']//tr")),
      description: parse_description(parsed_res.xpath("//div[@class='description']")),
      changelog: parse_log(parsed_res.xpath("//div[@class='change']"))
    }
    return "
            id: #{t[:ticket_id][:id]}

            href: #{t[:ticket_id][:href]}

            created: #{t[:date][:created_at]}

            updated: #{t[:date][:updated_at]}

            summary: #{t[:summary]}

            properties:

            #{t[:properties].map { |key, value| "#{key} #{value}"}.join("\n                \n                ")}

            description: #{t[:description].gsub("Description", "")}

            changelog:

            #{t[:changelog].join("\n                \n                ")}
           "
  else
    return [response.code, response.message]
  end
end

#tickets(options) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/lightsaber.rb', line 80

def tickets(options)
   if @@cookies.nil?

  if options[:query]
    @response = self.class.get("/query?#{options[:query]}")
  else
    filter = options[:filter] || 1
    page = options[:page] || 1
    asc = options[:asc] || 1
    sort = options[:sort] || 'component'
    @response = self.class.get("/report/#{filter}?page=#{page}&asc=#{asc}&sort=#{sort}", request_options)
  end

  if @response.code == 200
    parsed_res = Nokogiri::HTML(@response.body)
    pp parse_filtered(parsed_res.xpath("//tr"))
  end
end