fast_ci 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f73e219ff5c12432106b073687d7a644523407740dfdb44a380e369580c8b3a6
4
+ data.tar.gz: 8e57bc31745f2749d3b2b813d07b96ae501a06dd1c976f0e4b77436fe0079a13
5
+ SHA512:
6
+ metadata.gz: 8dc1ee8b038bf4134b3bda60e3d11129bf582dfebb61336dc04d1f6ef73c66cc8cb0203c9a85e1a9454771cef27c410f784db616722e33c99b163bfd6b532cbb
7
+ data.tar.gz: 3051caa8148a323a4ce5b539a378f0b15531db83a0b26f51147d42595900d560e5a6265c9250d919b594595115b2b8c77e62c283adb72c7c2a55bef5e1996fe6
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ Style/StringLiterals:
2
+ Enabled: true
3
+ EnforcedStyle: double_quotes
4
+
5
+ Style/StringLiteralsInInterpolation:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Layout/LineLength:
10
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in fast_ci.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/Gemfile.lock ADDED
@@ -0,0 +1,96 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fast_ci (0.1.0)
5
+ async-websocket (~> 0.19)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ async (1.30.1)
12
+ console (~> 1.10)
13
+ nio4r (~> 2.3)
14
+ timers (~> 4.1)
15
+ async-http (0.56.5)
16
+ async (>= 1.25)
17
+ async-io (>= 1.28)
18
+ async-pool (>= 0.2)
19
+ protocol-http (~> 0.22.0)
20
+ protocol-http1 (~> 0.14.0)
21
+ protocol-http2 (~> 0.14.0)
22
+ async-io (1.32.2)
23
+ async
24
+ async-pool (0.3.8)
25
+ async (>= 1.25)
26
+ async-websocket (0.19.0)
27
+ async-http (~> 0.54)
28
+ async-io (~> 1.23)
29
+ protocol-websocket (~> 0.7.0)
30
+ coderay (1.1.3)
31
+ console (1.13.1)
32
+ fiber-local
33
+ diff-lcs (1.4.4)
34
+ fiber-local (1.0.0)
35
+ method_source (1.0.0)
36
+ nio4r (2.5.8)
37
+ parallel (1.20.1)
38
+ parser (3.0.2.0)
39
+ ast (~> 2.4.1)
40
+ protocol-hpack (1.4.2)
41
+ protocol-http (0.22.5)
42
+ protocol-http1 (0.14.2)
43
+ protocol-http (~> 0.22)
44
+ protocol-http2 (0.14.2)
45
+ protocol-hpack (~> 1.4)
46
+ protocol-http (~> 0.18)
47
+ protocol-websocket (0.7.5)
48
+ protocol-http (~> 0.2)
49
+ protocol-http1 (~> 0.2)
50
+ pry (0.14.1)
51
+ coderay (~> 1.1)
52
+ method_source (~> 1.0)
53
+ rainbow (3.0.0)
54
+ rake (13.0.6)
55
+ regexp_parser (2.1.1)
56
+ rexml (3.2.5)
57
+ rspec (3.10.0)
58
+ rspec-core (~> 3.10.0)
59
+ rspec-expectations (~> 3.10.0)
60
+ rspec-mocks (~> 3.10.0)
61
+ rspec-core (3.10.1)
62
+ rspec-support (~> 3.10.0)
63
+ rspec-expectations (3.10.1)
64
+ diff-lcs (>= 1.2.0, < 2.0)
65
+ rspec-support (~> 3.10.0)
66
+ rspec-mocks (3.10.2)
67
+ diff-lcs (>= 1.2.0, < 2.0)
68
+ rspec-support (~> 3.10.0)
69
+ rspec-support (3.10.2)
70
+ rubocop (1.20.0)
71
+ parallel (~> 1.10)
72
+ parser (>= 3.0.0.0)
73
+ rainbow (>= 2.2.2, < 4.0)
74
+ regexp_parser (>= 1.8, < 3.0)
75
+ rexml
76
+ rubocop-ast (>= 1.9.1, < 2.0)
77
+ ruby-progressbar (~> 1.7)
78
+ unicode-display_width (>= 1.4.0, < 3.0)
79
+ rubocop-ast (1.11.0)
80
+ parser (>= 3.0.1.1)
81
+ ruby-progressbar (1.11.0)
82
+ timers (4.3.3)
83
+ unicode-display_width (2.0.0)
84
+
85
+ PLATFORMS
86
+ arm64-darwin-20
87
+
88
+ DEPENDENCIES
89
+ fast_ci!
90
+ pry
91
+ rake (~> 13.0)
92
+ rspec (~> 3.0)
93
+ rubocop (~> 1.7)
94
+
95
+ BUNDLED WITH
96
+ 2.2.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Ale ∴
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # FastCI
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/fast_ci`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'fast_ci'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install fast_ci
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/fast_ci.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "fast_ci"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/fast_ci.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/fast_ci/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "fast_ci"
7
+ spec.version = FastCI::VERSION
8
+ spec.authors = ["Ale ∴"]
9
+ spec.email = ["ale@alexvko.com"]
10
+
11
+ spec.summary = "Ruby wrapper for creating FastCI integrations"
12
+ spec.description = "Ruby wrapper for creating FastCI integrations"
13
+ spec.homepage = "https://github.com/alexvko/fast_ci-rb"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.4.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = spec.homepage
19
+ spec.metadata["changelog_uri"] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
25
+ end
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "async-websocket", "~> 0.19"
31
+ spec.add_development_dependency "pry"
32
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FastCI
4
+ class Configuration
5
+ attr_accessor :run_key, :build_id, :commit, :branch, :api_url, :secret_key
6
+
7
+ def initialize
8
+ # Settings defaults
9
+ self.run_key = nil
10
+ self.build_id = ENV.fetch("BUILD_ID")
11
+ self.commit = `git rev-parse --short HEAD`.chomp
12
+ self.branch = `git rev-parse --abbrev-ref HEAD`.chomp
13
+ self.api_url = ENV["FAST_CI_API_URL"] || "api.fast.ci"
14
+ self.secret_key = ENV.fetch("FAST_CI_SECRET_KEY")
15
+ end
16
+
17
+ def reset
18
+ initialize
19
+ end
20
+
21
+ def run_key
22
+ @run_key || raise("#run_key was not configured.")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FastCI
4
+ class EventAlreadyDefinedError < StandardError
5
+ def initialize(event)
6
+ super("Event '#{event}' is already defined")
7
+ end
8
+ end
9
+
10
+ class EventNotSupportedError < StandardError
11
+ def initialize(event)
12
+ msg = "Event '#{event}' not supported. \n" \
13
+ "Supported events are #{FastCI::SUPPORTED_EVENTS.inspect}}"
14
+ super(msg)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FastCI
4
+ VERSION = "0.1.0"
5
+ end
data/lib/fast_ci.rb ADDED
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "fast_ci/version"
4
+ require_relative "fast_ci/configuration"
5
+ require_relative "fast_ci/exceptions"
6
+
7
+ require "async"
8
+ require "async/http/endpoint"
9
+ require "async/websocket/client"
10
+
11
+ module FastCI
12
+ class Error < StandardError; end
13
+
14
+ class << self
15
+ def configuration
16
+ @configuration ||= FastCI::Configuration.new
17
+ end
18
+
19
+ def configure
20
+ yield(configuration)
21
+ end
22
+
23
+ def ws
24
+ @ws ||= WebSocket.new
25
+ end
26
+
27
+ def await
28
+ ws.await
29
+ end
30
+
31
+ def debug(msg)
32
+ puts "\n\e[36mDEBUG: \e[0m #{msg}\n" if ENV["FAST_CI_DEBUG"]
33
+ end
34
+ end
35
+
36
+ class WebSocket
37
+ attr_reader :node_index
38
+
39
+ SUPPORTED_EVENTS = %i[enq_request deq].freeze
40
+
41
+ def initialize
42
+ @on = {}
43
+ @ref = 0
44
+ end
45
+
46
+ def on(event, &block)
47
+ raise EventNotSupportedError, event unless SUPPORTED_EVENTS.include?(event)
48
+ raise EventAlreadyDefinedError, event if @on[event]
49
+
50
+ @on[event] = block
51
+ end
52
+
53
+ def send_msg(connection, event, payload = {})
54
+ FastCI.debug("ws#send_msg: #{event} -> #{payload.inspect}")
55
+ connection.write({ "topic": topic, "event": event, "payload": payload, "ref": ref })
56
+ connection.flush
57
+ end
58
+
59
+ def await
60
+ before_start_connection
61
+ Async do |task|
62
+ Async::WebSocket::Client.connect(endpoint) do |connection|
63
+ after_start_connection
64
+ send_msg(connection, "phx_join")
65
+
66
+ while message = connection.read
67
+ FastCI.debug("ws#msg_received: #{message.inspect}")
68
+
69
+ response = message.dig(:payload, :response)
70
+
71
+ case response&.dig(:event) || message[:event]
72
+ when "join"
73
+ handle_join(connection, response)
74
+ when "deq_request"
75
+ handle_deq_request(connection, response)
76
+ when "deq"
77
+ if (tests = response[:tests]).any?
78
+ result = @on[:deq].call(tests)
79
+ task.async do
80
+ send_msg(connection, "deq", result)
81
+ end
82
+ else
83
+ break
84
+ end
85
+ when "error"
86
+ raise(response.inspect)
87
+ else
88
+ puts response
89
+ end
90
+ end
91
+ ensure
92
+ send_msg(connection, "leave")
93
+ connection.close
94
+ end
95
+ end
96
+ end
97
+
98
+ private
99
+
100
+ # https://github.com/bblimke/webmock/blob/b709ba22a2949dc3bfac662f3f4da88a21679c2e/lib/webmock/http_lib_adapters/async_http_client_adapter.rb#L8
101
+ def before_start_connection
102
+ if defined?(WebMock::HttpLibAdapters::AsyncHttpClientAdapter)
103
+ WebMock::HttpLibAdapters::AsyncHttpClientAdapter.disable!
104
+ end
105
+ end
106
+
107
+ # https://github.com/bblimke/webmock/blob/b709ba22a2949dc3bfac662f3f4da88a21679c2e/lib/webmock/http_lib_adapters/async_http_client_adapter.rb#L8
108
+ def after_start_connection
109
+ if defined?(WebMock::HttpLibAdapters::AsyncHttpClientAdapter)
110
+ WebMock::HttpLibAdapters::AsyncHttpClientAdapter.enable!
111
+ end
112
+ end
113
+
114
+ def handle_join(connection, response)
115
+ @node_index = response[:node_index]
116
+
117
+ FastCI.debug("NODE_INDEX: #{@node_index}")
118
+
119
+ send_msg(connection, "enq", { tests: @on[:enq_request].call }) if node_index.zero?
120
+
121
+ send_msg(connection, "deq") if response[:state] == "running"
122
+ end
123
+
124
+ def handle_deq_request(connection, _response)
125
+ send_msg(connection, "deq")
126
+ end
127
+
128
+ def ref
129
+ @ref += 1
130
+ end
131
+
132
+ def topic
133
+ "test_orchestrator:#{FastCI.configuration.run_key}-#{FastCI.configuration.build_id}"
134
+ end
135
+
136
+ def endpoint
137
+ params = URI.encode_www_form({
138
+ build_id: FastCI.configuration.build_id,
139
+ run_key: FastCI.configuration.run_key,
140
+ secret_key: FastCI.configuration.secret_key,
141
+ commit: FastCI.configuration.commit,
142
+ branch: FastCI.configuration.branch
143
+ })
144
+
145
+ url = "ws://#{FastCI.configuration.api_url}/test_orchestrators/socket/websocket?#{params}"
146
+
147
+ Async::HTTP::Endpoint.parse(url)
148
+ end
149
+ end
150
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fast_ci
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ale ∴
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: async-websocket
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.19'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.19'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Ruby wrapper for creating FastCI integrations
42
+ email:
43
+ - ale@alexvko.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - Gemfile
52
+ - Gemfile.lock
53
+ - LICENSE.txt
54
+ - README.md
55
+ - Rakefile
56
+ - bin/console
57
+ - bin/setup
58
+ - fast_ci.gemspec
59
+ - lib/fast_ci.rb
60
+ - lib/fast_ci/configuration.rb
61
+ - lib/fast_ci/exceptions.rb
62
+ - lib/fast_ci/version.rb
63
+ homepage: https://github.com/alexvko/fast_ci-rb
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/alexvko/fast_ci-rb
68
+ source_code_uri: https://github.com/alexvko/fast_ci-rb
69
+ changelog_uri: https://github.com/alexvko/fast_ci-rb
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.4.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.2.22
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Ruby wrapper for creating FastCI integrations
89
+ test_files: []