Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new interface to register experiments #5755

Merged
merged 2 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions bin/dry-run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@
[o.strip.downcase.to_sym, true]
end
end

$options[:updater_options].each do |name, val|
Dependabot::Experiment.register(name, val)
end
Comment on lines +228 to +230
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use :updater_options for more than experiments? Maybe it's not such a big deal for dry-run, but the name doesn't really fit the purpose.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah we use a few different names in different places, we ended up with an options arg for the FileUpdater with the idea that it could be used for more things than just experiments. I'm not sure that we ever have, and going forward we should probably rename things where it makes sense, but I was aiming to keep the change as small as possible

end

opts.on("--security-updates-only",
Expand Down
19 changes: 19 additions & 0 deletions common/lib/dependabot/experiments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module Dependabot
module Experiments
@experiments = {}

def self.reset!
@experiments = {}
end

def self.register(name, value)
@experiments[name.to_sym] = value
end

def self.enabled?(name)
!!@experiments[name.to_sym]
end
end
end
23 changes: 23 additions & 0 deletions common/spec/dependabot/experiments_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

require "spec_helper"
require "dependabot/experiments"

RSpec.describe Dependabot::Experiments do
before do
described_class.reset!
end

it "can register experiments as enabled" do
described_class.register(:my_test, true)

expect(described_class.enabled?(:my_test)).to be_truthy
end

it "works with string names and symbols" do
described_class.register("my_test", true)

expect(described_class.enabled?("my_test")).to be_truthy
expect(described_class.enabled?(:my_test)).to be_truthy
end
end
4 changes: 4 additions & 0 deletions common/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
require "uri"

require "dependabot/dependency_file"
require "dependabot/experiments"
require "dependabot/registry_client"
require_relative "dummy_package_manager/dummy"
require_relative "warning_monkey_patch"
Expand Down Expand Up @@ -43,6 +44,9 @@
config.after do
# Ensure we clear any cached timeouts between tests
Dependabot::RegistryClient.clear_cache!

# Ensure we reset any experiments between tests
Dependabot::Experiments.reset!
end

config.around do |example|
Expand Down
3 changes: 2 additions & 1 deletion docker/lib/dependabot/docker/file_fetcher.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

require "dependabot/experiments"
require "dependabot/file_fetchers"
require "dependabot/file_fetchers/base"

Expand All @@ -21,7 +22,7 @@ def self.required_files_message
private

def kubernetes_enabled?
options.key?(:kubernetes_updates) && options[:kubernetes_updates]
Experiments.enabled?(:kubernetes_updates)
end

def fetch_files
Expand Down
16 changes: 10 additions & 6 deletions docker/spec/dependabot/docker/file_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
described_class.new(
source: source,
credentials: credentials,
repo_contents_path: nil,
options: options
repo_contents_path: nil
)
end
let(:options) { {} }
let(:directory) { "/" }
let(:github_url) { "https://api.github.com/" }
let(:url) { github_url + "repos/gocardless/bump/contents/" }
Expand Down Expand Up @@ -184,7 +182,9 @@
end

let(:kubernetes_fixture) { fixture("github", "contents_kubernetes.json") }
let(:options) { { kubernetes_updates: true } }
before do
Dependabot::Experiments.register(:kubernetes_updates, true)
end

it "fetches the pod.yaml" do
expect(file_fetcher_instance.files.count).to eq(1)
Expand All @@ -211,7 +211,9 @@
end

context "with kubernetes not enabled" do
let(:options) { { kubernetes_updates: false } }
before do
Dependabot::Experiments.register(:kubernetes_updates, false)
end

it "raises a helpful error" do
expect { file_fetcher_instance.files }.
Expand Down Expand Up @@ -247,7 +249,9 @@

let(:kubernetes_fixture) { fixture("github", "contents_kubernetes.json") }
let(:kubernetes_2_fixture) { fixture("github", "contents_kubernetes.json") }
let(:options) { { kubernetes_updates: true } }
before do
Dependabot::Experiments.register(:kubernetes_updates, true)
end

it "fetches both YAMLs" do
expect(file_fetcher_instance.files.count).to eq(2)
Expand Down
4 changes: 4 additions & 0 deletions updater/lib/dependabot/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def initialize(service:, job_id:, job:, dependency_files:,
def run
return unless job

job.experiments.each do |name, value|
Dependabot::Experiments.register(name, value)
end

if job.updating_a_pull_request?
logger_info("Starting PR update job for #{job.source.repo}")
check_and_update_existing_pr_with_error_handling(dependencies)
Expand Down