Module: Diffend::Execute

Defined in:
lib/diffend/execute.rb

Overview

Executes a check for a given command

Class Method Summary collapse

Class Method Details

.build_allow_message(command, response) ⇒ String

Parameters:

  • command (String)

    either install or update

  • response (Hash)

    response from diffend API

Returns:

  • (String)


96
97
98
99
100
101
102
# File 'lib/diffend/execute.rb', line 96

def build_allow_message(command, response)
  "    \#{build_message_header('an allow', command)}\n    \#{build_message_info(response)}\\n\n    \#{response['review_url']}\\n\n  MSG\nend\n"

.build_definition(command) ⇒ Bundler::Definition

Build bundler definition

Returns:

  • (Bundler::Definition)


22
23
24
25
26
27
28
# File 'lib/diffend/execute.rb', line 22

def build_definition(command)
  Diffend::BuildBundlerDefinition.call(
    command,
    Bundler.default_gemfile,
    Bundler.default_lockfile
  )
end

.build_deny_message(command, response) ⇒ String

Parameters:

  • command (String)

    either install or update

  • response (Hash)

    response from diffend API

Returns:

  • (String)


120
121
122
123
124
125
126
# File 'lib/diffend/execute.rb', line 120

def build_deny_message(command, response)
  "    \#{build_message_header('a deny', command)}\n    \#{build_message_info(response)} Please go to the url below and review the issues.\\n\n    \#{response['review_url']}\\n\n  MSG\nend\n"

.build_error(response) ⇒ Object

Parameters:

  • response (Hash)

    response from diffend API

Raises:



49
50
51
52
53
54
# File 'lib/diffend/execute.rb', line 49

def build_error(response)
  build_error_message(response)
    .tap(&Bundler.ui.method(:error))

  raise Diffend::Errors::HandledException
end

.build_error_message(response) ⇒ String

Parameters:

  • response (Hash)

    response from diffend API

Returns:

  • (String)


85
86
87
88
89
90
# File 'lib/diffend/execute.rb', line 85

def build_error_message(response)
  "    \\nDiffend returned an error for your request.\\n\n    \#{response['error']}\\n\n  MSG\nend\n"

.build_message(command, config, response) ⇒ Object

Parameters:

  • command (String)

    either install or update

  • config (OpenStruct)

    diffend config

  • response (Hash)

    response from diffend API



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/diffend/execute.rb', line 33

def build_message(command, config, response)
  if response.key?('error')
    build_error(response)
  elsif response.key?('action')
    build_verdict(command, config, response)
  else
    Diffend::HandleErrors::Report.call(
      config: config,
      message: :unsupported_response,
      payload: response,
      report: true
    )
  end
end

.build_message_header(type, command) ⇒ String

Parameters:

  • type (String)

    verdict type

  • command (String)

    either install or update

Returns:

  • (String)


132
133
134
# File 'lib/diffend/execute.rb', line 132

def build_message_header(type, command)
  "\nDiffend reported #{type} verdict for #{command} command for this project."
end

.build_message_info(response) ⇒ String

Parameters:

  • response (Hash)

    response from diffend API

Returns:

  • (String)


139
140
141
# File 'lib/diffend/execute.rb', line 139

def build_message_info(response)
  "\nQuality score: #{response['quality_score']}, allows: #{response['allows_count']}, warnings: #{response['warns_count']}, denies: #{response['denies_count']}."
end

.build_verdict(command, config, response) ⇒ Object

Parameters:

  • command (String)

    either install or update

  • config (OpenStruct)

    diffend config

  • response (Hash)

    response from diffend API



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/diffend/execute.rb', line 59

def build_verdict(command, config, response)
  case response['action']
  when 'allow'
    build_allow_message(command, response)
      .tap(&Bundler.ui.method(:confirm))
  when 'warn'
    build_warn_message(command, response)
      .tap(&Bundler.ui.method(:warn))
  when 'deny'
    build_deny_message(command, response)
      .tap(&Bundler.ui.method(:error))

    exit 1
  else
    Diffend::HandleErrors::Report.call(
      config: config,
      message: :unsupported_verdict,
      payload: response,
      report: true
    )
  end
end

.build_warn_message(command, response) ⇒ String

Parameters:

  • command (String)

    either install or update

  • response (Hash)

    response from diffend API

Returns:

  • (String)


108
109
110
111
112
113
114
# File 'lib/diffend/execute.rb', line 108

def build_warn_message(command, response)
  "    \#{build_message_header('a warn', command)}\n    \#{build_message_info(response)} Please go to the url below and review the issues.\\n\n    \#{response['review_url']}\\n\n  MSG\nend\n"

.call(command, config) ⇒ Object

Build verdict

Parameters:

  • command (String)

    either install or update

  • config (OpenStruct)

    diffend config



11
12
13
14
15
16
17
# File 'lib/diffend/execute.rb', line 11

def call(command, config)
  Diffend::RequestVerdict
    .call(command, config, build_definition(command))
    .tap { |response| build_message(command, config, response) }
rescue Diffend::Errors::DependenciesResolveException
  # We are unable to resolve dependencies, no message will be printed
end