Class: WssAgent::ResponsePolicies

Inherits:
Response
  • Object
show all
Defined in:
lib/wss_agent/response_policies.rb

Constant Summary collapse

REJECT_ACTION =
'Reject'.freeze

Constants inherited from Response

WssAgent::Response::BAD_REQUEST_STATUS, WssAgent::Response::SERVER_ERROR_STATUS, WssAgent::Response::SUCCESS_STATUS

Instance Attribute Summary

Attributes inherited from Response

#data, #response, #response_data, #status

Instance Method Summary collapse

Methods inherited from Response

#initialize, #parse_error, #response_success?, #success?

Constructor Details

This class inherits a constructor from WssAgent::Response

Instance Method Details

#add_resource(resource) ⇒ Object



58
59
60
61
# File 'lib/wss_agent/response_policies.rb', line 58

def add_resource(resource)
  @policy_violations ||= []
  @policy_violations << resource
end

#check(resource) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wss_agent/response_policies.rb', line 63

def check(resource)
  if resource.key?('resource') && resource.key?('policy') &&
     (resource['policy']['actionType'] == REJECT_ACTION)
    add_resource(
      'resource' => resource['resource'],
      'policy' => resource['policy']
    )
  end

  if resource.key?('children') && resource['children'].is_a?(Array)
    resource['children'].each { |j| check(j) }
  end
end

#check_existing_projectsObject



50
51
52
# File 'lib/wss_agent/response_policies.rb', line 50

def check_existing_projects
  data['existingProjects'].each { |_proj_name, resource| check(resource) }
end

#check_new_projectsObject



54
55
56
# File 'lib/wss_agent/response_policies.rb', line 54

def check_new_projects
  data['newProjects'].each { |_proj_name, resource| check(resource) }
end

#messageObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wss_agent/response_policies.rb', line 23

def message
  if success?
    if policy_violations?
      @message = [
        'Some dependencies do not conform with open source policies',
        'List of violations:'
      ]
      @message << policy_violations.each_with_index.map { |j, i|
        "#{i + 1}. Package: #{j['resource']['displayName']} - #{j['policy']['displayName']}"
      }.join("\n")
      @message.join("\n")
    else
      'All dependencies conform with open source policies'
    end
  end
end

#parse_responseObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/wss_agent/response_policies.rb', line 5

def parse_response
  if response.success?
    begin
      @response_data = MultiJson.load(response.body)
      @status = @response_data['status'].to_i
      @message = @response_data['message']
      check_new_projects
      check_existing_projects
    rescue
      @status = SERVER_ERROR_STATUS
      @message = response.body
    end
  else
    @status = SERVER_ERROR_STATUS
    @message = response.body
  end
end

#policy_violationsObject



40
41
42
# File 'lib/wss_agent/response_policies.rb', line 40

def policy_violations
  @policy_violations || []
end

#policy_violations?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/wss_agent/response_policies.rb', line 44

def policy_violations?
  !policy_violations.nil? &&
    !policy_violations.empty? &&
    policy_violations.size > 0
end