Class: Fourchette::Fork

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/fourchette/fork.rb

Instance Method Summary collapse

Methods included from Logger

#logger

Constructor Details

#initialize(params) ⇒ Fork

Returns a new instance of Fork.



4
5
6
7
8
# File 'lib/fourchette/fork.rb', line 4

def initialize params
  @params = params
  @heroku = Fourchette::Heroku.new
  @github = Fourchette::GitHub.new
end

Instance Method Details

#branch_nameObject



51
52
53
# File 'lib/fourchette/fork.rb', line 51

def branch_name
  @params['pull_request']['head']['ref']
end

#createObject



36
37
38
39
40
# File 'lib/fourchette/fork.rb', line 36

def create
  @github.comment_pr(pr_number, "Fourchette is initializing a new fork.") if Fourchette::DEBUG
  create_unless_exists
  update
end

#create_unless_existsObject



59
60
61
62
63
64
# File 'lib/fourchette/fork.rb', line 59

def create_unless_exists
  unless @heroku.app_exists?(fork_name)
    @heroku.fork(ENV['FOURCHETTE_HEROKU_APP_TO_FORK'] ,fork_name)
    post_fork_url
  end
end

#deleteObject



42
43
44
45
# File 'lib/fourchette/fork.rb', line 42

def delete
  @heroku.delete(fork_name)
  @github.comment_pr(pr_number, "Test app deleted!")
end

#fork_nameObject



47
48
49
# File 'lib/fourchette/fork.rb', line 47

def fork_name
  "#{ENV['FOURCHETTE_HEROKU_APP_PREFIX']}-PR-#{pr_number}".downcase # It needs to be lowercase only.
end

#monitor_build(build) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/fourchette/fork.rb', line 24

def monitor_build build
  logger.info "Start of the build process on Heroku..."
  build_info = @heroku.client.build.info(fork_name, build['id'])
  # Let's just leave some time to Heroku to download the tarball and start
  # the process. This is some random timing that seems to make sense at first.
  sleep 30
  if build_info['status'] == 'failed'
    @github.comment_pr(pr_number, "The build failed on Heroku. See the activity tab on Heroku.")
    fail Fourchette::DeployException
  end
end

#pr_numberObject



55
56
57
# File 'lib/fourchette/fork.rb', line 55

def pr_number
  @params['pull_request']['number']
end

#updateObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/fourchette/fork.rb', line 10

def update
  create_unless_exists
  tarball = Fourchette::Tarball.new
  tarball_url = tarball.url(github_git_url, git_branch_name, ENV['FOURCHETTE_GITHUB_PROJECT'])
  options = {
    source_blob: {
        url: tarball_url
      }
  }

  build = @heroku.client.build.create(fork_name, options)
  monitor_build(build)
end