|
| 1 | +require "capistrano" |
1 | 2 | require "capistrano/ghostinspector/version" |
2 | 3 | require "capistrano/ghostinspector/arrays" |
3 | 4 | require "capistrano/ghostinspector/api" |
4 | | -require "capistrano" |
| 5 | +require "capistrano/ghostinspector/analytics" |
5 | 6 |
|
6 | 7 | module Capistrano |
7 | 8 | module Ghostinspector |
8 | 9 | def self.load_into(config) |
9 | 10 | config.load do |
10 | | - after "deploy", "capistrano:ghostinspector:run" |
| 11 | + after "deploy", "ghostinspector:setup" |
| 12 | + after "ghostinspector:setup", "ghostinspector:run" |
11 | 13 |
|
12 | | - namespace :capistrano do |
13 | | - namespace :ghostinspector do |
14 | | - task :run, :only => { :primary => true } do |
| 14 | + gi_config = YAML::load(File.read("gi_config.yaml")) |
15 | 15 |
|
16 | | - set :giconfig, YAML::load(File.read("gi_config.yaml")) |
| 16 | + namespace :ghostinspector do |
| 17 | + desc "Setup Ghost Inspector Config" |
| 18 | + task :setup, :only => { :primary => true } do |
17 | 19 |
|
18 | | - # Ghost Inspector API key |
19 | | - set :gi_api_key, giconfig["APIKEY"] |
| 20 | + # Ghost Inspector API key |
| 21 | + set :gi_api_key, gi_config["APIKEY"] |
20 | 22 |
|
21 | | - # Google Analytics Tracking Property |
22 | | - set :ga_property, giconfig["ga_property"] |
| 23 | + # Google Analytics Tracking Property |
| 24 | + set :ga_property, gi_config['ga_property'] |
23 | 25 |
|
24 | | - # Get tests and suites from command line |
25 | | - set :gitest, fetch(:gitest, nil) |
26 | | - set :gisuite, fetch(:gisuite, nil) |
| 26 | + if gi_config.has_key?("ga_custom_1") |
| 27 | + set :ga_custom_1, gi_config["ga_custom_1"] |
| 28 | + else |
| 29 | + set :ga_custom_1, "1" |
| 30 | + end |
27 | 31 |
|
28 | | - # Check if GI is enabled for this deployment (Default: true) |
29 | | - set :gi_enabled, fetch(:gi_enabled, giconfig["gi_enabled"]) |
| 32 | + if gi_config.has_key?("ga_custom_2") |
| 33 | + set :ga_custom_2, gi_config["ga_custom_2"] |
| 34 | + else |
| 35 | + set :ga_custom_2, "2" |
| 36 | + end |
30 | 37 |
|
31 | | - # Should we rollback on failed GI tests (Default: true) |
32 | | - set :rollback, fetch(:rollback, giconfig["rollback"]) |
| 38 | + if gi_config.has_key?("jira_project_code") |
| 39 | + set :jira_project_code, gi_config["jira_project_code"] |
| 40 | + else |
| 41 | + set :jira_project_code, "GHOST" |
| 42 | + end |
33 | 43 |
|
34 | | - if (gi_enabled == true) |
| 44 | + # Get tests and suites from command line |
| 45 | + set :gitest, fetch(:gitest, nil) |
| 46 | + set :gisuite, fetch(:gisuite, nil) |
35 | 47 |
|
36 | | - # run each test |
37 | | - Capistrano::Ghostinspector.getTests(gitest, giconfig["tests"]).each do |test| |
38 | | - puts "* * * Running Ghost Inspector Test * * *" |
39 | | - set :passing, Capistrano::Ghostinspector::Api.executeApi("tests", test, gi_api_key, domain, rollback, ga_property, current_revision[0,7]) |
40 | | - end |
| 48 | + # Check if GI is enabled for this deployment (Default: true) |
| 49 | + set :gi_enabled, fetch(:gi_enabled, gi_config['gi_enabled']) |
41 | 50 |
|
42 | | - # run each suite |
43 | | - Capistrano::Ghostinspector.getTests(gisuite, giconfig["suites"]).each do |suite| |
44 | | - puts "* * * Running Ghost Inspector Suite * * *" |
45 | | - set :passing, Capistrano::Ghostinspector::Api.executeApi("suites", suite, gi_api_key, domain, rollback, ga_property, current_revision[0,7]) |
46 | | - end |
| 51 | + # Should we rollback on failed GI tests (Default: true) |
| 52 | + set :rollback, fetch(:rollback, gi_config['rollback']) |
| 53 | + end |
| 54 | + |
| 55 | + desc "Run Ghost Inspector Tests" |
| 56 | + task :run, :only => { :primary => true } do |
| 57 | + |
| 58 | + if (fetch(:gi_enabled) == true) |
| 59 | + |
| 60 | + giApi = Api.new(fetch(:gi_api_key), fetch(:domain), fetch(:rollback), fetch(:ga_property)) |
| 61 | + |
| 62 | + @collection = Array.new |
| 63 | + # run each test |
| 64 | + Capistrano::Ghostinspector.getTests(fetch(:gitest), gi_config["tests"]).each do |test| |
| 65 | + puts "* * * Running Ghost Inspector Test * * *" |
| 66 | + set :data, giApi.executeApi("tests", test) |
| 67 | + |
| 68 | + items = { :passing => data[0], :results => data[1], :type => "tests"} |
| 69 | + @collection << items |
| 70 | + end |
47 | 71 |
|
48 | | - # If any test fails and the stage allows rollbacks then |
49 | | - # rollback to previous version. |
50 | | - if (passing == false && rollback == true) |
51 | | - puts "* * * Ghost Inspector Failed. Rolling back * * *" |
52 | | - run_locally %{cap #{stage} deploy:rollback} |
53 | | - else |
54 | | - puts "* * * Ghost Inspector Complete. Deployment Complete * * *" |
| 72 | + # run each suite |
| 73 | + Capistrano::Ghostinspector.getTests(fetch(:gisuite), gi_config["suites"]).each do |suite| |
| 74 | + puts "* * * Running Ghost Inspector Suite * * *" |
| 75 | + set :data, giApi.executeApi("suites", suite) |
| 76 | + |
| 77 | + data[1]["data"].each do |test| |
| 78 | + items = { :passing => test["passing"], :results => test, :type => "suites"} |
| 79 | + @collection << items |
55 | 80 | end |
56 | 81 |
|
57 | 82 | end |
| 83 | + |
| 84 | + end |
| 85 | + |
| 86 | + end |
| 87 | + |
| 88 | + desc "Send Results to Google Analytics" |
| 89 | + task :sendGA, :only => { :primary => true } do |
| 90 | + |
| 91 | + puts "* * * Sending Data to Google Analytics * * *" |
| 92 | + |
| 93 | + jira_project_code = fetch(:jira_project_code) |
| 94 | + |
| 95 | + log = capture( |
| 96 | + "cd #{current_path} && git log #{previous_revision[0,7]}..#{current_revision[0,7]} --format=\"%s\" | grep -oh '#{jira_project_code}-[0-9]\\+' | sort | uniq" |
| 97 | + ) |
| 98 | + |
| 99 | + options = { |
| 100 | + :ga_property => fetch(:ga_property), |
| 101 | + :ga_custom_1 => fetch(:ga_custom_1), |
| 102 | + :ga_custom_2 => fetch(:ga_custom_2), |
| 103 | + :domain => fetch(:domain), |
| 104 | + :current_revision => fetch(:current_revision), |
| 105 | + :previous_revision => fetch(:previous_revision), |
| 106 | + :branch => fetch(:branch, "default"), |
| 107 | + :stage => fetch(:stage), |
| 108 | + :tickets => Capistrano::Ghostinspector.getTickets(log) |
| 109 | + } |
| 110 | + |
| 111 | + analytics = Analytics.new(options) |
| 112 | + |
| 113 | + @collection.each do |item| |
| 114 | + analytics.pushData(item[:type], item[:results]) |
| 115 | + end |
| 116 | + |
| 117 | + end |
| 118 | + |
| 119 | + desc "Finalise Ghost Inspector Run" |
| 120 | + task :finalise_run, :only => { :primary => true } do |
| 121 | + |
| 122 | + set :passing, true |
| 123 | + @collection.each do |item| |
| 124 | + if item[:passing] == false |
| 125 | + set :passing, false |
| 126 | + end |
58 | 127 | end |
| 128 | + |
| 129 | + # If any test fails and the stage allows rollbacks then |
| 130 | + # rollback to previous version. |
| 131 | + if (fetch(:passing) == false && fetch(:rollback) == true) |
| 132 | + puts "* * * Ghost Inspector Failed. Rolling back * * *" |
| 133 | + run_locally %{cap #{stage} deploy:rollback} |
| 134 | + else |
| 135 | + puts "* * * Ghost Inspector Complete. Deployment Complete * * *" |
| 136 | + end |
| 137 | + |
59 | 138 | end |
| 139 | + |
60 | 140 | end |
61 | 141 |
|
| 142 | + |
| 143 | + after "ghostinspector:run", "ghostinspector:sendGA" |
| 144 | + after "ghostinspector:sendGA", "ghostinspector:finalise_run" |
| 145 | + |
62 | 146 | end |
63 | 147 | end |
| 148 | + |
| 149 | + |
| 150 | + def self.getTickets(log) |
| 151 | + |
| 152 | + tickets = "" |
| 153 | + log.each_line do |line| |
| 154 | + line.delete!('";') |
| 155 | + line.strip! |
| 156 | + line.gsub!("'", '\u0027') |
| 157 | + tickets = "#{tickets}, #{line}" |
| 158 | + end |
| 159 | + |
| 160 | + if (tickets.to_s == "") |
| 161 | + tickets = "None" |
| 162 | + else |
| 163 | + tickets[0] = '' |
| 164 | + end |
| 165 | + |
| 166 | + return tickets |
| 167 | + |
| 168 | + end |
| 169 | + |
| 170 | + |
64 | 171 | end |
65 | 172 | end |
66 | 173 |
|
|
0 commit comments