require "uri" require "net/http" require "cgi" require 'lita' module Lita module Handlers class Youtrack < Handler def initialize(robot) super end def self.default_config(config) config.login = nil config.password = nil end route(/\bproject (\w+)?/i, :project, command: true, help: { "project car" => "Displays the issues with youtrack project car"}) route(/\bprojects\b/i, :projects, command: true, help: { "projects" => "Displays a list of projects and their short names"}) route(/\bbugs *(\w*)/i, :bugs, command: true, help: { "bugs" => "Display all issues with filter -Done -{Parking Lot} #Bug sort by: AMA_Priorities, Updated ", "bugs parking" => "Also displays bugs in parking lot"}) def project(response) project_name = response.matches[0][0] uri = URI("#{youtrack_hosturl}/rest/export/#{project_name}/issues") response_body = call_http(uri) parse_data = parse_project(response_body) response.reply project_response(parse_data, project_name) end def projects(response) uri = URI("#{youtrack_hosturl}/rest/project/all") response_body = call_http(uri) parse_data = parse_projects(response_body) response.reply projects_response(parse_data) end def bugs(response) filter = filter(response.matches[0][0]) uri = URI("#{youtrack_hosturl}/rest/issue?filter=%23Bug+-Done+#{filter}sort+by%3A+AMA_Priorities%2C+Updated&max=25") response_body = call_http(uri) parse_data = parse_bugs(response_body, filter) response.reply bugs_response(parse_data) end def call_http (uri) Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http| resp, data = http.get(uri, login) resp.body end end private def filter(param) if param.downcase == "parking" "" else "-%7BParking+Lot%7D+" end end def parse_project(response_body) data_hash = { responseString: "" , numbers: [] , longnames: response_body.scan(/ *(.*?)<\/value>/i) } response_body.scan(/name="numberInProject">\d*/i) { |m| data_hash[:numbers] << m.scan(/[0-9]+/).first } data_hash[:responseString] = "Project not found - type 'lita projects' for list" if data_hash[:longnames].count == 0 data_hash end def parse_projects(response_body) data_hash = { responseString: "", longnames: [], shortnames: []} response_body.scan(/ *(.*?)<\/value>/i), shortnames: [], responseString: ""} data_hash[:responseString] += "I found #{data_hash[:longnames].count} bugs. Bugs can be viewed at #{youtrack_hosturl}/issues?q=-Done+#{filter}%23Bug+sort+by%3A+AMA_Priorities%2C+Updated\n" response_body.scan(/name="projectShortName">\n*[A-Z]*<\/value>/) { |m| data_hash[:shortnames] << m.scan( />([^>]*)\d*/i) { |m| data_hash[:numbers] << m.gsub(/[^0-9]/i,'')} data_hash end def project_response(parse_data = {}, project_name) parse_data[:longnames].each_with_index do |title, index| parse_data[:responseString] += title[0] + " - #{youtrack_hosturl}/issue/#{project_name}-#{parse_data[:numbers][index]}\n" end parse_data[:responseString] end def projects_response(parse_data = {}) parse_data[:longnames].each_with_index do | title, index| parse_data[:responseString] += parse_data[:shortnames][index] + " - " + title + " - #{youtrack_hosturl}/issues?q=project:+{#{title.gsub(' ', '+')}}\n" end parse_data[:responseString] end def bugs_response(parse_data = {}) parse_data[:longnames].each_with_index do | title, index| parse_data[:responseString] += parse_data[:shortnames][index] + "-" + parse_data[:numbers][index] + " - " + title[0] + " - #{youtrack_hosturl}/issue/#{parse_data[:shortnames][index]}-#{parse_data[:numbers][index]}\n" end parse_data[:responseString] end def login name = youtrack_login password = youtrack_password http_response = Net::HTTP.post_form(URI.parse("#{youtrack_hosturl}/rest/user/login"), 'login' => name, 'password' => password) cookie = http_response['set-cookie'] headers = { 'Cookie' => cookie } end def youtrack_login Lita.config.handlers.youtrack.login end def youtrack_password Lita.config.handlers.youtrack.password end def youtrack_hosturl Lita.config.handlers.youtrack.hosturl end end Lita.register_handler(Youtrack) end end