class Fourchette::Fork

Public Class Methods

new(params) click to toggle source
# File lib/fourchette/fork.rb, line 4
def initialize(params)
  @params = params
  @heroku = Fourchette::Heroku.new
  @github = Fourchette::GitHub.new
end

Public Instance Methods

branch_name() click to toggle source
# File lib/fourchette/fork.rb, line 48
def branch_name
  @params['pull_request']['head']['ref']
end
create() click to toggle source
# File lib/fourchette/fork.rb, line 31
def create
  @github.comment_pr(
    pr_number, 'Fourchette is initializing a new fork.') if Fourchette::DEBUG
  create_unless_exists
  update
end
create_unless_exists() click to toggle source
# File lib/fourchette/fork.rb, line 56
def create_unless_exists
  unless app_exists?
    @heroku.fork(ENV['FOURCHETTE_HEROKU_APP_TO_FORK'], fork_name)
    post_fork_url
  end
end
delete() click to toggle source
# File lib/fourchette/fork.rb, line 38
def delete
  @heroku.delete(fork_name)
  @github.comment_pr(pr_number, 'Test app deleted!')
end
fork_name() click to toggle source
# File lib/fourchette/fork.rb, line 43
def fork_name
  # It needs to be lowercase only.
  "#{ENV['FOURCHETTE_HEROKU_APP_PREFIX']}-PR-#{pr_number}".downcase
end
monitor_build(build) click to toggle source
# File lib/fourchette/fork.rb, line 17
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_number() click to toggle source
# File lib/fourchette/fork.rb, line 52
def pr_number
  @params['pull_request']['number']
end
update() click to toggle source
# File lib/fourchette/fork.rb, line 10
def update
  create_unless_exists

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

Private Instance Methods

app_exists?() click to toggle source
# File lib/fourchette/fork.rb, line 65
def app_exists?
  @heroku.app_exists?(fork_name)
end
git_branch_name() click to toggle source
# File lib/fourchette/fork.rb, line 95
def git_branch_name
  "remotes/origin/#{branch_name}"
end
github_git_url() click to toggle source
# File lib/fourchette/fork.rb, line 99
def github_git_url
  @params['pull_request']['head']['repo']['clone_url']
    .gsub(
      '//github.com',
      "//#{ENV['FOURCHETTE_GITHUB_USERNAME']}:" \
      "#{ENV['FOURCHETTE_GITHUB_PERSONAL_TOKEN']}@github.com"
    )
end
post_fork_url() click to toggle source

Update PR with URL. This is a method so that we can override it and just not have that, if we don't want. Use case: we have custom domains, so we post the URLs later on.

# File lib/fourchette/fork.rb, line 88
def post_fork_url
  @github.comment_pr(
    pr_number,
    "Test URL: #{@heroku.client.app.info(fork_name)['web_url']}"
  )
end
tarball_options() click to toggle source
# File lib/fourchette/fork.rb, line 69
def tarball_options
  {
    source_blob: {
      url: tarball_url
    }
  }
end
tarball_url() click to toggle source
# File lib/fourchette/fork.rb, line 77
def tarball_url
  Fourchette::Tarball.new.url(
    github_git_url,
    git_branch_name,
    ENV['FOURCHETTE_GITHUB_PROJECT']
  )
end