Class: Dryrun::Github

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

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Github

Returns a new instance of Github.



9
10
11
12
13
14
15
# File 'lib/dryrun/github.rb', line 9

def initialize(url)
  url = url.split('?').first
  url.chop! if url.end_with? '/'

  @base_url = url
  @destination = destination
end

Instance Method Details

#clonable_urlObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dryrun/github.rb', line 29

def clonable_url
  starts_with_git = @base_url.split(//).first(4).join.eql? 'git@'
  ends_with_git = @base_url.split(//).last(4).join.eql? '.git'

  # ends with git but doesnt start with git
  return @base_url if ends_with_git && !starts_with_git

  # ends with git but doesnt start with git
  return "#{@base_url}.git" if !ends_with_git && !starts_with_git

  @base_url
end

#clone(branch, tag, cleanup) ⇒ Object

CLONE THE REPOSITORY



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dryrun/github.rb', line 45

def clone(branch, tag, cleanup)
  clonable = clonable_url

  tmpdir = Dir.tmpdir + "/dryrun/#{@destination}"

  if cleanup
    puts 'Wiping the folder: ' + tmpdir.green
    FileUtils.rm_rf tmpdir
    # FileUtils.mkdir_p tmpdir
  end

  folder_exists = File.directory?(tmpdir)

  if folder_exists
    Dir.chdir tmpdir

    is_git_repo = system('git rev-parse')

    if !is_git_repo
      FileUtils.rm_rf(tmpdir)
      DryrunUtils.execute("git clone --depth 1 #{clonable} #{tmpdir}")
      DryrunUtils.execute("git checkout #{branch}")
    else
      puts "Found project in #{tmpdir.green}..."
      DryrunUtils.execute('git reset --hard HEAD')
      DryrunUtils.execute('git fetch --all')
      DryrunUtils.execute("git checkout #{branch}")
      DryrunUtils.execute("git pull origin #{branch}")
    end
  else
    DryrunUtils.execute("git clone --depth 1 #{clonable} #{tmpdir}")
  end

  if tag
    Dir.chdir tmpdir
    DryrunUtils.execute('git fetch --depth=10000')
    DryrunUtils.execute("git checkout #{tag}")
  end

  tmpdir
end

#destinationObject



17
18
19
# File 'lib/dryrun/github.rb', line 17

def destination
  Digest::SHA256.hexdigest @base_url
end

#valid?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
# File 'lib/dryrun/github.rb', line 21

def valid?
  starts_with_git = @base_url.split(//).first(4).join.eql? 'git@'
  starts_with_http = @base_url.split(//).first(7).join.eql? 'http://'
  starts_with_https = @base_url.split(//).first(8).join.eql? 'https://'

  (starts_with_git || starts_with_https || starts_with_http)
end