-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(PDK-470) Validation of task metadata. #301
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
40f1ef7
(PDK-470) Validation of task metadata.
bmjen a4874a8
(fixup) Stylistic changes
rodjek 8e2744f
(fixup) Enumerable#any? is a more idiomatic way of doing this
rodjek 7ac422a
(fixup) Split PDK::Validate::TaskMetadataLint.schema_file into simple…
rodjek 0b3b835
Add exception debug
bmjen 3b762ec
Potential workaround for windows SSL issue
scotje 6f9c9c1
fixup powershell invocation
scotje 9977180
switching to net-http
bmjen 15bf7ff
test and lint fixups
bmjen 3c6148a
docs
bmjen dc28a9d
only set verify_none on windows
bmjen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
require 'pdk' | ||
require 'pdk/cli/exec' | ||
require 'pdk/validators/base_validator' | ||
require 'pdk/util' | ||
require 'pathname' | ||
require 'json-schema' | ||
|
||
module PDK | ||
module Validate | ||
class TaskMetadataLint < BaseValidator | ||
FORGE_SCHEMA_URL = 'https://forgeapi.puppet.com/schemas/task.json'.freeze | ||
|
||
def self.name | ||
'task-metadata-lint' | ||
end | ||
|
||
def self.pattern | ||
'tasks/*.json' | ||
end | ||
|
||
def self.spinner_text(_targets = []) | ||
_('Checking task metadata style (%{targets}).') % { | ||
targets: pattern, | ||
} | ||
end | ||
|
||
def self.create_spinner(targets = [], options = {}) | ||
return if PDK.logger.debug? | ||
options = options.merge(PDK::CLI::Util.spinner_opts_for_platform) | ||
|
||
exec_group = options[:exec_group] | ||
@spinner = if exec_group | ||
exec_group.add_spinner(spinner_text(targets), options) | ||
else | ||
TTY::Spinner.new("[:spinner] #{spinner_text(targets)}", options) | ||
end | ||
@spinner.auto_spin | ||
end | ||
|
||
def self.stop_spinner(exit_code) | ||
if exit_code.zero? && @spinner | ||
@spinner.success | ||
elsif @spinner | ||
@spinner.error | ||
end | ||
end | ||
|
||
def self.vendored_task_schema_path | ||
@vendored_task_schema_path ||= File.join(PDK::Util.package_cachedir, 'task.json') | ||
end | ||
|
||
def self.schema_file | ||
schema = if PDK::Util.package_install? && File.exist?(vendored_task_schema_path) | ||
File.read(vendored_task_schema_path) | ||
else | ||
download_schema_from_forge | ||
end | ||
|
||
JSON.parse(schema) | ||
rescue JSON::ParserError | ||
raise PDK::CLI::FatalError, _('Failed to parse Task Metadata Schema file.') | ||
end | ||
|
||
def self.download_schema_from_forge | ||
PDK.logger.debug(_('Task Metadata Schema was not found in the cache. Now downloading from the forge.')) | ||
require 'net/https' | ||
require 'openssl' | ||
|
||
uri = URI.parse(FORGE_SCHEMA_URL) | ||
http = Net::HTTP.new(uri.host, uri.port) | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if Gem.win_platform? | ||
request = Net::HTTP::Get.new(uri.request_uri) | ||
response = http.request(request) | ||
|
||
raise PDK::CLI::FatalError, _('Unable to download Task Metadata Schema file. %{code}: %{message}.') % { code: response.code, message: response.message } unless response.code == '200' | ||
|
||
response.body | ||
rescue StandardError => e | ||
raise PDK::CLI::FatalError, _('Unable to download Task Metadata Schema file. Please check internet connectivity and retry this action. %{error}') % { error: e } | ||
end | ||
|
||
def self.invoke(report, options = {}) | ||
targets, skipped, invalid = parse_targets(options) | ||
|
||
process_skipped(report, skipped) | ||
process_invalid(report, invalid) | ||
|
||
return 0 if targets.empty? | ||
|
||
return_val = 0 | ||
create_spinner(targets, options) | ||
|
||
targets.each do |target| | ||
unless File.readable?(target) | ||
report.add_event( | ||
file: target, | ||
source: name, | ||
state: :failure, | ||
severity: 'error', | ||
message: _('Could not be read.'), | ||
) | ||
return_val = 1 | ||
next | ||
end | ||
|
||
begin | ||
# Need to set the JSON Parser and State Generator to the Native one to be | ||
# compatible with the multi_json adapter. | ||
JSON.parser = JSON::Ext::Parser if defined?(JSON::Ext::Parser) | ||
JSON.generator = JSON::Ext::Generator | ||
|
||
begin | ||
errors = JSON::Validator.fully_validate(schema_file, File.read(target)) || [] | ||
rescue JSON::Schema::SchemaError => e | ||
raise PDK::CLI::FatalError, _('Unable to validate Task Metadata. %{error}.') % { error: e.message } | ||
end | ||
|
||
if errors.empty? | ||
report.add_event( | ||
file: target, | ||
source: name, | ||
state: :passed, | ||
severity: 'ok', | ||
) | ||
else | ||
errors.each do |error| | ||
# strip off the trailing parts that aren't relevant | ||
error = error.split('in schema').first if error.include? 'in schema' | ||
|
||
report.add_event( | ||
file: target, | ||
source: name, | ||
state: :failure, | ||
severity: 'error', | ||
message: error, | ||
) | ||
end | ||
return_val = 1 | ||
end | ||
end | ||
end | ||
|
||
stop_spinner(return_val) | ||
return_val | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a ticket or PR for adding this to the vanagon repo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(and it's runtime dependencies)