Class: GithubBot::Validator::Base
- Inherits:
-
Object
- Object
- GithubBot::Validator::Base
- 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
-
.validate ⇒ Object
Public: Validation method that every consumer will need to override.
Instance Method Summary collapse
-
#check_run(name:, **_opts) ⇒ Object
Public: Block for execution logic that is the be evaluated and finalized within the GitHub check run content.
-
#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.
-
#files ⇒ Object
Public: Return all files from request.
-
#in_progress(**opts) ⇒ Object
Public: Moves the check run into the in progress status.
-
#initialize ⇒ Base
constructor
Public: Initializes an instance of the validator.
-
#load_yaml(file) ⇒ Object
Public: Loads the yaml of the file provided.
-
#modified_files ⇒ Object
Public: Return the modified files from request.
-
#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.
-
#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.
-
#validate_file_extension(file, extension) ⇒ Object
Public: Validates the file extension for the provided file.
-
#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.
Constructor Details
#initialize ⇒ Base
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
.validate ⇒ Object
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 |
# 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/ @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 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: , **opts } end |
#files ⇒ Object
Public: Return all files from request.
93 94 95 |
# File 'lib/github_bot/validator/base.rb', line 93 def files client_api.files end |
#in_progress(**opts) ⇒ Object
Public: Moves the check run into the in progress status
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
119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/github_bot/validator/base.rb', line 119 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_files ⇒ Object
Public: Return the modified files from request.
98 99 100 |
# File 'lib/github_bot/validator/base.rb', line 98 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
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/github_bot/validator/base.rb', line 203 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
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/github_bot/validator/base.rb', line 140 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
106 107 108 109 110 111 112 113 114 |
# File 'lib/github_bot/validator/base.rb', line 106 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
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/github_bot/validator/base.rb', line 174 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 |