summaryrefslogtreecommitdiff
path: root/lib/e2e/project.rb
blob: 10c68ebdc47ce6a6253f234ba6147e1e8eed7f7d (plain)
1
2
3
4
5
6
7
8
9
10
11
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
50
51
# frozen_string_literal: true
require 'securerandom'

class Project
  attr_reader :path

  def initialize(path = Pathname.pwd.join('tmp').join(SecureRandom.uuid))
    FileUtils.mkdir_p(path)
    @path = Pathname(path)
  end

  def report_for(type:)
    case type&.to_sym
    when :dependency_scanning
      DependencyScanningReport.new(project_path: path)
    else
      JSON.parse(path.join("gl-#{type}-report.json").read)
    end
  end

  def mount(dir:)
    FileUtils.cp_r("#{dir}/.", path)
  end

  def chdir
    Dir.chdir path do
      yield
    end
  end

  def clone(repo, branch: 'master')
    if branch.match?(/\b[0-9a-f]{5,40}\b/)
      execute({}, 'git', 'clone', repo, path.to_s)
      chdir do
        execute({}, 'git', 'checkout', branch)
      end
    else
      execute({}, 'git', 'clone', '--depth=1', '--single-branch', '--branch', branch, repo, path.to_s)
    end
  end

  def execute(env = {}, *args)
    Bundler.with_unbundled_env do
      system(env, *args, exception: true)
    end
  end

  def cleanup
    FileUtils.rm_rf(path) if path.exist?
  end
end