Class: TravisCI

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/travis.rb

Overview

Functions to run on Travis

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ TravisCI

Returns a new instance of TravisCI.



9
10
11
12
13
14
15
16
# 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

Class Method Details

.pull_request?Boolean

If CI job running on a pull request

Returns:

  • (Boolean)


24
25
26
# File 'lib/travis/travis.rb', line 24

def self.pull_request?
  ENV['TRAVIS_PULL_REQUEST'].to_s == 'true'
end

.running_on_travis?Boolean

If currently running on a Travis CI machine

Returns:

  • (Boolean)


19
20
21
# File 'lib/travis/travis.rb', line 19

def self.running_on_travis?
  ENV['HAS_JOSH_K_SEAL_OF_APPROVAL']
end

Instance Method Details

#branch_build_successful?Boolean

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.

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
# 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_failedObject

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



49
50
51
52
53
54
55
# 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

#repoObject

Get repository from Travis



40
41
42
43
44
45
46
# 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