Class: DroneHunter

Inherits:
Object
  • Object
show all
Defined in:
lib/drone_hunter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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(**options)
    @log    ||= options.fetch(:log) { Logger.new(STDERR) }
    @github ||= options.fetch(:client) { Octokit::Client.new(auto_paginate: true) }
    @cache  ||= options.fetch(:cache) { Moneta.new(:File, dir: 'drone-hunter.cache') }
    @owners ||= Set.new(options.fetch(:owners, []))
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



28
29
30
# File 'lib/drone_hunter.rb', line 28

def cache
  @cache
end

#githubObject (readonly)

Returns the value of attribute github.



27
28
29
# File 'lib/drone_hunter.rb', line 27

def github
  @github
end

#logObject (readonly)

Returns the value of attribute log.



26
27
28
# File 'lib/drone_hunter.rb', line 26

def log
  @log
end

#ownersObject (readonly)

Returns the value of attribute owners.



29
30
31
# File 'lib/drone_hunter.rb', line 29

def owners
  @owners
end

Instance Method Details

#blobsObject



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

#dronefilesObject



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

#treesObject



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