Class: AssertThatBDD::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/assertthat-bdd.rb

Class Method Summary collapse

Class Method Details

.upload(accessKey: , secretKey: , token: , projectId: nil, runName: 'Test run ' + Time.now.strftime("%d %b %Y %H:%M:%S"), jsonReportFolder: './reports', jsonReportIncludePattern: '.*.json', jql: nil, jiraServerUrl: nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/assertthat-bdd.rb', line 55

def self.upload(accessKey: ENV['ASSERTTHAT_ACCESS_KEY'], secretKey: ENV['ASSERTTHAT_ACCESS_KEY'], token: ENV['ASSERTTHAT_TOKEN'], projectId: nil, runName: 'Test run ' + Time.now.strftime("%d %b %Y %H:%M:%S"), jsonReportFolder: './reports', jsonReportIncludePattern: '.*.json', jql: nil, jiraServerUrl: nil)
  url = "https://bdd.assertthat.app/rest/api/1/project/#{projectId}/report"
  url = "#{jiraServerUrl}/rest/assertthat/latest/project/#{projectId}/client/report" unless jiraServerUrl.nil?

  headers = { params: { runName: runName, jql: jql } }
  headers[:authorization] = "Bearer #{token}" if token

  files = Find.find(jsonReportFolder).grep(/#{jsonReportIncludePattern}/)
  puts "*** INFO: #{files.count} files found matching pattern #{jsonReportIncludePattern}:"
  puts "*** INFO: #{files}"
  runId = -1

  files.each do |f|
    payload = { multipart: true, file: File.new(f, 'rb') }
    
    if token
      request = RestClient::Request.new(
        method: :post,
        url: url,
        payload: payload,
        headers: headers
      )
    else
      request = RestClient::Request.new(
        method: :post,
        url: url,
        user: accessKey,
        password: secretKey,
        payload: payload,
        headers: headers
      )
    end

    begin
      response = request.execute
      response_json = JSON.parse(response)
      if response_json['result'] == 'success'
        runId = response_json['runId']
      else
        puts "*** ERROR: Failed to submit json #{f}: " + response_json['message']
      end
    rescue RestClient::ExceptionWithResponse => e
      case e.http_code
      when 401
        puts '*** ERROR: Unauthorized error (401). Supplied secretKey/accessKey is invalid'
      when 500
        puts '*** ERROR: Jira server error (500)'
      else
        puts "*** ERROR: Failed to submit json #{f}: " + e.message
      end
      return
    end
  end
end