Class: CloneClient

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

Constant Summary collapse

REPO_FORMAT =
%r{\A[A-Za-z0-9\-_.]+/[A-Za-z0-9\-_.]+\z}

Instance Method Summary collapse

Constructor Details

#initializeCloneClient

Returns a new instance of CloneClient.



8
9
10
# File 'lib/clone_client.rb', line 8

def initialize
    @tmpdir = nil
end

Instance Method Details

#cleanupObject



61
62
63
# File 'lib/clone_client.rb', line 61

def cleanup
    FileUtils.rm_rf(@tmpdir) if @tmpdir
end

#fetch_dependabot_config(repo) ⇒ Object



51
52
53
54
# File 'lib/clone_client.rb', line 51

def fetch_dependabot_config(repo)
    return nil unless @tmpdir
    LocalClient.new(@tmpdir).fetch_dependabot_config(repo)
end

#fetch_workflows(repo) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/clone_client.rb', line 12

def fetch_workflows(repo)
    unless repo.match?(REPO_FORMAT)
        $stderr.puts "Invalid repo format: #{repo} (expected owner/repo)"
        return []
    end

    @tmpdir = Dir.mktmpdir("sentinel-")

    # Shallow sparse clone — only .github/ directory
    success = system(
        "git", "clone", "--depth", "1", "--filter=blob:none", "--sparse",
        "https://github.com/#{repo}.git", @tmpdir,
        [:out, :err] => File::NULL
    )

    unless success
        $stderr.puts ""
        $stderr.puts "ERROR: Could not access #{repo}"
        $stderr.puts ""
        $stderr.puts "This repo may be private. To scan private repos:"
        $stderr.puts ""
        $stderr.puts "  export GITHUB_TOKEN=$(gh auth token)"
        $stderr.puts "  sentinel scan #{repo}"
        $stderr.puts ""
        $stderr.puts "Or pass a token directly:"
        $stderr.puts ""
        $stderr.puts "  sentinel scan --token ghp_xxx #{repo}"
        $stderr.puts ""
        exit 2
    end

    system(
        "git", "-C", @tmpdir, "sparse-checkout", "set", ".github",
        [:out, :err] => File::NULL
    )

    LocalClient.new(@tmpdir).fetch_workflows(repo)
end

#file_exists?(repo, path) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/clone_client.rb', line 56

def file_exists?(repo, path)
    return false unless @tmpdir
    File.exist?(File.join(@tmpdir, path))
end