Class: GithubBot::Validator::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/github_bot/validator/base.rb

Overview

Public: Base class for validators to extend to provide helpful methods for interactions with GitHub

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Public: Initializes an instance of the validator



14
15
16
17
# File 'lib/github_bot/validator/base.rb', line 14

def initialize
  @feedback = []
  @check = nil
end

Class Method Details

.validateObject

Public: Validation method that every consumer will need to override



10
# File 'lib/github_bot/validator/base.rb', line 10

def validate; end

Instance Method Details

#check_run(name:, **_opts) ⇒ Object

Public: Block for execution logic that is the be evaluated and finalized within the GitHub check run content



49
50
51
52
53
54
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
# File 'lib/github_bot/validator/base.rb', line 49

def check_run(name:, **_opts)
  # queue request for processing
  @check = client_api.create_check_run(
    name: name,
    output: {
      title: "#{name} validation has been queued...",
      summary: "We're awaiting processing."
    }
  )

  yield if block_given?

  if @feedback.empty?
    @check.complete!(
      output: {
        title: "#{name} validation is complete...",
        summary: "#{name} validation passed!"
      }
    )

  else
    # because limitation of 50 checks per API call execution, break up annotations
    # https://developer.github.com/v3/checks/runs/

    # need to identify if something other than warnings
    non_warnings = @feedback.select { |h| h[:annotation_level] != 'warning' }

    if non_warnings.empty?
      @feedback.each_slice(50).to_a.each do |annotations|
        @check.neutral!(
          output: {
            title: "#{name} validation is complete...",
            summary: "#{name} validation determined there are no required changes; however, please review the " \
                     'warnings as they may impact future changes.',
            annotations: annotations
          }
        )
      end
    else
      @feedback.each_slice(50).to_a.each do |annotations|
        @check.action_required!(
          output: {
            title: "#{name} validation is complete...",
            summary: "#{name} validation determined there are changes that need attention.",
            annotations: annotations
          }
        )
      end
    end
  end
rescue StandardError => e
  Rails.logger.error message: 'Error occurred during check run', exception: e
  @check.action_required!(
    output: {
      title: "#{name} validation failed...",
      summary: "# Message\n```\n#{e.message}\n```\n# Exception\n```\n#{e.backtrace.join("\n")}\n```"
    }
  )
end

#feedback(path:, start_line: 0, end_line: 0, annotation_level:, message:, **opts) ⇒ Object

Public: Provide feedback to be generated upon completion of the check run see docs.github.com/en/rest/reference/checks#create-a-check-run for additional details on available parameters for the ‘annotations’ item



22
23
24
25
26
27
28
29
30
31
# File 'lib/github_bot/validator/base.rb', line 22

def feedback(path:, start_line: 0, end_line: 0, annotation_level:, message:, **opts)
  @feedback << {
    path: path,
    start_line: start_line,
    end_line: end_line,
    annotation_level: annotation_level,
    message: message,
    **opts
  }
end

#filesObject

Public: Return all files from request.



110
111
112
# File 'lib/github_bot/validator/base.rb', line 110

def files
  client_api.files
end

#in_progress(**opts) ⇒ Object

Public: Moves the check run into the in progress status

Raises:

  • (StandardError)


34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/github_bot/validator/base.rb', line 34

def in_progress(**opts)
  raise StandardError, 'check run has not been established' unless @check

  defaults = {
    output: {
      title: "#{@check.name} validation is in progress...",
      summary: 'We\'re currently validating this request. Please stand by.'
    }
  }

  @check.in_progress!(defaults.deep_merge(opts))
end

#load_yaml(file) ⇒ Object

Public: Loads the yaml of the file provided

Parameters:

  • file (Sawyer::Resource)

    The file to load



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/github_bot/validator/base.rb', line 136

def load_yaml(file)
  YAML.safe_load(client_api.file_content(file)).with_indifferent_access
rescue StandardError => e
  feedback(
    path: file.filename,
    annotation_level: 'failure',
    message: 'Malformed yaml content'\
             "exception: #{e.message}"
  )

  {}
end

#modified_filesObject

Public: Return the modified files from request.



115
116
117
# File 'lib/github_bot/validator/base.rb', line 115

def modified_files
  client_api.modified_files
end

#validate_allowed_fields(file:, ident:, hash:, allowed_fields:, **_opts) ⇒ Object

Public: Validates for the provided file at a given hash point that the allowed fields are met

Parameters:

  • file (Sawyer::Resource)

    The file for reference that is being evaluated

  • ident (String/Symbol)

    The text identifier for the hash being evaluated

  • hash (Hash)

    The hash to review keys

  • allowed_fields (Array)

    An array of allowed fields



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/github_bot/validator/base.rb', line 220

def validate_allowed_fields(file:, ident:, hash:, allowed_fields:, **_opts)
  unless hash
    feedback(
      path: file.filename,
      annotation_level: 'failure',
      message: "#{ident}: Element is empty or nil"
    )

    return
  end

  hash.keys&.each do |key|
    unless allowed_fields.include?(key)
      feedback(
        path: file.filename,
        annotation_level: 'failure',
        message: "#{ident}: Key '#{key}' not in the allowed fields [#{allowed_fields.join(', ')}]"
      )
    end
  end
end

#validate_fields(file:, ident:, hash:, required_fields:, allowed_fields:, **opts) ⇒ Object

Public: Validates for the provided file at a given hash point that the required and allowed fields are met

Parameters:

  • file (Sawyer::Resource)

    The file for reference that is being evaluated

  • ident (String/Symbol)

    The text identifier for the hash being evaluated

  • hash (Hash)

    The hash to review keys

  • required_fields (Array)

    An array of required elements

  • allowed_fields (Array)

    An array of allowed fields



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/github_bot/validator/base.rb', line 157

def validate_fields(file:, ident:, hash:, required_fields:, allowed_fields:, **opts)
  unless hash
    feedback(
      path: file.filename,
      annotation_level: 'failure',
      message: 'Element is empty or nil'
    )

    return
  end

  validate_required_fields(
    file: file,
    ident: ident,
    hash: hash,
    required_fields: required_fields,
    **opts
  )
  validate_allowed_fields(
    file: file,
    ident: ident,
    hash: hash,
    allowed_fields: allowed_fields,
    **opts
  )
end

#validate_file_extension(file, extension) ⇒ Object

Public: Validates the file extension for the provided file

Parameters:

  • file (Sawyer::Resource)

    The file to evaluate

  • extension (String)

    The extension type to evaluate



123
124
125
126
127
128
129
130
131
# File 'lib/github_bot/validator/base.rb', line 123

def validate_file_extension(file, extension)
  return if File.extname(file.filename) == ".#{extension}"

  feedback(
    path: file.filename,
    annotation_level: 'failure',
    message: "File suffix is incorrect, please use '.#{extension}'"
  )
end

#validate_required_fields(file:, ident:, hash:, required_fields:, **_opts) ⇒ Object

Public: Validates for the provided file at a given hash point that the required fields are met

Parameters:

  • file (Sawyer::Resource)

    The file for reference that is being evaluated

  • ident (String/Symbol)

    The text identifier for the hash being evaluated

  • hash (Hash)

    The hash to review keys

  • required_fields (Array)

    An array of required elements



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/github_bot/validator/base.rb', line 191

def validate_required_fields(file:, ident:, hash:, required_fields:, **_opts)
  unless hash
    feedback(
      path: file.filename,
      annotation_level: 'failure',
      message: "#{ident}: Element is empty or nil"
    )

    return
  end

  required_fields.each do |key|
    next unless hash[key].nil?

    feedback(
      path: file.filename,
      annotation_level: 'failure',
      message: "#{ident}: Missing required element '#{key}'"
    )
  end
end