Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Nishnha committed Jun 26, 2023
2 parents c645d88 + b70130d commit a56bdcd
Show file tree
Hide file tree
Showing 31 changed files with 914 additions and 220 deletions.
27 changes: 14 additions & 13 deletions common/lib/dependabot/shared_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@ module SharedHelpers
"(+https://github.com/dependabot/dependabot-core)"
SIGKILL = 9

def self.in_a_temporary_repo_directory(directory = "/",
repo_contents_path = nil,
&block)
def self.in_a_temporary_repo_directory(directory = "/", repo_contents_path = nil, &block)
if repo_contents_path
path = Pathname.new(File.join(repo_contents_path, directory)).expand_path
reset_git_repo(repo_contents_path)
# Handle missing directories by creating an empty one and relying on the
# file fetcher to raise a DependencyFileNotFound error
FileUtils.mkdir_p(path)

if (workspace = Dependabot::Workspace.active_workspace)
return workspace.change(&block)
# If a workspace has been defined to allow orcestration of the git repo
# by the runtime we should defer to it, otherwise we prepare the folder
# for direct use and yield.
if Dependabot::Workspace.active_workspace
Dependabot::Workspace.active_workspace.change(&block)
else
path = Pathname.new(File.join(repo_contents_path, directory)).expand_path
reset_git_repo(repo_contents_path)
# Handle missing directories by creating an empty one and relying on the
# file fetcher to raise a DependencyFileNotFound error
FileUtils.mkdir_p(path)

Dir.chdir(path) { yield(path) }
end

Dir.chdir(path) { yield(path) }
else
in_a_temporary_directory(directory, &block)
end
Expand Down
28 changes: 28 additions & 0 deletions common/lib/dependabot/workspace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,33 @@ module Workspace
class << self
attr_accessor :active_workspace
end

def self.setup(repo_contents_path:, directory:)
Dependabot.logger.debug("Setting up workspace in #{repo_contents_path}")

full_path = Pathname.new(File.join(repo_contents_path, directory)).expand_path
# Handle missing directories by creating an empty one and relying on the
# file fetcher to raise a DependencyFileNotFound error
FileUtils.mkdir_p(full_path)

@active_workspace = Dependabot::Workspace::Git.new(full_path)
end

def self.store_change(memo:)
return unless @active_workspace

Dependabot.logger.debug("Storing change to workspace: #{memo}")

@active_workspace.store_change(memo)
end

def self.cleanup!
return unless @active_workspace

Dependabot.logger.debug("Cleaning up current workspace")

@active_workspace.reset!
@active_workspace = nil
end
end
end
12 changes: 4 additions & 8 deletions common/lib/dependabot/workspace/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,15 @@ def failed_change_attempts
end

def change(memo = nil)
change_attempt = nil
Dir.chdir(path) { yield(path) }
change_attempt = capture_change(memo)
rescue StandardError => e
change_attempt = capture_failed_change_attempt(memo, e)
capture_failed_change_attempt(memo, e)
clean # clean up any failed changes
raise e
ensure
change_attempts << change_attempt unless change_attempt.nil?
clean
end

def store_change(memo = nil); end

def to_patch
""
end
Expand All @@ -42,8 +40,6 @@ def reset!; end

protected

def capture_change(memo = nil); end

def capture_failed_change_attempt(memo = nil, error = nil); end
end
end
Expand Down
52 changes: 41 additions & 11 deletions common/lib/dependabot/workspace/git.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
module Dependabot
module Workspace
class Git < Base
USER = "dependabot[bot]"
EMAIL = "#{USER}@users.noreply.github.com"

attr_reader :initial_head_sha

def initialize(repo_contents_path, directory = "/")
super(Pathname.new(File.join(repo_contents_path, directory)).expand_path)
def initialize(path)
super(path)
@initial_head_sha = head_sha
configure_git
end

def changed?
changes.any? || !changed_files.empty?
end

def to_patch
Expand All @@ -26,27 +34,33 @@ def reset!
nil
end

protected

def capture_change(memo = nil)
changed_files = run_shell_command("git status --short .").strip
def store_change(memo = nil)
return nil if changed_files.empty?

debug("store_change - before: #{current_commit}")
sha, diff = commit(memo)
ChangeAttempt.new(self, id: sha, memo: memo, diff: diff)

change_attempts << ChangeAttempt.new(self, id: sha, memo: memo, diff: diff)
ensure
debug("store_change - after: #{current_commit}")
end

protected

def capture_failed_change_attempt(memo = nil, error = nil)
changed_files =
run_shell_command("git status --untracked-files=all --ignored=matching --short .").strip
return nil if changed_files.nil? && error.nil?
return nil if changed_files(ignored_mode: "matching").empty? && error.nil?

sha, diff = stash(memo)
ChangeAttempt.new(self, id: sha, memo: memo, diff: diff, error: error)
change_attempts << ChangeAttempt.new(self, id: sha, memo: memo, diff: diff, error: error)
end

private

def configure_git
run_shell_command(%(git config user.name "#{USER}"), allow_unsafe_shell_command: true)
run_shell_command(%(git config user.email "#{EMAIL}"), allow_unsafe_shell_command: true)
end

def head_sha
run_shell_command("git rev-parse HEAD").strip
end
Expand All @@ -55,6 +69,18 @@ def last_stash_sha
run_shell_command("git rev-parse refs/stash").strip
end

def current_commit
# Avoid emiting the user's commit message to logs if Dependabot hasn't made any changes
return "Initial SHA: #{initial_head_sha}" if changes.empty?

# Prints out the last commit in the format "<short-ref> <commit-message>"
run_shell_command(%(git log -1 --pretty="%h% B"), allow_unsafe_shell_command: true).strip
end

def changed_files(ignored_mode: "traditional")
run_shell_command("git status --untracked-files=all --ignored=#{ignored_mode} --short .").strip
end

def stash(memo = nil)
msg = memo || "workspace change attempt"
run_shell_command("git add --all --force .")
Expand Down Expand Up @@ -87,6 +113,10 @@ def clean
def run_shell_command(*args, **kwargs)
Dir.chdir(path) { SharedHelpers.run_shell_command(*args, **kwargs) }
end

def debug(message)
Dependabot.logger.debug("[workspace] #{message}")
end
end
end
end
3 changes: 2 additions & 1 deletion common/spec/dependabot/shared_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def existing_tmp_folders
context "when there is an active workspace" do
let(:project_name) { "simple" }
let(:repo_contents_path) { build_tmp_repo(project_name, tmp_dir_path: Dir.tmpdir) }
let(:workspace) { Dependabot::Workspace::Git.new(repo_contents_path, directory) }
let(:workspace_path) { Pathname.new(File.join(repo_contents_path, directory)).expand_path }
let(:workspace) { Dependabot::Workspace::Git.new(workspace_path) }

before do
allow(Dependabot::Workspace).to receive(:active_workspace).and_return(workspace)
Expand Down
Loading

0 comments on commit a56bdcd

Please sign in to comment.