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 Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCloneClient

Returns a new instance of CloneClient.



10
11
12
# File 'lib/clone_client.rb', line 10

def initialize
    @tmpdir = nil
end

Instance Attribute Details

#tmpdirObject (readonly)

Returns the value of attribute tmpdir.



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

def tmpdir
  @tmpdir
end

Instance Method Details

#cleanupObject



63
64
65
# File 'lib/clone_client.rb', line 63

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

#fetch_dependabot_config(repo) ⇒ Object



53
54
55
56
# File 'lib/clone_client.rb', line 53

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

#fetch_workflows(repo) ⇒ Object



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
50
51
# File 'lib/clone_client.rb', line 14

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)


58
59
60
61
# File 'lib/clone_client.rb', line 58

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