Skip to content

Commit

Permalink
Initialize active workspace in updater
Browse files Browse the repository at this point in the history
Build a workspace for the in-flight job and set Dependabot::Workspace.active_workspace if the :shared_workspace experiment is enabled.
  • Loading branch information
dependabot-ci committed Mar 29, 2023
1 parent 036fdfe commit 8aaca08
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 4 deletions.
1 change: 1 addition & 0 deletions updater/lib/dependabot/updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
require "dependabot/updater/operations"
require "dependabot/security_advisory"
require "dependabot/update_checkers"
require "dependabot/workspace"
require "wildcard_matcher"

# rubocop:disable Metrics/ClassLength
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def perform
# We should log the rule being executed, let's just hard-code wildcard for now
# since the prototype makes best-effort to do everything in one pass.
Dependabot.logger.info("Starting update group for '#{GROUP_NAME_PLACEHOLDER}'")
prepare_workspace
dependency_change = compile_dependency_change

if dependency_change.dependencies.any?
Expand Down Expand Up @@ -362,6 +363,17 @@ def generate_dependency_files_for(lead_dependency, updated_dependencies, current
error_handler.handle_dependabot_error(error: e, dependency: lead_dependency)
current_dependency_files # return the files unchanged
end

def prepare_workspace
return unless Dependabot::Experiments.enabled?(:shared_workspace)
return unless job.clone?
return if job.repo_contents_path.nil?

Dependabot::Workspace.active_workspace = Dependabot::Workspace::Git.new(
job.repo_contents_path,
Pathname.new(job.source.directory || "/").cleanpath
)
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions updater/lib/dependabot/updater/operations/update_all_versions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def check_and_create_pr_with_error_handling(dependency)
# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def check_and_create_pull_request(dependency)
prepare_workspace
checker = update_checker_for(dependency, raise_on_ignored: raise_on_ignored?(dependency))

log_checking_for_update(dependency)
Expand Down Expand Up @@ -351,6 +352,22 @@ def create_pull_request(dependencies, updated_dependency_files)
}.compact
end
end

def prepare_workspace
if Dependabot::Workspace.active_workspace
Dependabot::Workspace.active_workspace.reset!
return
end

return unless Dependabot::Experiments.enabled?(:shared_workspace)
return unless job.clone?
return if job.repo_contents_path.nil?

Dependabot::Workspace.active_workspace = Dependabot::Workspace::Git.new(
job.repo_contents_path,
Pathname.new(job.source.directory || "/").cleanpath
)
end
end
end
end
Expand Down
84 changes: 80 additions & 4 deletions updater/spec/dependabot/updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,81 @@ def expect_update_checker_with_ignored_versions(versions)
end
end

describe "#run with :shared_workspace experiment enabled" do
let(:repo_contents_path) do
tmp = Dir.mktmpdir("dependabot_")
tmp_path = Pathname.new(tmp).expand_path
FileUtils.mkdir_p(tmp_path)
tmp_path.to_s
end

after { FileUtils.rm_rf(repo_contents_path) }

it "creates a workspace for the job" do
dependency_files = default_dependency_files

Dir.chdir(repo_contents_path) do
`git init .`
dependency_files.each do |f|
File.write(f.name, f.content)
`git add #{f.name}`
end
`git commit -m init`
end

job = build_job(
experiments: { "shared-workspace" => true },
repo_contents_path: repo_contents_path
)
allow(job).to receive(:clone?).and_return(true)

service = build_service
updater = build_updater(service: service, job: job, dependency_files: dependency_files)

workspace = Dependabot::Workspace::Git.new(repo_contents_path, job.source.directory)
allow(Dependabot::Workspace::Git).to receive(:new).and_return(workspace)
expect(Dependabot::Workspace).to receive(:active_workspace=).with(workspace).and_call_original

updater.run

expect(Dependabot::Workspace.active_workspace).to eq(workspace)
end

context "for a job without a #repo_contents_path" do
it "does not create a workspace for the job" do
job = build_job(
experiments: { "shared-workspace" => true },
repo_contents_path: nil
)
allow(job).to receive(:clone?).and_return(true)

service = build_service
updater = build_updater(service: service, job: job)

expect(Dependabot::Workspace).not_to receive(:active_workspace=)

updater.run
end
end

context "for a job that is not #clone?" do
it "does not create a workspace for the job" do
job = build_job(
experiments: { "shared-workspace" => true },
repo_contents_path: nil
)
allow(job).to receive(:clone?).and_return(false)

service = build_service
updater = build_updater(service: service, job: job)

expect(Dependabot::Workspace).not_to receive(:active_workspace=)

updater.run
end
end
end

def build_updater(service: build_service, job: build_job, dependency_files: default_dependency_files)
Dependabot::Updater.new(
service: service,
Expand All @@ -2242,12 +2317,12 @@ def default_dependency_files
[
Dependabot::DependencyFile.new(
name: "Gemfile",
content: fixture("bundler/original/Gemfile"),
content: fixture("bundler2/original/Gemfile"),
directory: "/"
),
Dependabot::DependencyFile.new(
name: "Gemfile.lock",
content: fixture("bundler/original/Gemfile.lock"),
content: fixture("bundler2/original/Gemfile.lock"),
directory: "/"
)
]
Expand Down Expand Up @@ -2275,7 +2350,8 @@ def build_service

def build_job(requested_dependencies: nil, allowed_updates: default_allowed_updates, # rubocop:disable Metrics/MethodLength
existing_pull_requests: [], ignore_conditions: [], security_advisories: [],
experiments: {}, updating_a_pull_request: false, security_updates_only: false)
experiments: {}, updating_a_pull_request: false, security_updates_only: false,
repo_contents_path: nil)
Dependabot::Job.new(
id: 1,
token: "token",
Expand Down Expand Up @@ -2317,7 +2393,7 @@ def build_job(requested_dependencies: nil, allowed_updates: default_allowed_upda
"include-scope" => true
},
security_updates_only: security_updates_only,
repo_contents_path: nil
repo_contents_path: repo_contents_path
)
end

Expand Down

0 comments on commit 8aaca08

Please sign in to comment.