Class: DroneHunter
- Inherits:
-
Object
- Object
- DroneHunter
- Defined in:
- lib/drone_hunter.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#github ⇒ Object
readonly
Returns the value of attribute github.
-
#log ⇒ Object
readonly
Returns the value of attribute log.
-
#owners ⇒ Object
readonly
Returns the value of attribute owners.
Instance Method Summary collapse
- #blobs ⇒ Object
- #branches(repo = nil) ⇒ Object
- #cached(key, *rest, &block) ⇒ Object
- #dronefiles ⇒ Object
-
#initialize(**options) ⇒ DroneHunter
constructor
A new instance of DroneHunter.
- #repositories(owner = nil) ⇒ Object
- #trees ⇒ Object
Constructor Details
#initialize(**options) ⇒ DroneHunter
Returns a new instance of DroneHunter.
19 20 21 22 23 24 |
# File 'lib/drone_hunter.rb', line 19 def initialize(**) @log ||= .fetch(:log) { Logger.new(STDERR) } @github ||= .fetch(:client) { Octokit::Client.new(auto_paginate: true) } @cache ||= .fetch(:cache) { Moneta.new(:File, dir: 'drone-hunter.cache') } @owners ||= Set.new(.fetch(:owners, [])) end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
28 29 30 |
# File 'lib/drone_hunter.rb', line 28 def cache @cache end |
#github ⇒ Object (readonly)
Returns the value of attribute github.
27 28 29 |
# File 'lib/drone_hunter.rb', line 27 def github @github end |
#log ⇒ Object (readonly)
Returns the value of attribute log.
26 27 28 |
# File 'lib/drone_hunter.rb', line 26 def log @log end |
#owners ⇒ Object (readonly)
Returns the value of attribute owners.
29 30 31 |
# File 'lib/drone_hunter.rb', line 29 def owners @owners end |
Instance Method Details
#blobs ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/drone_hunter.rb', line 73 def blobs trees.map do |repo, tree| blobs = tree .select { |entry| entry.path.match?(/ya?ml$/) } .select { |entry| entry.path.match?(/drone/) } .map do |entry| { entry.path => cached("blob/#{repo}/#{entry.sha}") { github.blob(repo, entry.sha) } } end next if blobs.empty? { repo => blobs.reduce(&:merge) } end.compact.reduce(&:merge) end |
#branches(repo = nil) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/drone_hunter.rb', line 49 def branches(repo = nil) case repo when String then cached("branches/#{repo}") { github.branches(repo) } when Sawyer::Resource then branches(repo.full_name) when nil then repositories.flat_map { |repo| branches(repo) } else raise TypeError end end |
#cached(key, *rest, &block) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/drone_hunter.rb', line 31 def cached(key, *rest, &block) if cache.key?(key) log.debug("(cache) #{key}") cache.fetch(key) else log.info("(fetch) #{key}") cache.store(key, block.call(*rest)) end end |
#dronefiles ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/drone_hunter.rb', line 91 def dronefiles blobs.flat_map do |repo, blobs| blobs.map do |path, blob| case blob[:encoding] when "base64" { "repository" => repo, "path" => path, "sha" => blob[:sha], "content" => Base64::decode64(blob[:content]) } else raise EncodingError end end end end |
#repositories(owner = nil) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/drone_hunter.rb', line 41 def repositories(owner = nil) case owner when String then cached("repositories/#{owner}") { github.repositories(owner) } when nil then owners.flat_map { |owner| repositories(owner) } else raise TypeError end end |
#trees ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/drone_hunter.rb', line 58 def trees repositories.map do |repo| tree_sha = branches(repo.full_name).find do |branch| branch.name == repo.default_branch end&.commit&.sha next unless tree_sha repo = repo.full_name { repo => cached("tree/#{repo}/#{tree_sha}") { github.tree(repo, tree_sha) }.tree } end.compact.reduce(&:merge) end |