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

104 outsource the git layer to gitlab git #120

Merged
merged 5 commits into from
Jun 6, 2017
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
20 changes: 10 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
GIT
remote: git://github.com/ontohub/gitlab_git.git
revision: 6f0c6f0afb65fc69e688865a51e70b1b68bf4c76
revision: 231a1a847ef7746dbacaee533b96fab96084dc0f
branch: master
specs:
gitlab_git (10.7.0)
gitlab_git (11.0.0)
activesupport (>= 4.0)
charlock_holmes (~> 0.7.3)
github-linguist (~> 4.7.0)
rugged (~> 0.24.0)
github-linguist (~> 5.0.10)
rugged (~> 0.25.1)

GIT
remote: git://github.com/ontohub/ontohub-models.git
Expand Down Expand Up @@ -128,11 +128,11 @@ GEM
faraday (0.12.1)
multipart-post (>= 1.2, < 3)
ffi (1.9.18)
github-linguist (4.7.6)
github-linguist (5.0.11)
charlock_holmes (~> 0.7.3)
escape_utils (~> 1.1.0)
mime-types (>= 1.19)
rugged (>= 0.23.0b)
rugged (>= 0.25.1)
globalid (0.4.0)
activesupport (>= 4.2.0)
i18n (0.8.4)
Expand All @@ -154,13 +154,13 @@ GEM
mime-types (3.1)
mime-types-data (~> 3.2015)
mime-types-data (3.2016.0521)
mini_portile2 (2.1.0)
mini_portile2 (2.2.0)
minitest (5.10.2)
multi_json (1.12.1)
multipart-post (2.0.0)
nio4r (2.0.0)
nokogiri (1.7.2)
mini_portile2 (~> 2.1.0)
nokogiri (1.8.0)
mini_portile2 (~> 2.2.0)
orm_adapter (0.5.0)
orm_adapter-sequel (0.1.0)
activemodel (>= 3.0.0)
Expand Down Expand Up @@ -242,7 +242,7 @@ GEM
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
ruby_dep (1.5.0)
rugged (0.24.6.1)
rugged (0.25.1.1)
sequel (4.42.1)
sequel-devise (0.0.9)
devise
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/v2/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def destroy

protected

def render_resource(status = :ok)
def render_resource(status = :ok, serializer: resource_serializer)
render status: status,
json: resource,
serializer: resource_serializer,
serializer: serializer,
include: permitted_includes
end

Expand Down
6 changes: 6 additions & 0 deletions app/controllers/v2/resources_controller/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ def collection
klass = self.class.instance_variable_get(:@resource_class)
@collection = klass.where(parent_params)
if @collection.respond_to?(:all)
# :nocov:
# This is not yet used. Remove nocov as soon as it's used
@collection = @collection.all
# :nocov:
else
@collection
end
Expand Down Expand Up @@ -122,9 +125,12 @@ def actions_to_remove(to_keep)

def permitted_params_options(params, opts)
if params.last.is_a?(Hash)
# :nocov:
# This is not yet used. Remove nocov as soon as it's used
opts.merge(params.pop.map do |action, action_params|
[action, Array(action_params).map(&:to_sym)]
end&.to_h)
# :nocov:
end
opts
end
Expand Down
35 changes: 26 additions & 9 deletions app/controllers/v2/trees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ def destroy
render_error(:unprocessable_entity)
end

def multiaction
@resource = MultiBlob.new(files: params[:data][:attributes][:files],
commit_message:
resource_params[:commit_message],
branch: ref,
repository: repository,
user: current_user)
render_resource(:ok, serializer: V2::MultiBlobSerializer) if resource.save
rescue MultiBlob::ValidationFailed
render_error(:unprocessable_entity)
end

protected

def repository
Expand All @@ -69,9 +81,10 @@ def resource_tree
end

def resource
@resource ||= Blob.find(repository_id: repository.to_param,
branch: ref,
path: params[:path])
return @resource if @resource
@resource = Blob.find(repository_id: repository.to_param,
branch: ref,
path: params[:path])
@resource&.user = current_user
@resource
end
Expand All @@ -91,13 +104,17 @@ def update_resource
repository: repository,
user: current_user}
# Only for moving the file
attributes[:path] = resource_params[:path] if resource_params[:path]
# Only if changing the content
if resource_params[:content]
attributes[:content] = resource_params[:content]
attributes[:encoding] = resource_params[:encoding]
if resource_params[:path]
attributes[:path] = resource_params[:path]
attributes[:previous_path] = params[:path]
else
attributes[:path] = params[:path]
attributes[:previous_path] = nil
end
attributes[:previous_path] = params[:path]
# Only if changing the content
attributes[:content] = resource_params[:content]
attributes[:encoding] = resource_params[:encoding]

resource.update(attributes)
resource.save
end
Expand Down
140 changes: 43 additions & 97 deletions app/models/blob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,29 @@ def create

def destroy
self.commit_message = attributes[:commit_message] = "Delete #{path}."
git.remove_file(commit_info, previous_head_sha)
rescue Gitlab::Git::Repository::InvalidRef
@errors.add(:branch, "branch does not exist: #{branch}")
raise ValidationFailed
multi_blob = MultiBlob.new(multi_blob_params('remove'))
multi_blob.save
rescue MultiBlob::ValidationFailed
convert_errors_and_raise(multi_blob)
end

def save(mode: nil)
raise ValidationFailed, @errors.messages.to_json unless valid?
commit_sha =
multi_blob =
if mode == :create
MultiBlob.new(multi_blob_params('create'))
elsif rename_file? && content.nil?
params = multi_blob_params('rename')
reset_content
MultiBlob.new(params)
else
MultiBlob.new(multi_blob_params('update'))
end
self.commit_id =
begin
if rename_file?
git.rename_file(commit_info, previous_head_sha)
elsif mode == :create
git.create_file(commit_info, previous_head_sha)
else
git.update_file(commit_info, previous_head_sha)
end
rescue Git::Committing::HeadChangedError
@errors.add(:branch,
'Could not save the file in the git repository '\
'because it has changed in the meantime. '\
'Please try again after checking out the current revision.')
raise ValidationFailed
multi_blob.save
rescue MultiBlob::ValidationFailed
convert_errors_and_raise(multi_blob)
end
self.commit_id = commit_sha
create_file_version(commit_sha)

changes_applied

Expand All @@ -121,92 +118,41 @@ def url_path
protected

def rename_file?
previous_path.present? && previous_path != path
end

# rubocop:disable Style/IfUnlessModifier
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/PerceivedComplexity
def valid?(mode = :save)
return @errors.blank? if @validated
case mode
when :create
if !git&.empty? && git&.branch_exists?(branch) &&
git&.path_exists?(branch, path)
@errors.add(:path, "path already exists: #{path}")
end
when :update
if !content_changed? && !path_changed?
@errors.add(:content, 'either content or path must be changed')
end
end
unless repository.is_a?(RepositoryCompound)
@errors.add(:repository, 'repository must be set')
end
if user.blank?
@errors.add(:user, 'No user set')
end
if !git&.empty? && !git&.branch_exists?(branch)
@errors.add(:branch, "branch does not exist: #{branch}")
end
unless ENCODINGS.include?(encoding)
@errors.add(:encoding, "encoding not supported: #{encoding}. "\
"Must be one of #{ENCODINGS.join(',')}")
end
if content.nil?
@errors.add(:content, 'content must exist')
end
unless content.is_a?(String)
@errors.add(:content, 'content must be a string')
end
unless commit_message.present?
@errors.add(:commit_message, 'commit_message is not present')
end
@validated = true
@errors.blank?
!previous_path.nil?
end
# rubocop:enable Style/IfUnlessModifier
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity

def reset_validation_state
@errors.clear
@validated = false
end

def decoded_content
@decoded_content ||=
case encoding
when 'base64'
Base64.decode64(content)
when 'plain'
content
end
def reset_content
return unless content.nil?
blob = repository.git.blob(branch, previous_path)
self.content = blob.data
self.encoding = blob.binary ? 'base64' : 'plain'
end

def create_file_version(commit_sha)
file_version =
FileVersion.new(path: path,
commit_sha: commit_sha,
repository_id: repository.pk,
url_path_method: ->(_file_version) { url_path })
file_version.save
file_version
def multi_blob_params(mutli_blob_action)
params = {files: [{path: path,
content: content,
encoding: encoding,
action: mutli_blob_action}],
previous_head_sha: previous_head_sha,
commit_message: commit_message,
branch: branch,
repository: repository,
user: user}
params[:files].first[:previous_path] = previous_path if rename_file?
params
end

def commit_info
now = Time.now
@commit_info ||= {file: {content: decoded_content, path: path},
author: user_info(now),
committer: user_info(now),
commit: {message: commit_message, branch: branch}}
@commit_info[:file][:previous_path] = previous_path if rename_file?
@commit_info
end

def user_info(time = nil)
{email: user.email,
name: user.display_name || user.slug,
time: time || Time.now}
def convert_errors_and_raise(multi_blob)
multi_blob.errors.messages.each do |attribute, messages|
messages.each do |message|
@errors.add(attribute.to_s.sub('files/0/', ''), message)
end
end
raise ValidationFailed, @errors.messages.to_json
end
end
Loading