trent 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8bf0702ce2c0fff153f326fd2f975aed86e42d6fd2fc3ea1bc09f9264f40fca3
4
- data.tar.gz: 0b43ac26659e0032f9def1b7d6090acf8587357e3b2074136d5bb72aa0e2acab
3
+ metadata.gz: 0c24028242dbee5242968caf0debba171505838ed9ea679f67c1a45080bba48d
4
+ data.tar.gz: 4e08149c17144b39350bfce96733a7efe0f53d77b66383700bf81a743a7b390d
5
5
  SHA512:
6
- metadata.gz: 58437bb5955c6b6f89b546aec964f423696d7a177ba88bd644611cd84a5c22aee0820d218432e4cd206e709e79b8afc1a01769a8e0e6cd2dadfe98a8662d18f3
7
- data.tar.gz: '097c6e79bfe8c9fdcffcc06b35aff26e9db37167915462886c5ee4e72bf5186047e6ac9bb5afd8db0e1320e201152e4413eb16b70be5747056582158fdf74225'
6
+ metadata.gz: b7b9184aae962c908747e33a35482d8c7f470c52dd5fd9ea9f249fc0f0d6fca2edf4e863d670fa9e116691f56d94d4dd33a0513671e3bd730ca26c712318798c
7
+ data.tar.gz: dab47c51d9b65bc25753d91b2ad6507e893809f50a7e16e4844f9a22c651798a5387425cea9c8139cf9a9268c0933c4ee615dd3f2e28ecee792cbe6322956843
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ### [0.4.0] 2019-03-07
2
+
3
+ ### Added
4
+ - Add small set of utility functions for Travis-CI. Specifically determining if the previous branch build for a pull request was successful or not. [Discussion](https://github.com/levibostian/Trent/issues/22)
5
+
1
6
  ### [0.3.0] 2018-11-27
2
7
 
3
8
  ### Added
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  # Trent
6
6
 
7
- Run and debug bash commands on Travis-CI much easier.
7
+ Run and debug bash commands on Travis-CI much easier. Make writing commands to run on your CI not painful.
8
8
 
9
9
  ...Yes, Trent is open to working with more then Travis-CI. At this time, Travis is what I use so it's what's supported. Make a pull request to add more.
10
10
 
@@ -148,6 +148,34 @@ ci.config_github('wfoweifwoeifjweoijwefowefweoif')
148
148
  ci.github.comment("Tests pass!") # Send comment to pull request for the Travis build. The pull request information is automatically retrieved from the Travis virtual machine the build is running on.
149
149
  ```
150
150
 
151
+ # Travis
152
+
153
+ Trent comes with some handy commands that allow you to work with Travis, itself, easily. Here are some things you can do with it:
154
+
155
+ ### Skip rest of Trent commands if previous branch build failed and on a pull request
156
+
157
+ When I am working on a project and I get it done, I tend to push up my branch to GitHub and then make a pull request right away to indicate it is complete.
158
+
159
+ The problem is, if my branch build fails, the pull request build on Travis will still happen (and most likely fail if configured similar to my push commands). I would rather have Travis skip running some commands on a pull request build if the previous branch build failed.
160
+
161
+ ```ruby
162
+ require 'trent'
163
+
164
+ ci = Trent.new()
165
+ # Set Travis CI API key to authenticate user account.
166
+ # Log into travis-ci.com or travis-ci.org (depending on where your repo is located), and browse to your profile page: https://travis-ci.com/account/preferences Copy the API key shown.
167
+ # I like to store this API key in an environment variable.
168
+ # Also indicate if this is a private repo or not.
169
+ ci.config_travis(:api_key => ENV["TRAVIS_API_KEY"], :private_repo => true)
170
+
171
+ if TravisCI.pull_request? && ci.travis.branch_build_successful?
172
+ # Do something if we are in a pull request and the most recent branch build was successful.
173
+ end
174
+
175
+ # Or, fail is Trent figures out we are in a pull request and the most recent branch build was not successful.
176
+ ci.travis.fail_if_pr_branch_build_failed()
177
+ ```
178
+
151
179
  # Docs
152
180
 
153
181
  Trent documentation is hosted [here](https://www.rubydoc.info/gems/trent/0.1.0).
@@ -170,6 +198,8 @@ ci = Trent.new(:color => :green)
170
198
 
171
199
  Trent is open for pull requests. Check out the [list of issues](https://github.com/levibostian/trent/issues) for tasks I am planning on working on. Check them out if you wish to contribute in that way.
172
200
 
201
+ At this time, Trent is a random collection of random tasks that I have found a use case for while working with CI servers. I see Trent being more of a core system that can run a series of plugins for you. I want to think of it as "The [Danger](https://danger.systems/) of creating scripts for CI servers" (aka: a convenience tool to help you write scripts quickly and painless with less bugs).
202
+
173
203
  **Want to add features to Trent?** Before you decide to take a bunch of time and add functionality to the library, please, [create an issue](https://github.com/levibostian/trent/issues/new) stating what you wish to add. This might save you some time in case your purpose does not fit well in the use cases of Trent.
174
204
 
175
205
  ### Building Trent
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'travis'
4
+ require 'travis/pro' # For private repos
5
+ require_relative '../log'
6
+
7
+ # Functions to run on Travis
8
+ class TravisCI
9
+ def initialize(params = {})
10
+ Log.fatal('Cannot run commands against Travis when not running on a Travis server') unless TravisCI.running_on_travis?
11
+
12
+ @private = params.fetch(:private_repo, false)
13
+ @travis_token = params.fetch(:api_key)
14
+
15
+ authenticate
16
+ end
17
+
18
+ # If currently running on a Travis CI machine
19
+ def self.running_on_travis?
20
+ ENV['HAS_JOSH_K_SEAL_OF_APPROVAL']
21
+ end
22
+
23
+ # If CI job running on a pull request
24
+ def self.pull_request?
25
+ ENV['TRAVIS_PULL_REQUEST'].to_s == 'true'
26
+ end
27
+
28
+ # If the current job is a pull request, this will determine if the previous build for the current branch was successful or not.
29
+ # Note: An exception will be thrown if not in a pull request.
30
+ def branch_build_successful?
31
+ Log.fatal('Cannot determine if branch build was successful if not running on a pull request') unless TravisCI.pull_request?
32
+
33
+ travis_repo = repo
34
+ original_branch = ENV['TRAVIS_PULL_REQUEST_BRANCH']
35
+
36
+ travis_repo.branch(original_branch).green?
37
+ end
38
+
39
+ # Get repository from Travis
40
+ def repo
41
+ if @private
42
+ Travis::Pro::Repository.find(ENV['TRAVIS_REPO_SLUG'])
43
+ else
44
+ Travis::Repository.find(ENV['TRAVIS_REPO_SLUG'])
45
+ end
46
+ end
47
+
48
+ # If the previous build on current branch failed on Travis, skip this build too.
49
+ def fail_if_pr_branch_build_failed
50
+ return unless TravisCI.pull_request?
51
+
52
+ Log.warning('Checking if previous build for this branch was successful on Travis...')
53
+
54
+ Log.fatal('Skipping running command because previous build for branch failed.') unless branch_build_successful?
55
+ end
56
+
57
+ private
58
+
59
+ def authenticate
60
+ if @private
61
+ Travis::Pro.access_token = @travis_token
62
+ else
63
+ Travis.access_token = @travis_token
64
+ end
65
+ end
66
+ end
data/lib/trent.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'command/ssh/ssh'
4
4
  require 'github/github'
5
+ require 'travis/travis'
5
6
  require 'command/sh/sh'
6
7
  require 'colorize'
7
8
  require_relative './log'
@@ -14,7 +15,7 @@ class Trent
14
15
  # local - Run Trent locally on your own machine instead of a CI server.
15
16
  def initialize(params = {})
16
17
  running_local = params.fetch(:local, false)
17
- Log.fatal('Trent is designed to run on Travis-CI builds. Run it on Travis-CI.') unless ENV['HAS_JOSH_K_SEAL_OF_APPROVAL'] || running_local
18
+ Log.fatal('Trent is designed to run on Travis-CI builds. Run it on Travis-CI.') unless TravisCI.running_on_travis? || running_local
18
19
 
19
20
  @color = params.fetch(:color, :blue)
20
21
  @sh = Sh.new
@@ -32,6 +33,11 @@ class Trent
32
33
  @github = GitHub.new(api_key)
33
34
  end
34
35
 
36
+ # Configure how to communicate with Travis
37
+ def config_travis(api_key, private_repo)
38
+ @travis = TravisCI.new(api_key: api_key, private_repo: private_repo)
39
+ end
40
+
35
41
  # While working with bash commands, some commands are not added to the path. That's annoying.
36
42
  # Convenient method to assign a command to a path for replacing.
37
43
  # Example:
@@ -65,6 +71,12 @@ class Trent
65
71
  result
66
72
  end
67
73
 
74
+ # Get instance of GitHub class to run commands against GitHub
75
+ def travis
76
+ Log.fatal('You did not configure Travis yet.') unless @travis
77
+ @travis
78
+ end
79
+
68
80
  # Get instance of GitHub class to run commands against GitHub
69
81
  def github
70
82
  Log.fatal('You did not configure GitHub yet.') unless @github
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trent
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Levi Bostian
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-11-27 00:00:00.000000000 Z
11
+ date: 2019-03-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -50,6 +50,26 @@ dependencies:
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
52
  version: 5.0.2
53
+ - !ruby/object:Gem::Dependency
54
+ name: travis
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '1.8'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.8.2
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '1.8'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.8.2
53
73
  - !ruby/object:Gem::Dependency
54
74
  name: rubocop
55
75
  requirement: !ruby/object:Gem::Requirement
@@ -149,6 +169,7 @@ files:
149
169
  - lib/command/ssh/ssh.rb
150
170
  - lib/github/github.rb
151
171
  - lib/log.rb
172
+ - lib/travis/travis.rb
152
173
  - lib/trent.rb
153
174
  homepage: http://github.com/levibostian/trent
154
175
  licenses: