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 31, 2023
1 parent aa45a43 commit 5d47c09
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 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 @@ -359,6 +360,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
1 change: 1 addition & 0 deletions updater/spec/dependabot/integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "dependabot/update_files_command"

require "dependabot/api_client"
require "dependabot/workspace"

RSpec.describe "Dependabot Updates" do
let(:fetch_files) { Dependabot::FileFetcherCommand.new }
Expand Down
80 changes: 78 additions & 2 deletions updater/spec/dependabot/updater_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2213,6 +2213,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_", Dir.tmpdir)
tmp_path = Pathname.new(tmp).expand_path
FileUtils.mkdir_p(tmp_path.to_s)
tmp_path.to_s
end

let!(:dependency_files) { default_dependency_files }

before do
Dir.chdir(repo_contents_path) do
`git init .`
`git config user.name dependabot-ci`
`git config user.email no-reply@github.com`
dependency_files.each { |f| File.write(f.name, f.content) }
`git add .`
`git commit -m init`
end
end

after { FileUtils.rm_rf(repo_contents_path) }

it "creates a workspace for the job" do
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).at_least(1).times

updater.run
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 Down Expand Up @@ -2261,7 +2336,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 @@ -2303,7 +2379,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 5d47c09

Please sign in to comment.