module Capistrano::Ghostinspector

Constants

VERSION

Public Class Methods

getTests(test, giconfig, default) click to toggle source
# File lib/capistrano/ghostinspector/arrays.rb, line 3
def self.getTests(test, giconfig, default)

  # Return an array of tests/suites to
  # run in ghost inspector
  array = Array.new
  if (test != nil)
    test.split(',').each do |key|
      if (giconfig.has_key?(key))
        array << giconfig[key]
      end
    end
  end

  # add any default tests or suites set by the stage
  if (default != nil)
    default.split(',').each do |key|
      if (giconfig.has_key?(key))
        if(array.include?(giconfig[key]))
          # do nothing, it already exists
        else
          array << giconfig[key]
        end            
      end
    end
  end

  return array

end
getTickets(log) click to toggle source
# File lib/capistrano/ghostinspector.rb, line 172
def self.getTickets(log)

    tickets = ""
    log.each_line do |line|
        line.delete!('";')
        line.strip!
        line.gsub!("'", '\u0027')
        tickets = "#{tickets}, #{line}"
    end

    if (tickets.to_s == "")
        tickets = "None"
    else
        tickets[0] = ''
    end

    return tickets

end
load_into(config) click to toggle source
# File lib/capistrano/ghostinspector.rb, line 9
def self.load_into(config)
  config.load do
    after "ghostinspector:setup", "ghostinspector:run"

    gi_config = YAML::load(File.read("gi_config.yaml"))

    namespace :ghostinspector do
      desc "Setup Ghost Inspector Config"
      task :setup, :only => { :primary => true } do

        # Ghost Inspector API key
        set :gi_api_key, gi_config["APIKEY"]

        # Google Analytics Tracking Property
        set :ga_property, gi_config['ga_property']

        if gi_config.has_key?("ga_custom_1")
          set :ga_custom_1, gi_config["ga_custom_1"]
        else
          set :ga_custom_1, "1"
        end

        if gi_config.has_key?("ga_custom_2")
          set :ga_custom_2, gi_config["ga_custom_2"]
        else
          set :ga_custom_2, "2"
        end

        if gi_config.has_key?("jira_project_code")
          set :jira_project_code, gi_config["jira_project_code"]
        else
          set :jira_project_code, "GHOST"
        end

        if gi_config.has_key?("ga_enabled")
          set :ga_enabled, fetch(:ga_enabled, gi_config["ga_enabled"])
        else
          set :ga_enabled, fetch(:ga_enabled, false)
        end

        set :domain, fetch(:domain, nil)

        # Get tests and suites from command line
        set :gitest, fetch(:gitest, nil)
        set :gisuite, fetch(:gisuite, nil)

        # Get any default tests that have been set on the stage
        set :gi_default_test, fetch(:gi_default_test, nil)
        set :gi_default_suite, fetch(:gi_default_suite, nil)

        # Check if GI is enabled for this deployment (Default: true)
        set :gi_enabled, fetch(:gi_enabled, gi_config['gi_enabled'])

        # Should we rollback on failed GI tests (Default: true)
        set :rollback, fetch(:rollback, gi_config['rollback'])
      end
      
      desc "Run Ghost Inspector Tests"
      task :run, :only => { :primary => true } do

        if (fetch(:gi_enabled) == true)

          giApi = Api.new(fetch(:gi_api_key), fetch(:domain), fetch(:rollback), fetch(:ga_enabled))
          
          @collection = Array.new
          # run each test
          Capistrano::Ghostinspector.getTests(fetch(:gitest), gi_config["tests"], fetch(:gi_default_test)).each do |test|
            puts "* * * Running Ghost Inspector Test * * *"
            set :data, giApi.executeApi("tests", test)

            items = { :passing => data[0], :results => data[1], :type =>  "tests"}
            @collection << items
          end

          # run each suite
          Capistrano::Ghostinspector.getTests(fetch(:gisuite), gi_config["suites"], fetch(:gi_default_suite)).each do |suite|
            puts "* * * Running Ghost Inspector Suite * * *"
            set :data, giApi.executeApi("suites", suite)

            data[1]["data"].each do |test|
              items = {
                :passing => giApi.shouldWaitForResults() ? true : test["passing"],
                :results => test,
                :type =>  "suites"
              }
              @collection << items
            end

          end

        end

      end

      desc "Send Results to Google Analytics"
      task :sendGA, :only => { :primary => true } do

        if (fetch(:gi_enabled) == true && fetch(:ga_enabled) == true && fetch(:domain) != nil)

          puts "* * * Sending Data to Google Analytics * * *"

          jira_project_code = fetch(:jira_project_code)

          log = capture(
            "cd #{current_path} && git log #{previous_revision[0,7]}..#{current_revision[0,7]} --format=\"%s\" | grep -oh '#{jira_project_code}-[0-9]\\+' | sort | uniq"
          )

          options = { 
            :ga_property => fetch(:ga_property),
            :ga_custom_1 => fetch(:ga_custom_1),
            :ga_custom_2 => fetch(:ga_custom_2),
            :domain => fetch(:domain), 
            :current_revision => fetch(:current_revision),
            :previous_revision => fetch(:previous_revision),
            :branch => fetch(:branch, "default"),
            :stage => fetch(:stage),
            :tickets => Capistrano::Ghostinspector.getTickets(log)
          }

          analytics = Analytics.new(options)

          @collection.each do |item|
            analytics.pushData(item[:type], item[:results])
          end

        end

      end

      desc "Finalise Ghost Inspector Run"
      task :finalise_run, :only => { :primary => true } do

        if (fetch(:gi_enabled) == true)
          set :passing, true
          @collection.each do |item|
            if item[:passing] == false
              set :passing, false
            end
          end

          # If any test fails and the stage allows rollbacks then
          # rollback to previous version.
          if (fetch(:passing) == false && fetch(:rollback) == true)
            puts "* * * Ghost Inspector Failed. Rolling back * * *"
            run_locally %{cap #{stage} deploy:rollback}
          else
            puts "* * * Ghost Inspector Complete. Deployment Complete * * *"
          end

        end
        
      end

    end


    after "ghostinspector:run", "ghostinspector:sendGA"
    after "ghostinspector:sendGA", "ghostinspector:finalise_run"

  end
end