class RogerSneakpeek::Finalizer

Finalizer to zip and upload release

Public Instance Methods

default_options() click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 17
def default_options
  {
    zip: "zip",
    project: nil,
    gitlab_project: nil,
    ci_only: true,
    sneakpeek_api_url: "http://api.peek.digitpaint.nl"
  }
end
perform() click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 27
def perform
  unless @options[:project]
    fail ArgumentError, "You must specify a project to RogerSneakpeek"
  end

  unless @options[:gitlab_project]
    fail ArgumentError, "You must specify a gitlab_project to RogerSneakpeek"
  end

  # If we run in ci_only mode and are not in CI we stop.
  return if @options[:ci_only] && !CI.ci?

  check_zip_command

  @release.log(self, "Starting upload to Sneakpeek")
  upload_release zip_release
end

Protected Instance Methods

check_zip_command() click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 51
def check_zip_command
  `#{zip_command} -v`
rescue Errno::ENOENT
  raise "Could not find zip in #{zip_command.inspect}"
end
git(*args) click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 57
def git(*args)
  cmd = Shellwords.join([@options[:git]] + args)
  `#{cmd}`
end
handle_result(result) click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 129
def handle_result(result)
  case result.status
  when 201
    JSON.parse(result.body)
  when 422
    fail "Upload to Sneakpeek failed with error: #{response.body[:error]}"
  else
    fail "Upload to Sneakpeek failed with unknown error (status: #{result.status})"
  end
end
perform_upload(url, zip_path, params) click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 110
def perform_upload(url, zip_path, params)
  conn = Faraday.new(@options[:sneakpeek_api_url]) do |f|
    f.request :multipart
    f.request :url_encoded
    f.adapter :net_http
  end

  data = params.dup
  data[:file] = Faraday::UploadIO.new(zip_path, "application/zip")

  result = conn.post do |req|
    req.url url
    req.body = data
    req.options.timeout = 300
  end

  handle_result(result)
end
sneakpeek_url(git) click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 98
def sneakpeek_url(git)
  project = @options[:project]
  case
  when git.tag
    "/projects/#{project}/tags/#{URI.escape(git.tag)}"
  when git.branch
    "/projects/#{project}/branches/#{URI.escape(git.branch)}"
  else
    fail "Current project is neither on a tag nor a branch"
  end
end
tmpname() click to toggle source

Shameless rip-off of github.com/rails/rails/pull/31462/files

# File lib/roger_sneakpeek/finalizer.rb, line 74
def tmpname
  t = Time.now.strftime("%Y%m%d")
  "release-zip-#{t}-#{$PROCESS_ID}-#{rand(0x100000000).to_s(36)}.zip"
end
upload_release(zip_path) click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 79
def upload_release(zip_path)
  git = if CI.ci?
          CI.new
        else
          Git.new(path: @release.project.path)
        end

  data = perform_upload(
    sneakpeek_url(git),
    zip_path,
    sha: git.sha,
    gitlab_project: @options[:gitlab_project]
  )

  @release.log(self, "Sneakpeek url: #{data['url']}") if data
ensure
  File.unlink zip_path
end
zip_command(*args) click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 47
def zip_command(*args)
  ([Shellwords.escape(@options[:zip])] + args).join(" ")
end
zip_release() click to toggle source
# File lib/roger_sneakpeek/finalizer.rb, line 62
def zip_release
  zip_path = File.join(Dir.tmpdir, tmpname)
  ::Dir.chdir(@release.build_path) do
    command = zip_command("-r", "-9", Shellwords.escape(zip_path), "./*")
    output = `#{command}`
    fail "Could not generate zipfile\n#{output}" if $CHILD_STATUS.to_i != 0
  end

  zip_path
end