buildkite_watcher 0.1.0 → 0.3.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: ce94c9f805c452c1c2d347d496c6035f2c46978c4380ece167df5d4fc8e185bf
4
- data.tar.gz: 37134a0e829761200e9c864416f023325ef3d716cadc929f7c5c1c9817786f8c
3
+ metadata.gz: d3a24a5b71c0b483775becf3121225b092adfea7af00912325854bb9ffce00ca
4
+ data.tar.gz: e190ca0fefacc2760abaae640650761c68af784a7618f3055e43783bc04db9cf
5
5
  SHA512:
6
- metadata.gz: 84da1398fdb493ff787030ce14e9499a58dda0b8b5c6c6e55310b76a389204c7345cd02febd7824bab80f730cf46e5bfa2561db94580f58c450353e40f648d55
7
- data.tar.gz: fe35e7b28d99cf4bb0e44667cf4f35b96381b5aa499dc6aabd8e7c9725ddb8a963f931006c4742f8e30be5e94b2163094c13ec30c138727a2e172ad93cf82d3c
6
+ metadata.gz: b0227a1e7d51c320d4d4b9439addb6304212bb9175e0158b5195c74dd22663c74a012e3fdea16cb5882fd2ccdc02d92119c2b84b166f5abb9b32c3e90aad6a80
7
+ data.tar.gz: a6af957c33a368227766afbe5bcdf4ef392facaf1e84ccc348580fc3d3fc877fd13cfa34331cb8243a7cfe0226c40419652dabf2746bbc54f0e99938d97053b3
@@ -1,16 +1,22 @@
1
- name: Ruby
1
+ name: CI
2
2
 
3
3
  on: [push,pull_request]
4
4
 
5
5
  jobs:
6
- build:
7
- runs-on: ubuntu-latest
6
+ buildtest:
7
+ strategy:
8
+ fail-fast: false
9
+ matrix:
10
+ os: [ubuntu-latest, macos-latest]
11
+ # Due to https://github.com/actions/runner/issues/849, we have to use quotes for '3.0'
12
+ ruby: [2.7, '3.0']
13
+ runs-on: ${{ matrix.os }}
8
14
  steps:
9
15
  - uses: actions/checkout@v2
10
16
  - name: Set up Ruby
11
17
  uses: ruby/setup-ruby@v1
12
18
  with:
13
- ruby-version: 3.0
19
+ ruby-version: ${{ matrix.ruby }}
14
20
  bundler-cache: true
15
21
  - name: Run the default task
16
22
  run: bundle exec rake
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.0.2
data/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2021-07-20
4
+
5
+ - Changed notification title to use name of this gem
6
+
7
+ ## [0.2.0] - 2021-07-17
8
+
9
+ - Uses config files to store buildkite token and buildkite pipeline slug
10
+ - Prompts the user for token and buildkite slug when running for the first time
11
+ - Properly prints errors that are caught
12
+
3
13
  ## [0.1.0] - 2021-06-04
4
14
 
5
15
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,8 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- buildkite_watcher (0.1.0)
5
- rest-client (~> 2.1.0)
4
+ buildkite_watcher (0.3.0)
5
+ rest-client (~> 2.0)
6
+ tty-config (~> 0.5)
7
+ tty-link (~> 0.1)
8
+ tty-prompt (~> 0.23)
6
9
 
7
10
  GEM
8
11
  remote: https://rubygems.org/
@@ -24,6 +27,8 @@ GEM
24
27
  parallel (1.20.1)
25
28
  parser (3.0.1.1)
26
29
  ast (~> 2.4.1)
30
+ pastel (0.8.0)
31
+ tty-color (~> 0.5)
27
32
  pry (0.13.1)
28
33
  coderay (~> 1.1)
29
34
  method_source (~> 1.0)
@@ -69,10 +74,23 @@ GEM
69
74
  rubocop (~> 1.0)
70
75
  rubocop-ast (>= 1.1.0)
71
76
  ruby-progressbar (1.11.0)
77
+ tty-color (0.6.0)
78
+ tty-config (0.5.0)
79
+ tty-cursor (0.7.1)
80
+ tty-link (0.1.1)
81
+ tty-prompt (0.23.1)
82
+ pastel (~> 0.8)
83
+ tty-reader (~> 0.8)
84
+ tty-reader (0.9.0)
85
+ tty-cursor (~> 0.7)
86
+ tty-screen (~> 0.8)
87
+ wisper (~> 2.0)
88
+ tty-screen (0.8.1)
72
89
  unf (0.1.4)
73
90
  unf_ext
74
91
  unf_ext (0.0.7.7)
75
92
  unicode-display_width (2.0.0)
93
+ wisper (2.0.1)
76
94
 
77
95
  PLATFORMS
78
96
  x86_64-darwin-19
@@ -89,4 +107,4 @@ DEPENDENCIES
89
107
  rubocop-rspec (~> 2.3.0)
90
108
 
91
109
  BUNDLED WITH
92
- 2.2.17
110
+ 2.2.22
data/README.md CHANGED
@@ -1,24 +1,37 @@
1
1
  # Buildkite Watcher
2
2
 
3
- Command line utility that continuously watches for the buildkite job running current git HEAD and notifies on build status changes.
3
+ [![Gem Version](https://badge.fury.io/rb/buildkite_watcher.svg)](https://badge.fury.io/rb/buildkite_watcher)
4
+ [![CI Status](https://github.com/nulogy/buildkite_watcher/workflows/CI/badge.svg?branch=main)](https://github.com/nulogy/buildkite_watcher/actions?query=workflow%3ACI)
5
+
6
+ It continuously watches the most recent buildkite job running the current git branch and notifies on build status changes.
7
+
8
+ ![Notification Screenshot](docs/notification_screenshot.png)
4
9
 
5
10
  ## Installation
6
11
 
7
12
  Run this command:
8
13
 
9
- $ gem install buildkite_watcher
14
+ gem install buildkite_watcher
10
15
 
11
16
  ## Usage
12
17
 
13
- From a directory in your project, run.
18
+ From your project's root directory, run:
14
19
 
15
- `$ bw `
20
+ bw
16
21
 
17
22
  ## Development
18
23
 
19
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
24
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
25
+
26
+ To install this gem onto your local machine, run `bundle exec rake install`.
27
+
28
+ ### To release a new version
20
29
 
21
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
30
+ 1. Update `CHANGELOG.md`
31
+ 1. Update the version number in `version.rb`
32
+ 1. Run `bundle install`
33
+ 1. Commit the changes
34
+ 1. Run `bundle exec rake release`
22
35
 
23
36
  ## Contributing
24
37
 
@@ -29,5 +29,8 @@ Gem::Specification.new do |spec|
29
29
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
30
  spec.require_paths = ["lib"]
31
31
 
32
- spec.add_dependency "rest-client", "~> 2.1.0"
32
+ spec.add_dependency "rest-client", "~> 2.0"
33
+ spec.add_dependency "tty-config", "~> 0.5"
34
+ spec.add_dependency "tty-link", "~> 0.1"
35
+ spec.add_dependency "tty-prompt", "~> 0.23"
33
36
  end
@@ -0,0 +1,12 @@
1
+ # How to create the API Access Token in Buildkite
2
+
3
+ 1. Go to [the New API Access Token page](https://buildkite.com/user/api-access-tokens/new) in Buildkite
4
+ 1. Add a description (e.g. buildkite_watcher)
5
+ 1. In "Organization Access", check the organization the pipeline is in.
6
+ 1. Check "Enable GraphQL API Access"
7
+ 1. Click "Create New API Access Token"
8
+ 1. Copy the new token
9
+
10
+ Example:
11
+
12
+ ![New API Access Token page](new_api_access_token_page.png)
Binary file
Binary file
@@ -0,0 +1,7 @@
1
+ # How to find the Buildkite Pipeline Slug
2
+
3
+ 1. Go to https://buildkite.com/ and log in
4
+ 1. On the home page, find the pipeline you want to watch
5
+ 1. The URL in the browser will give you the pipeline slug.
6
+
7
+ For Example, if you see https://buildkite.com/foo-corp/bar-pipeline, the pipeline slug is `foo-corp/bar-pipeline`
@@ -2,7 +2,9 @@
2
2
 
3
3
  require "rainbow"
4
4
  require "rest-client"
5
+ require "json"
5
6
  require "buildkite_watcher/version"
7
+ require "buildkite_watcher/config_loader"
6
8
 
7
9
  # Command line utility that continuously watches for the buildkite job running current git HEAD and
8
10
  # notifies on build status changes.
@@ -71,6 +73,8 @@ module BuildkiteWatcher
71
73
  BUILD_UNKNOWN_STATUS = "UNKNOWN"
72
74
 
73
75
  def result_watch
76
+ config = ConfigLoader.load
77
+
74
78
  Signal.trap("SIGINT") do
75
79
  puts
76
80
  puts Rainbow("Program interrupt received. Exiting.").red
@@ -80,7 +84,7 @@ module BuildkiteWatcher
80
84
  previous_result = BUILD_UNKNOWN_STATUS
81
85
  loop do
82
86
  system("clear")
83
- new_result = result
87
+ new_result = result(config)
84
88
 
85
89
  maybe_notify(previous_result, new_result)
86
90
  previous_result = new_result
@@ -97,7 +101,7 @@ module BuildkiteWatcher
97
101
  # - current commit has a build, has passed
98
102
  # - current commit has a build, has failed
99
103
 
100
- def result
104
+ def result(config)
101
105
  puts Rainbow("Branch: ").bright + Rainbow(branch_name)
102
106
  puts Rainbow("HEAD ➜ ").bright + Rainbow(commit_hash)
103
107
  puts
@@ -105,13 +109,13 @@ module BuildkiteWatcher
105
109
  buildkite_query = {
106
110
  query: GRAPHQL_QUERY,
107
111
  variables: {
108
- pipelineSlug: pipeline_slug,
112
+ pipelineSlug: config.pipeline_slug,
109
113
  commitHash: commit_hash,
110
114
  branch: branch_name,
111
115
  },
112
116
  }
113
117
 
114
- builds = fetch_build_data(buildkite_query)
118
+ builds = fetch_build_data(buildkite_query, config)
115
119
  commit_hash_builds = simplify_builds_response_data(builds.commit_hash_builds)
116
120
  branch_builds = simplify_builds_response_data(builds.branch_builds)
117
121
 
@@ -151,7 +155,7 @@ module BuildkiteWatcher
151
155
  build.state
152
156
  rescue StandardError => e
153
157
  puts
154
- puts Rainbow("ERROR: #{e.message}\n#{e.backtrace}").red
158
+ puts Rainbow("ERROR: \n#{e.backtrace.reverse.join("\n")}\n#{e.message}").red
155
159
  end
156
160
 
157
161
  private
@@ -159,11 +163,12 @@ module BuildkiteWatcher
159
163
  def maybe_notify(previous_result, new_result)
160
164
  return if new_result == previous_result || previous_result == BUILD_UNKNOWN_STATUS
161
165
 
166
+ # You can find available sounds here: '/System/Library/Sounds'
162
167
  case new_result
163
- when build_passed?(new_result)
164
- system('osascript -e \'display notification "CI PASSED" with title "CI Result Watch" sound name "Glass"\'')
168
+ when BUILD_PASSED, BUILD_BLOCKED
169
+ system('osascript -e \'display notification "CI PASSED" with title "Buildkite Watcher" sound name "Glass"\'')
165
170
  when BUILD_FAILED
166
- system('osascript -e \'display notification "CI FAILED" with title "CI Result Watch" sound name "Basso"\'')
171
+ system('osascript -e \'display notification "CI FAILED" with title "Buildkite Watcher" sound name "Funk"\'')
167
172
  end
168
173
  end
169
174
 
@@ -171,13 +176,13 @@ module BuildkiteWatcher
171
176
  [BUILD_PASSED, BUILD_BLOCKED].include?(state)
172
177
  end
173
178
 
174
- def fetch_build_data(buildkite_query)
179
+ def fetch_build_data(buildkite_query, config)
175
180
  response =
176
181
  JSON.parse(
177
182
  RestClient.post(
178
183
  "https://graphql.buildkite.com/v1",
179
184
  buildkite_query.to_json,
180
- { Authorization: "Bearer #{buildkite_token}", content_type: :json },
185
+ { Authorization: "Bearer #{config.buildkite_token}", content_type: :json },
181
186
  ),
182
187
  object_class: OpenStruct,
183
188
  )
@@ -210,7 +215,7 @@ module BuildkiteWatcher
210
215
 
211
216
  def print_state(state)
212
217
  case state
213
- when build_passed?(state)
218
+ when BUILD_PASSED, BUILD_BLOCKED
214
219
  puts Rainbow("CI Status: ").bright + Rainbow(state).green
215
220
  when BUILD_RUNNING
216
221
  puts Rainbow("CI Status: ").bright + Rainbow(state).yellow
@@ -256,24 +261,26 @@ module BuildkiteWatcher
256
261
  puts
257
262
  puts Rainbow("#{failures.count} failures from these specs:").yellow
258
263
  puts
259
- failures.map(&:name).map { |name| /\[(\w+)\] (.*): .*/.match(name).captures }.group_by(&:first)
264
+ failures
265
+ .map(&:name)
266
+ .map { |name| /\[(\w+)\] (.*): .*/.match(name).captures }
267
+ .group_by(&:first)
260
268
  .each do |key, values|
261
- puts key
262
- values.map { |e| e[1] }.uniq.each do |failure_file|
263
- failure_file
264
- puts Rainbow(failure_file).red
269
+ puts key
270
+ values
271
+ .map { |e| e[1] }
272
+ .uniq
273
+ .each do |failure_file|
274
+ failure_file
275
+ puts Rainbow(failure_file).red
276
+ end
265
277
  end
266
- end
267
278
  end
268
279
 
269
280
  def fetch_failures(download_url)
270
281
  JSON.parse(RestClient.get(download_url), object_class: OpenStruct)
271
282
  end
272
283
 
273
- def pipeline_slug
274
- ENV.fetch("PIPELINE_SLUG")
275
- end
276
-
277
284
  def branch_name
278
285
  ENV.fetch("BRANCH_NAME", `git symbolic-ref --short HEAD`.strip)
279
286
  end
@@ -281,9 +288,5 @@ module BuildkiteWatcher
281
288
  def commit_hash
282
289
  ENV.fetch("COMMIT_HASH", `git rev-parse HEAD`.strip)
283
290
  end
284
-
285
- def buildkite_token
286
- @_buildkite_token ||= `cat ~/.buildkite_token`
287
- end
288
291
  end
289
292
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BuildkiteWatcher
4
+ class Config
5
+ attr_reader :pipeline_slug, :buildkite_token
6
+
7
+ def initialize(config, secrets)
8
+ @pipeline_slug = config.fetch(:pipeline_slug)
9
+ @buildkite_token = secrets.fetch(:buildkite_token)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "tty-config"
4
+ require "tty-link"
5
+ require "tty-prompt"
6
+ require "buildkite_watcher/config"
7
+
8
+ module BuildkiteWatcher
9
+ class ConfigLoader
10
+ CONFIG_FILE_NAME = ".buildkite_watcher"
11
+ SECRETS_FILE_NAME = ".buildkite_watcher_secrets"
12
+ EXTENSION = ".yml"
13
+
14
+ def self.load(config = TTY::Config.new, secrets = TTY::Config.new, prompt = TTY::Prompt.new)
15
+ new(config, secrets, prompt).load
16
+ end
17
+
18
+ def load
19
+ config.filename = CONFIG_FILE_NAME
20
+ config.extname = EXTENSION
21
+ config.append_path Dir.pwd
22
+ config.exist? ? config.read : generate_config_file
23
+
24
+ secrets.filename = SECRETS_FILE_NAME
25
+ config.extname = EXTENSION
26
+ secrets.append_path Dir.home
27
+
28
+ secrets.exist? ? secrets.read : generate_secrets_file
29
+
30
+ Config.new(config, secrets)
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :config, :secrets, :prompt
36
+
37
+ def initialize(config, secrets, prompt)
38
+ @config = config
39
+ @secrets = secrets
40
+ @prompt = prompt
41
+ end
42
+
43
+ def generate_config_file
44
+ prompt.ok("Welcome to Buildkite Watcher!")
45
+ prompt.say("I can't find the configuration file, so I'll generate one for you now.")
46
+ link_to_instructions =
47
+ TTY::Link.link_to("instructions", "https://github.com/nulogy/buildkite_watcher/blob/main/docs/pipeline_slug.md")
48
+ pipeline_slug = prompt.ask(<<~MSG)
49
+ What's the pipeline slug of the pipeline you want to watch? See #{link_to_instructions} if you don't know how to find the pipeline slug.
50
+ MSG
51
+ config.set(:pipeline_slug, value: pipeline_slug)
52
+ config.write(create: true)
53
+ end
54
+
55
+ def generate_secrets_file
56
+ link_to_instructions =
57
+ TTY::Link.link_to("here", "https://github.com/nulogy/buildkite_watcher/blob/main/docs/buildkite_token.md")
58
+ link_to_new_token =
59
+ TTY::Link.link_to("New API Access token in Buildkite", "https://buildkite.com/user/api-access-tokens/new")
60
+
61
+ # We use #chop as a work around for a defect in TTY::Prompt that reprints prompt on every character
62
+ # entered
63
+ buildkite_token = prompt.mask(<<~MSG.chop)
64
+ Create a #{link_to_new_token}.
65
+ Make sure it has Organization and GraphQL API Access (more detailed instructions #{link_to_instructions}), and paste the token here:
66
+ MSG
67
+ secrets.set(:buildkite_token, value: buildkite_token)
68
+ secrets.write(create: true)
69
+ end
70
+ end
71
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BuildkiteWatcher
4
- VERSION = "0.1.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: buildkite_watcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arturo Pie
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-04 00:00:00.000000000 Z
11
+ date: 2021-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -16,14 +16,56 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 2.1.0
19
+ version: '2.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 2.1.0
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: tty-config
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: tty-link
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
55
+ - !ruby/object:Gem::Dependency
56
+ name: tty-prompt
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.23'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.23'
27
69
  description: |
28
70
  Command line utility that continuously watches for the buildkite job running current git HEAD and
29
71
  notifies on build status changes.
@@ -39,6 +81,7 @@ files:
39
81
  - ".rspec"
40
82
  - ".rubocop.yml"
41
83
  - ".rubocop_todo.yml"
84
+ - ".tool-versions"
42
85
  - CHANGELOG.md
43
86
  - Gemfile
44
87
  - Gemfile.lock
@@ -47,8 +90,14 @@ files:
47
90
  - bin/console
48
91
  - bin/setup
49
92
  - buildkite_watcher.gemspec
93
+ - docs/buildkite_token.md
94
+ - docs/new_api_access_token_page.png
95
+ - docs/notification_screenshot.png
96
+ - docs/pipeline_slug.md
50
97
  - exe/bw
51
98
  - lib/buildkite_watcher.rb
99
+ - lib/buildkite_watcher/config.rb
100
+ - lib/buildkite_watcher/config_loader.rb
52
101
  - lib/buildkite_watcher/version.rb
53
102
  homepage:
54
103
  licenses: []
@@ -73,7 +122,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
122
  - !ruby/object:Gem::Version
74
123
  version: '0'
75
124
  requirements: []
76
- rubygems_version: 3.2.16
125
+ rubygems_version: 3.2.22
77
126
  signing_key:
78
127
  specification_version: 4
79
128
  summary: CLI utility that watches buildkite and notify on build status changes