mirror of
https://github.com/michenriksen/gitrob.git
synced 2026-05-02 14:29:37 -05:00
Add ability to define custom signatures
Gitrob will now look for a file at `~/.gitrobsignatures` which it expects to be a JSON document that follows the same structure as the main signatures.json file. If the signatures are valid, it will be loaded into the BlobObserver.
This commit is contained in:
+38
-10
@@ -2,6 +2,8 @@ module Gitrob
|
||||
class BlobObserver
|
||||
SIGNATURES_FILE_PATH = File.expand_path(
|
||||
"../../../signatures.json", __FILE__)
|
||||
CUSTOM_SIGNATURES_FILE_PATH = File.join(
|
||||
Dir.home, ".gitrobsignatures")
|
||||
|
||||
REQUIRED_SIGNATURE_KEYS = %w(part type pattern caption description)
|
||||
ALLOWED_TYPES = %w(regex match)
|
||||
@@ -30,23 +32,49 @@ module Gitrob
|
||||
|
||||
def self.load_signatures!
|
||||
@signatures = []
|
||||
JSON.load(File.read(SIGNATURES_FILE_PATH)).each do |signature|
|
||||
signatures = JSON.load(File.read(SIGNATURES_FILE_PATH))
|
||||
validate_signatures!(signatures)
|
||||
signatures.each_with_index do |signature|
|
||||
@signatures << Signature.new(signature)
|
||||
end
|
||||
validate_signatures!
|
||||
rescue CorruptSignaturesError => e
|
||||
raise e
|
||||
rescue
|
||||
raise CorruptSignaturesError, "Could not parse signature file"
|
||||
end
|
||||
|
||||
def self.validate_signatures!
|
||||
def self.unload_signatures
|
||||
@signatures = []
|
||||
end
|
||||
|
||||
def self.custom_signatures?
|
||||
File.exist?(CUSTOM_SIGNATURES_FILE_PATH)
|
||||
end
|
||||
|
||||
def self.load_custom_signatures!
|
||||
signatures = JSON.load(File.read(CUSTOM_SIGNATURES_FILE_PATH))
|
||||
validate_signatures!(signatures)
|
||||
signatures.each do |signature|
|
||||
@signatures << Signature.new(signature)
|
||||
end
|
||||
rescue CorruptSignaturesError => e
|
||||
raise e
|
||||
rescue
|
||||
raise CorruptSignaturesError, "Could not parse signature file"
|
||||
end
|
||||
|
||||
def self.validate_signatures!(signatures)
|
||||
if !signatures.is_a?(Array) || signatures.empty?
|
||||
fail CorruptSignaturesError,
|
||||
"Signature file contains no signatures"
|
||||
end
|
||||
signatures.each do |signature|
|
||||
validate_signature!(signature)
|
||||
signatures.each_with_index do |signature, index|
|
||||
begin
|
||||
validate_signature!(signature)
|
||||
rescue CorruptSignaturesError => e
|
||||
raise CorruptSignaturesError,
|
||||
"Validation failed for Signature ##{index + 1}: #{e.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -58,7 +86,7 @@ module Gitrob
|
||||
|
||||
def self.validate_signature_keys!(signature)
|
||||
REQUIRED_SIGNATURE_KEYS.each do |key|
|
||||
unless signature.respond_to?(key)
|
||||
unless signature.key?(key)
|
||||
fail CorruptSignaturesError,
|
||||
"Missing required signature key: #{key}"
|
||||
end
|
||||
@@ -66,16 +94,16 @@ module Gitrob
|
||||
end
|
||||
|
||||
def self.validate_signature_type!(signature)
|
||||
unless ALLOWED_TYPES.include?(signature.type)
|
||||
unless ALLOWED_TYPES.include?(signature["type"])
|
||||
fail CorruptSignaturesError,
|
||||
"Invalid signature type: #{signature.type}"
|
||||
"Invalid signature type: #{signature['type']}"
|
||||
end
|
||||
end
|
||||
|
||||
def self.validate_signature_part!(signature)
|
||||
unless ALLOWED_PARTS.include?(signature.part)
|
||||
unless ALLOWED_PARTS.include?(signature["part"])
|
||||
fail CorruptSignaturesError,
|
||||
"Invalid signature part: #{signature.part}"
|
||||
"Invalid signature part: #{signature['part']}"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -40,6 +40,15 @@ module Gitrob
|
||||
task("Loading signatures...", true) do
|
||||
Gitrob::BlobObserver.load_signatures!
|
||||
end
|
||||
|
||||
if Gitrob::BlobObserver.custom_signatures?
|
||||
task("Loading custom signatures...", true) do
|
||||
Gitrob::BlobObserver.load_custom_signatures!
|
||||
end
|
||||
info("Please consider contributing your custom signatures to the " \
|
||||
"Gitrob project.")
|
||||
end
|
||||
info("Loaded #{Gitrob::BlobObserver.signatures.count} signatures")
|
||||
end
|
||||
|
||||
def start_web_server
|
||||
|
||||
Reference in New Issue
Block a user