class TravisCI

Functions to run on Travis

Public Class Methods

new(params = {}) click to toggle source
# File lib/travis/travis.rb, line 9
def initialize(params = {})
  Log.fatal('Cannot run commands against Travis when not running on a Travis server') unless TravisCI.running_on_travis?

  @private = params.fetch(:private_repo, false)
  @travis_token = params.fetch(:api_key)

  authenticate
end
pull_request?() click to toggle source

If CI job running on a pull request

# File lib/travis/travis.rb, line 24
def self.pull_request?
  ENV['TRAVIS_PULL_REQUEST'].to_s == 'true'
end
running_on_travis?() click to toggle source

If currently running on a Travis CI machine

# File lib/travis/travis.rb, line 19
def self.running_on_travis?
  ENV['HAS_JOSH_K_SEAL_OF_APPROVAL']
end

Public Instance Methods

branch_build_successful?() click to toggle source

If the current job is a pull request, this will determine if the previous build for the current branch was successful or not. Note: An exception will be thrown if not in a pull request.

# File lib/travis/travis.rb, line 30
def branch_build_successful?
  Log.fatal('Cannot determine if branch build was successful if not running on a pull request') unless TravisCI.pull_request?

  travis_repo = repo
  original_branch = ENV['TRAVIS_PULL_REQUEST_BRANCH']

  travis_repo.branch(original_branch).green?
end
fail_if_pr_branch_build_failed() click to toggle source

If the previous build on current branch failed on Travis, skip this build too.

# File lib/travis/travis.rb, line 49
def fail_if_pr_branch_build_failed
  return unless TravisCI.pull_request?

  Log.warning('Checking if previous build for this branch was successful on Travis...')

  Log.fatal('Skipping running command because previous build for branch failed.') unless branch_build_successful?
end
repo() click to toggle source

Get repository from Travis

# File lib/travis/travis.rb, line 40
def repo
  if @private
    Travis::Pro::Repository.find(ENV['TRAVIS_REPO_SLUG'])
  else
    Travis::Repository.find(ENV['TRAVIS_REPO_SLUG'])
  end
end

Private Instance Methods

authenticate() click to toggle source
# File lib/travis/travis.rb, line 59
def authenticate
  if @private
    Travis::Pro.access_token = @travis_token
  else
    Travis.access_token = @travis_token
  end
end