require 'thor' require_relative 'api' require_relative 'configuration' require_relative 'test_report' require_relative 'job_info' module Korinthenkacker class CLI < Thor package_name 'korinthenkacker' desc 'jobs', 'Show all jobs' def jobs puts api.jobs["jobs"].map{ |job| job['name'] } end desc 'builds JOBNAME', 'Display JOBNAME builds and their status' method_option :limit, :aliases => '-l', :type => :numeric def builds(jobname) reports = test_reports(jobname, options.limit) output = [] output << "#\tsuccess\tduration" reports.each{ |report| output << "#{report.build}\t#{report.success?}\t#{report.duration}" } puts output.join("\n") end desc 'failed_scenarios JOBNAME [BUILD]', 'Display JOBNAME failing scenarios' method_option :limit, :aliases => '-l', :type => :numeric def failed_scenarios(jobname, build=nil) if build test_report = TestReport.new(jobname, build, api.test_report(jobname, build)) print_failed_table_header unless test_report.failed_cases.empty? test_report.failed_cases.each { |failed_case| print_scenario_report(build, failed_case) } else reports = test_reports(jobname, options.limit) failed_build_reports = reports.select{ |report| !report.success? } print_failed_table_header unless failed_build_reports.empty? failed_build_reports.each{ |report| report.failed_cases.each{ |failed_case| print_scenario_report(report.build, failed_case) } } end end desc 'report [JOBS...]', 'Display failing scenario for jobs' method_option :limit, :aliases => '-l', :type => :numeric def report(*jobs) jobs.each{ |job| puts job failed_scenarios(job, nil) } end private def print_failed_table_header printf("%s\t%-15s\t%s\n", '#', 'duration', 'failed scenario name') end def print_scenario_report(build, scenario) printf("%s\t%-15s\t'%s'\n", build, scenario.duration, scenario.name) end def test_reports(jobname, limit=nil) job_info = JobInfo.new(api.job(jobname)) builds = options.limit ? job_info.build_numbers.first(options.limit) : job_info.build_numbers builds.map{ |build| begin TestReport.new(jobname, build, api.test_report(jobname, build)) rescue Exception => ex nil end }.compact end def api @api ||= API.new(Configuration.jenkins_url) end end end