ruby_ci 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 57e0c7c2a361ec9e8593b1a18493ed2e59b6a2093f8cc50ac81e53757ad3b4e9
4
+ data.tar.gz: 8ed6ac909dba27be725e498ff068e422a5352dd432367bc71eb6b31b779ba9c5
5
+ SHA512:
6
+ metadata.gz: 06e0dad118feea336a5c37bd01b3be1fb381305863fbdba06e0aec6b66ee45e922164f8df1189c0089a74543897e91466004043b4b681c3b7c28786a30aaa812
7
+ data.tar.gz: ba26aef78a312efd0b6452121eea6fac7796a9e0a6a86823306faea2e31c6239dbf7d3e66f160458ca51712a7dc8c9275798c3c52d2c5017bcf42c865bf9f415
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,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in ruby_ci.gemspec
8
+ gemspec
9
+
10
+ gem "rake", "~> 13.0"
11
+
12
+ gem "rspec", "~> 3.0"
13
+
14
+ gem "rubocop", "~> 1.7"
15
+
16
+ gem "async-websocket", github: "socketry/async-websocket", branch: "master"
data/Gemfile.lock ADDED
@@ -0,0 +1,103 @@
1
+ GIT
2
+ remote: https://github.com/socketry/async-websocket
3
+ revision: a63151d0759edde5ca5cd9ffa0414d9f2e295ee8
4
+ branch: master
5
+ specs:
6
+ async-websocket (0.19.0)
7
+ async-http (~> 0.54)
8
+ async-io (~> 1.23)
9
+ protocol-websocket (~> 0.7.0)
10
+
11
+ PATH
12
+ remote: .
13
+ specs:
14
+ ruby_ci (0.1.1)
15
+ async-websocket
16
+
17
+ GEM
18
+ remote: https://rubygems.org/
19
+ specs:
20
+ ast (2.4.2)
21
+ async (1.30.1)
22
+ console (~> 1.10)
23
+ nio4r (~> 2.3)
24
+ timers (~> 4.1)
25
+ async-http (0.56.5)
26
+ async (>= 1.25)
27
+ async-io (>= 1.28)
28
+ async-pool (>= 0.2)
29
+ protocol-http (~> 0.22.0)
30
+ protocol-http1 (~> 0.14.0)
31
+ protocol-http2 (~> 0.14.0)
32
+ async-io (1.32.2)
33
+ async
34
+ async-pool (0.3.9)
35
+ async (>= 1.25)
36
+ coderay (1.1.3)
37
+ console (1.13.1)
38
+ fiber-local
39
+ diff-lcs (1.4.4)
40
+ fiber-local (1.0.0)
41
+ method_source (1.0.0)
42
+ nio4r (2.5.8)
43
+ parallel (1.20.1)
44
+ parser (3.0.2.0)
45
+ ast (~> 2.4.1)
46
+ protocol-hpack (1.4.2)
47
+ protocol-http (0.22.5)
48
+ protocol-http1 (0.14.2)
49
+ protocol-http (~> 0.22)
50
+ protocol-http2 (0.14.2)
51
+ protocol-hpack (~> 1.4)
52
+ protocol-http (~> 0.18)
53
+ protocol-websocket (0.7.5)
54
+ protocol-http (~> 0.2)
55
+ protocol-http1 (~> 0.2)
56
+ pry (0.14.1)
57
+ coderay (~> 1.1)
58
+ method_source (~> 1.0)
59
+ rainbow (3.0.0)
60
+ rake (13.0.6)
61
+ regexp_parser (2.1.1)
62
+ rexml (3.2.5)
63
+ rspec (3.10.0)
64
+ rspec-core (~> 3.10.0)
65
+ rspec-expectations (~> 3.10.0)
66
+ rspec-mocks (~> 3.10.0)
67
+ rspec-core (3.10.1)
68
+ rspec-support (~> 3.10.0)
69
+ rspec-expectations (3.10.1)
70
+ diff-lcs (>= 1.2.0, < 2.0)
71
+ rspec-support (~> 3.10.0)
72
+ rspec-mocks (3.10.2)
73
+ diff-lcs (>= 1.2.0, < 2.0)
74
+ rspec-support (~> 3.10.0)
75
+ rspec-support (3.10.2)
76
+ rubocop (1.20.0)
77
+ parallel (~> 1.10)
78
+ parser (>= 3.0.0.0)
79
+ rainbow (>= 2.2.2, < 4.0)
80
+ regexp_parser (>= 1.8, < 3.0)
81
+ rexml
82
+ rubocop-ast (>= 1.9.1, < 2.0)
83
+ ruby-progressbar (~> 1.7)
84
+ unicode-display_width (>= 1.4.0, < 3.0)
85
+ rubocop-ast (1.11.0)
86
+ parser (>= 3.0.1.1)
87
+ ruby-progressbar (1.11.0)
88
+ timers (4.3.3)
89
+ unicode-display_width (2.0.0)
90
+
91
+ PLATFORMS
92
+ arm64-darwin-20
93
+
94
+ DEPENDENCIES
95
+ async-websocket!
96
+ ruby_ci!
97
+ pry
98
+ rake (~> 13.0)
99
+ rspec (~> 3.0)
100
+ rubocop (~> 1.7)
101
+
102
+ BUNDLED WITH
103
+ 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
+ # RubyCI
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/ruby_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 'ruby_ci'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install ruby_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]/ruby_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 "ruby_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
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCI
4
+ class Configuration
5
+ attr_accessor :run_key, :build_id, :commit_msg, :commit, :branch,
6
+ :api_url, :secret_key, :author
7
+
8
+ def initialize
9
+ # Settings defaults
10
+ self.run_key = nil
11
+ self.build_id = guess_build_id
12
+ self.commit = guess_commit
13
+ self.commit_msg = `git log -1 --pretty=%B`.chomp
14
+ self.branch = guess_branch
15
+ self.api_url = ENV["RUBY_CI_API_URL"] || "api.fast.ci"
16
+ self.secret_key = ENV.fetch("RUBY_CI_SECRET_KEY")
17
+ self.author = guess_author
18
+ end
19
+
20
+ def reset
21
+ initialize
22
+ end
23
+
24
+ def run_key
25
+ @run_key || raise("#run_key was not configured.")
26
+ end
27
+
28
+ def guess_author
29
+ author_Line = `git show | grep Author:`
30
+ name, email = author_Line.scan(/Author: (.*) <(.*)>/).first
31
+ {
32
+ name: name,
33
+ email: email
34
+ }
35
+ end
36
+
37
+ def guess_build_id
38
+ %w[GITHUB_RUN_ID BUILD_ID CIRCLE_BUILD_NUM].find do |keyword|
39
+ key = ENV.keys.find { |k| k[keyword] }
40
+ break ENV[key] if key && ENV[key]
41
+ end || guess_commit
42
+ end
43
+
44
+ def guess_commit
45
+ %w[_COMMIT _SHA1 _SHA].find do |keyword|
46
+ key = ENV.keys.find { |k| k[keyword] }
47
+ break ENV[key] if key && ENV[key]
48
+ end || `git rev-parse --short HEAD`.chomp
49
+ end
50
+
51
+ def guess_branch
52
+ %w[_BRANCH _REF].find do |keyword|
53
+ key = ENV.keys.find { |k| k[keyword] }
54
+ break ENV[key] if key && ENV[key]
55
+ end || `git rev-parse --abbrev-ref HEAD`.chomp
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCI
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 #{RubyCI::SUPPORTED_EVENTS.inspect}}"
14
+ super(msg)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCI
4
+ VERSION = "0.1.1"
5
+ end
data/lib/ruby_ci.rb ADDED
@@ -0,0 +1,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ruby_ci/version"
4
+ require_relative "ruby_ci/configuration"
5
+ require_relative "ruby_ci/exceptions"
6
+
7
+ require "async"
8
+ require "async/http/endpoint"
9
+ require "async/websocket/client"
10
+ require "async/logger"
11
+ Async.logger.debug!
12
+
13
+ module RubyCI
14
+ class Error < StandardError; end
15
+
16
+ class << self
17
+ def configuration
18
+ @configuration ||= RubyCI::Configuration.new
19
+ end
20
+
21
+ def configure
22
+ yield(configuration)
23
+ end
24
+
25
+ def ws
26
+ @ws ||= WebSocket.new
27
+ end
28
+
29
+ def await
30
+ ws.await
31
+ end
32
+
33
+ def debug(msg)
34
+ puts "\n\e[36mDEBUG: \e[0m #{msg}\n" if ENV["RUBY_CI_DEBUG"]
35
+ end
36
+ end
37
+
38
+ class WebSocket
39
+ attr_reader :node_index
40
+ attr_accessor :connection, :task
41
+
42
+ SUPPORTED_EVENTS = %i[enq_request deq].freeze
43
+
44
+ def initialize
45
+ @on = {}
46
+ @ref = 0
47
+ end
48
+
49
+ def on(event, &block)
50
+ raise EventNotSupportedError, event unless SUPPORTED_EVENTS.include?(event)
51
+ raise EventAlreadyDefinedError, event if @on[event]
52
+
53
+ @on[event] = block
54
+ end
55
+
56
+ def send_msg(event, payload = {})
57
+ RubyCI.debug("ws#send_msg: #{event} -> #{payload.inspect}")
58
+ connection.write({ "topic": topic, "event": event, "payload": payload, "ref": ref })
59
+ connection.flush
60
+ end
61
+
62
+ def connect_to_ws
63
+ Async do |task|
64
+ before_start_connection
65
+ Async::WebSocket::Client.connect(endpoint) do |connection|
66
+ after_start_connection
67
+ self.connection = connection
68
+ self.task = task
69
+ yield
70
+ ensure
71
+
72
+ leave
73
+ end
74
+ end
75
+ end
76
+
77
+ def await(retry_count = 0)
78
+ connect_to_ws do
79
+ send_msg("phx_join")
80
+
81
+ while message = connection.read
82
+ RubyCI.debug("ws#msg_received: #{message.inspect}")
83
+
84
+ response = message.dig(:payload, :response)
85
+
86
+ case response&.dig(:event) || message[:event]
87
+ when "phx_error"
88
+ raise("[RubyCI] Unexpected error")
89
+ when "join"
90
+ handle_join(response)
91
+ when "deq_request"
92
+ handle_deq_request(response)
93
+ when "deq"
94
+ if (tests = response[:tests]).any?
95
+ result = @on[:deq].call(tests)
96
+ task.async do
97
+ send_msg("deq", result)
98
+ end
99
+ else
100
+ break
101
+ end
102
+ when "error"
103
+ raise(response.inspect)
104
+ else
105
+ puts response
106
+ end
107
+ end
108
+ end
109
+ end
110
+
111
+ private
112
+
113
+ def leave
114
+ send_msg("leave")
115
+ connection.close
116
+ rescue StandardError => e
117
+ # noop
118
+ end
119
+
120
+ # https://github.com/bblimke/webmock/blob/b709ba22a2949dc3bfac662f3f4da88a21679c2e/lib/webmock/http_lib_adapters/async_http_client_adapter.rb#L8
121
+ def before_start_connection
122
+ if defined?(WebMock::HttpLibAdapters::AsyncHttpClientAdapter)
123
+ WebMock::HttpLibAdapters::AsyncHttpClientAdapter.disable!
124
+ end
125
+ end
126
+
127
+ # https://github.com/bblimke/webmock/blob/b709ba22a2949dc3bfac662f3f4da88a21679c2e/lib/webmock/http_lib_adapters/async_http_client_adapter.rb#L8
128
+ def after_start_connection
129
+ if defined?(WebMock::HttpLibAdapters::AsyncHttpClientAdapter)
130
+ WebMock::HttpLibAdapters::AsyncHttpClientAdapter.enable!
131
+ end
132
+ end
133
+
134
+ def handle_join(response)
135
+ @node_index = response[:node_index]
136
+
137
+ RubyCI.debug("NODE_INDEX: #{@node_index}")
138
+
139
+ send_msg("enq", { tests: @on[:enq_request].call }) if node_index.zero?
140
+
141
+ send_msg("deq") if response[:state] == "running"
142
+ end
143
+
144
+ def handle_deq_request(_response)
145
+ send_msg("deq")
146
+ end
147
+
148
+ def ref
149
+ @ref += 1
150
+ end
151
+
152
+ def topic
153
+ "test_orchestrator:#{RubyCI.configuration.run_key}-#{RubyCI.configuration.build_id}"
154
+ end
155
+
156
+ def endpoint
157
+ params = URI.encode_www_form({
158
+ build_id: RubyCI.configuration.build_id,
159
+ run_key: RubyCI.configuration.run_key,
160
+ secret_key: RubyCI.configuration.secret_key,
161
+ branch: RubyCI.configuration.branch,
162
+ commit: RubyCI.configuration.commit,
163
+ commit_msg: RubyCI.configuration.commit_msg,
164
+ author: RubyCI.configuration.author.to_json
165
+ })
166
+
167
+ url = "ws://#{RubyCI.configuration.api_url}/test_orchestrators/socket/websocket?#{params}"
168
+
169
+ Async::HTTP::Endpoint.parse(url)
170
+ end
171
+ end
172
+ end
data/ruby_ci.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/ruby_ci/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ruby_ci"
7
+ spec.version = RubyCI::VERSION
8
+ spec.authors = ["Ale ∴"]
9
+ spec.email = ["ale@alexvko.com"]
10
+
11
+ spec.summary = "Ruby wrapper for creating RubyCI integrations"
12
+ spec.description = "Ruby wrapper for creating RubyCI integrations"
13
+ spec.homepage = "https://github.com/alexvko/ruby_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"
31
+ spec.add_development_dependency "pry"
32
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby_ci
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Ale ∴
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-11-16 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'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
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 RubyCI 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
+ - lib/ruby_ci.rb
59
+ - lib/ruby_ci/configuration.rb
60
+ - lib/ruby_ci/exceptions.rb
61
+ - lib/ruby_ci/version.rb
62
+ - ruby_ci.gemspec
63
+ homepage: https://github.com/alexvko/ruby_ci-rb
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/alexvko/ruby_ci-rb
68
+ source_code_uri: https://github.com/alexvko/ruby_ci-rb
69
+ changelog_uri: https://github.com/alexvko/ruby_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 RubyCI integrations
89
+ test_files: []