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.



8
9
10
11
# File 'lib/dryrun/github.rb', line 8

def initialize(url)
  @base_url = sanitize_url(url)
  @destination = destination
end

Instance Method Details

#clone(branch, tag, cleanup) ⇒ Object

CLONE THE REPOSITORY



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
86
87
88
89
90
91
92
93
94
95
# File 'lib/dryrun/github.rb', line 55

def clone(branch, tag, cleanup)
  cloneable = cloneable_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 #{cloneable} #{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 #{cloneable} #{tmpdir}")
  end

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

  tmpdir
end

#cloneable_urlObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dryrun/github.rb', line 39

def cloneable_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

#destinationObject



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

def destination
  unless @base_url.include? 'github.com'
    return Digest::SHA256.hexdigest @base_url
  end

  stripped_url = @base_url.gsub('.git', '')
  stripped_url = stripped_url.gsub('.git', '')
  stripped_url = stripped_url.gsub('[email protected]:', '')
  stripped_url = stripped_url.gsub('https://github.com/', '')
  stripped_url.gsub('http://github.com/', '')
end

#sanitize_url(url) ⇒ Object



13
14
15
16
17
# File 'lib/dryrun/github.rb', line 13

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

#valid?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/dryrun/github.rb', line 31

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