rspec-mergify 0.2.0-x86_64-linux

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: f780ead2142231a815ead11c224dfaa6755bf705face5454b912a0f6411f3711
4
+ data.tar.gz: ca7701151c7015707c8a1e9d784844e7a8c939f21dd3c3afd01b61141e1038c5
5
+ SHA512:
6
+ metadata.gz: f45dc1da3c292a7a5f05a8531ee63bf83fb804d0051beb7667075742522451bb6c36e1f2d99f4e0cdb56bd66e445860c5d9387331ae7ff8ae5b97fd15df70f69
7
+ data.tar.gz: 4b6295d176645780f74f2aebf3e6162110136163520624c4a274c12e7a06bd089103fc1f9d11712d5a6422e998fb3a4597f46a45830f3c8a74f463420dda3c0e
data/LICENSE ADDED
@@ -0,0 +1,176 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # rspec-mergify
2
+
3
+ RSpec plugin for [Mergify Test Insights](https://docs.mergify.com/ci-insights/).
4
+
5
+ ## Features
6
+
7
+ - **Test tracing** — Sends OpenTelemetry traces for every test to Mergify's API
8
+ - **Flaky test detection** — Intelligently reruns tests to detect flakiness with budget constraints
9
+ - **Test quarantine** — Quarantines failing tests so they don't block CI
10
+
11
+ ## Installation
12
+
13
+ Add to your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'rspec-mergify'
17
+ ```
18
+
19
+ Then run `bundle install`.
20
+
21
+ ## Configuration
22
+
23
+ Set the `MERGIFY_TOKEN` environment variable with your Mergify API token.
24
+
25
+ The plugin activates automatically when running in CI (detected via the `CI` environment variable). To enable outside CI, set `RSPEC_MERGIFY_ENABLE=true`.
26
+
27
+ ### Environment Variables
28
+
29
+ | Variable | Description | Default |
30
+ |---|---|---|
31
+ | `MERGIFY_TOKEN` | Mergify API authentication token | (required) |
32
+ | `MERGIFY_API_URL` | Mergify API endpoint | `https://api.mergify.com` |
33
+ | `RSPEC_MERGIFY_ENABLE` | Force-enable outside CI | `false` |
34
+ | `RSPEC_MERGIFY_DEBUG` | Print spans to console | `false` |
35
+ | `MERGIFY_TRACEPARENT` | W3C distributed trace context | — |
36
+ | `MERGIFY_TEST_JOB_NAME` | Mergify test job name | — |
37
+
38
+ For detailed documentation, see the [official guide](https://docs.mergify.com/ci-insights/test-frameworks/rspec/).
39
+
40
+ ## Development
41
+
42
+ ### Prerequisites
43
+
44
+ - Ruby >= 3.1 (`.ruby-version` pins to 4.0.6 — use [rbenv](https://github.com/rbenv/rbenv) or [mise](https://mise.jdx.dev/) to install it)
45
+ - Bundler
46
+
47
+ ### Setup
48
+
49
+ ```bash
50
+ rbenv install # install the Ruby version from .ruby-version (if needed)
51
+ bundle install
52
+ ```
53
+
54
+ ### Running Tests
55
+
56
+ ```bash
57
+ bundle exec rspec
58
+ ```
59
+
60
+ ### Linting
61
+
62
+ ```bash
63
+ bundle exec rubocop
64
+ ```
65
+
66
+ ## License
67
+
68
+ Apache-2.0 — see the [LICENSE](../../LICENSE) at the repository root.
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'securerandom'
4
+ require 'opentelemetry-sdk'
5
+ require_relative 'utils'
6
+ require_relative 'native'
7
+ require_relative 'synchronous_batch_span_processor'
8
+ require_relative 'resources/rspec'
9
+
10
+ module Mergify
11
+ module RSpec
12
+ # Central orchestrator for Mergify Test Insights: sets up OpenTelemetry tracing,
13
+ # manages the tracer provider, and coordinates flaky detection and quarantine.
14
+ # rubocop:disable-next Metrics/ClassLength
15
+ class CIInsights
16
+ attr_reader :token, :repo_name, :api_url, :test_run_id,
17
+ :tracer_provider, :tracer, :exporter,
18
+ :branch_name,
19
+ :flaky_detector, :flaky_detector_error_message, :quarantined_tests
20
+
21
+ # rubocop:disable-next Metrics/MethodLength
22
+ def initialize
23
+ @token = ENV.fetch('MERGIFY_TOKEN', nil)
24
+ @repo_name = Native.detect_repository_name
25
+ @api_url = ENV.fetch('MERGIFY_API_URL', 'https://api.mergify.com')
26
+ @test_run_id = SecureRandom.hex(8)
27
+ @tracer_provider = nil
28
+ @tracer = nil
29
+ @exporter = nil
30
+ @branch_name = nil
31
+ @flaky_detector = nil
32
+ @flaky_detector_error_message = nil
33
+ @quarantined_tests = nil
34
+
35
+ setup_tracing if Utils.in_ci?
36
+ end
37
+
38
+ def mark_test_as_quarantined_if_needed(example_id) # rubocop:disable Naming/PredicateMethod
39
+ return false unless @quarantined_tests&.include?(example_id)
40
+
41
+ @quarantined_tests.mark_as_used(example_id)
42
+ true
43
+ end
44
+
45
+ private
46
+
47
+ def setup_tracing
48
+ processor, exp = build_processor
49
+ return unless processor
50
+
51
+ @exporter = exp
52
+ resource = build_resource
53
+ @tracer_provider = OpenTelemetry::SDK::Trace::TracerProvider.new(resource: resource)
54
+ @tracer_provider.add_span_processor(processor)
55
+ @tracer = @tracer_provider.tracer('rspec-mergify', Mergify::RSpec::VERSION)
56
+ @branch_name = extract_branch_name(resource)
57
+ load_flaky_detector
58
+ load_quarantine
59
+ end
60
+
61
+ def build_processor
62
+ if debug_mode? || test_mode?
63
+ build_in_memory_processor
64
+ elsif @token && @repo_name
65
+ build_otlp_processor
66
+ else
67
+ [nil, nil]
68
+ end
69
+ end
70
+
71
+ def debug_mode?
72
+ ENV.key?('RSPEC_MERGIFY_DEBUG')
73
+ end
74
+
75
+ def test_mode?
76
+ ENV['_RSPEC_MERGIFY_TEST'] == 'true'
77
+ end
78
+
79
+ def build_in_memory_processor
80
+ exp = OpenTelemetry::SDK::Trace::Export::InMemorySpanExporter.new
81
+ processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(exp)
82
+ [processor, exp]
83
+ end
84
+
85
+ def build_otlp_processor
86
+ owner, repo = Utils.split_full_repo_name(@repo_name)
87
+ endpoint = "#{@api_url}/v1/ci/#{owner}/repositories/#{repo}/traces"
88
+ exp = create_otlp_exporter(endpoint)
89
+ processor = SynchronousBatchSpanProcessor.new(exp)
90
+ [processor, exp]
91
+ end
92
+
93
+ # The cicd.* and vcs.* attributes come from the Rust core, which every
94
+ # Mergify test client shares, so a provider gains them everywhere at once.
95
+ # What stays here is what only Ruby knows: the test framework, and the id
96
+ # this run invented for itself.
97
+ def build_resource
98
+ resources = [
99
+ OpenTelemetry::SDK::Resources::Resource.create(Native.detect_attributes),
100
+ Resources::RSpec.detect,
101
+ OpenTelemetry::SDK::Resources::Resource.create('test.run.id' => @test_run_id)
102
+ ]
103
+ resources.reduce(OpenTelemetry::SDK::Resources::Resource.create({})) do |merged, r|
104
+ merged.merge(r)
105
+ end
106
+ end
107
+
108
+ def extract_branch_name(resource)
109
+ attrs = resource.attribute_enumerator.to_h
110
+ @base_branch_name = attrs['vcs.ref.base.name']
111
+ @base_branch_name || attrs['vcs.ref.head.name']
112
+ end
113
+
114
+ # rubocop:disable-next Metrics/MethodLength
115
+ def create_otlp_exporter(endpoint)
116
+ require 'opentelemetry-exporter-otlp'
117
+ original_env = ENV.fetch('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT', nil)
118
+ ENV['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'] = endpoint
119
+ begin
120
+ OpenTelemetry::Exporter::OTLP::Exporter.new(
121
+ endpoint: endpoint,
122
+ headers: { 'Authorization' => "Bearer #{@token}" },
123
+ compression: 'gzip'
124
+ )
125
+ ensure
126
+ if original_env
127
+ ENV['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'] = original_env
128
+ else
129
+ ENV.delete('OTEL_EXPORTER_OTLP_TRACES_ENDPOINT')
130
+ end
131
+ end
132
+ end
133
+
134
+ # rubocop:disable-next Metrics/MethodLength
135
+ def load_flaky_detector
136
+ return unless @token && @repo_name
137
+
138
+ require_relative 'flaky_detection'
139
+ mode = @base_branch_name ? 'new' : 'unhealthy'
140
+ @flaky_detector = FlakyDetector.new(
141
+ token: @token,
142
+ url: @api_url,
143
+ full_repository_name: @repo_name,
144
+ mode: mode
145
+ )
146
+ rescue FlakyDetectionDisabledError
147
+ # The repository has not opted into flaky detection, or has no baseline
148
+ # yet. Both are expected; skip without surfacing an error.
149
+ nil
150
+ rescue StandardError => e
151
+ @flaky_detector_error_message = "Could not load flaky detector: #{e.message}"
152
+ end
153
+
154
+ def load_quarantine
155
+ return unless @token && @repo_name && @branch_name
156
+
157
+ require_relative 'quarantine'
158
+ @quarantined_tests = Quarantine.new(
159
+ api_url: @api_url,
160
+ token: @token,
161
+ repo_name: @repo_name,
162
+ branch_name: @branch_name
163
+ )
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'set'
4
+
5
+ module Mergify
6
+ module RSpec
7
+ # Registers RSpec hooks for quarantine and flaky detection, and adds the
8
+ # Mergify Test Insights formatter when running inside CI.
9
+ module Configuration
10
+ module_function
11
+
12
+ # rubocop:disable Metrics/MethodLength,Metrics/BlockLength,Metrics/AbcSize
13
+ # rubocop:disable-next Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
14
+ def setup!
15
+ ::RSpec.configure do |config|
16
+ # Add formatter when in CI
17
+ config.add_formatter(Mergify::RSpec::Formatter) if Utils.in_ci?
18
+
19
+ # Flaky detection: prepare session with all example IDs
20
+ config.before(:suite) do
21
+ ci = Mergify::RSpec.ci_insights
22
+ fd = ci&.flaky_detector
23
+ if fd
24
+ all_ids = ::RSpec.world.example_groups.flat_map(&:descendants).flat_map(&:examples).map(&:id)
25
+ fd.prepare_for_session(all_ids)
26
+ end
27
+ end
28
+
29
+ # Quarantine: mark tests before execution
30
+ config.before(:each) do |example|
31
+ ci = Mergify::RSpec.ci_insights
32
+ next unless ci&.quarantined_tests&.include?(example.id)
33
+
34
+ ci.quarantined_tests.mark_as_used(example.id)
35
+ example.metadata[:mergify_quarantined] = true
36
+ end
37
+
38
+ # Flaky detection: rerun tests within budget
39
+ config.around(:each) do |example|
40
+ ci = Mergify::RSpec.ci_insights
41
+ fd = ci&.flaky_detector
42
+
43
+ example.run
44
+
45
+ # Feed metrics from the initial run so the detector can evaluate
46
+ if fd
47
+ run_time = example.execution_result.run_time || 0.0
48
+ status = example.execution_result.status
49
+ fd.fill_metrics_from_report(example.id, 'setup', 0.0, status)
50
+ fd.fill_metrics_from_report(example.id, 'call', run_time, status)
51
+ fd.fill_metrics_from_report(example.id, 'teardown', 0.0, status)
52
+ end
53
+
54
+ next unless fd&.rerunning_test?(example.id)
55
+
56
+ # Mark as flaky detection candidate (even if too slow to rerun)
57
+ example.metadata[:mergify_flaky_detection] = true
58
+ example.metadata[:mergify_new_test] = true if fd.mode == 'new'
59
+
60
+ fd.set_test_deadline(example.id)
61
+ next if fd.test_too_slow?(example.id)
62
+
63
+ distinct_outcomes = Set.new
64
+ distinct_outcomes.add(example.execution_result.status) if example.execution_result.status
65
+
66
+ rerun_count = 0
67
+ # Use a null reporter to prevent reruns from being reported as
68
+ # individual failures. The final outcome is set after the loop.
69
+ null_reporter = ::RSpec::Core::NullReporter
70
+
71
+ until example.metadata[:is_last_rerun]
72
+ example.metadata[:is_last_rerun] = fd.last_rerun_for_test?(example.id)
73
+
74
+ # Reset example state for rerun
75
+ example.instance_variable_set(:@exception, nil)
76
+ if example.example_group_instance
77
+ memoized = example.example_group_instance.instance_variable_get(:@__memoized)
78
+ if memoized.respond_to?(:clear)
79
+ memoized.clear
80
+ elsif memoized
81
+ # RSpec >= 3.12 uses ThreadsafeMemoized which wraps an internal hash
82
+ inner = memoized.instance_variable_get(:@memoized)
83
+ inner&.clear
84
+ end
85
+ end
86
+
87
+ example.run(example.example_group_instance, null_reporter)
88
+
89
+ # Feed rerun metrics so budget/deadline tracking stays accurate
90
+ rerun_time = example.execution_result.run_time || 0.0
91
+ rerun_status = example.execution_result.status
92
+ fd.fill_metrics_from_report(example.id, 'call', rerun_time, rerun_status)
93
+
94
+ distinct_outcomes.add(rerun_status)
95
+ rerun_count += 1
96
+ end
97
+
98
+ is_flaky = distinct_outcomes.include?(:passed) &&
99
+ distinct_outcomes.include?(:failed)
100
+ example.metadata[:mergify_flaky] = true if is_flaky
101
+ example.metadata[:mergify_rerun_count] = rerun_count
102
+ end
103
+
104
+ # Quarantine: override failed quarantined test results
105
+ config.after(:each) do |example|
106
+ next unless example.metadata[:mergify_quarantined] && example.exception
107
+
108
+ example.instance_variable_set(:@exception, nil)
109
+ example.execution_result.status = :pending
110
+ example.execution_result.pending_message = 'Test is quarantined from Mergify Test Insights'
111
+ end
112
+ end
113
+ end
114
+ # rubocop:enable Metrics/MethodLength,Metrics/BlockLength,Metrics/AbcSize
115
+ end
116
+ end
117
+ end