Class: AssertThatBDD::Features

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

Class Method Summary collapse

Class Method Details

.download(accessKey: ENV['ASSERTTHAT_ACCESS_KEY'], secretKey: ENV['ASSERTTHAT_ACCESS_KEY'], token: ENV['ASSERTTHAT_TOKEN'], projectId: nil, outputFolder: './features/', proxy: nil, mode: 'automated', jql: '', tags: '', jiraServerUrl: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/assertthat-bdd.rb', line 8

def self.download(accessKey: ENV['ASSERTTHAT_ACCESS_KEY'], secretKey: ENV['ASSERTTHAT_ACCESS_KEY'], token: ENV['ASSERTTHAT_TOKEN'], projectId: nil, outputFolder: './features/', proxy: nil, mode: 'automated', jql: '', tags: '', jiraServerUrl: nil)
  RestClient.proxy = proxy unless proxy.nil?
  url = ['https://bdd.assertthat.app/rest/api/1/project/', projectId, '/features'].map(&:to_s).join('')
  url = [jiraServerUrl, "/rest/assertthat/latest/project/", projectId, "/client/features"].map(&:to_s).join('') unless jiraServerUrl.nil?
  
  headers = { accept: 'application/zip', params: { mode: mode, jql: jql, tags: tags } }
  if token
    headers[:authorization] = "Bearer #{token}"
    resource = RestClient::Resource.new(url, headers: headers)
  else
    resource = RestClient::Resource.new(url, user: accessKey, password: secretKey, headers: headers)
  end
  
  resource.get do |response, request, result|
    case response.code
    when 401
      puts '*** ERROR: Unauthorized error (401). Supplied secretKey/accessKey is invalid'
      return
    when 400
      puts '*** ERROR: ' + e.response	
      return
    when 500
      puts '*** ERROR: Jira server error (500)'
      return
    end

    Dir.mkdir(outputFolder) unless File.exists?(outputFolder)
    File.open("#{outputFolder}/features.zip", 'wb') { |f| f.write(response) }
    features_count = 0
    Zip::File.open("#{outputFolder}/features.zip") do |zip_file|
      zip_file.each do |entry|
        features_count += 1
        File.delete("#{outputFolder}#{entry.name}") if File.exists?("#{outputFolder}#{entry.name}")
        entry.extract("#{outputFolder}#{entry.name}")
      end
      if response.headers.key?(:features_count) && response.headers.key?(:scenarios_count)
        puts "*** INFO: #{response.headers[:features_count]} features downloaded with #{response.headers[:scenarios_count]} scenarios"
      else
        puts "*** INFO: #{features_count} features downloaded"
      end
      File.delete("#{outputFolder}/features.zip")
    end
  end
end