debian-mirror-gitlab/lib/api/helpers/snippets_helpers.rb

99 lines
3.1 KiB
Ruby
Raw Normal View History

2020-05-24 23:13:21 +05:30
# frozen_string_literal: true
module API
module Helpers
module SnippetsHelpers
2020-07-28 23:09:34 +05:30
extend Grape::API::Helpers
params :raw_file_params do
requires :file_path, type: String, file_path: true, desc: 'The url encoded path to the file, e.g. lib%2Fclass%2Erb'
requires :ref, type: String, desc: 'The name of branch, tag or commit'
end
2020-10-24 23:57:45 +05:30
params :create_file_params do
optional :files, type: Array, desc: 'An array of files' do
requires :file_path, type: String, file_path: true, allow_blank: false, desc: 'The path of a snippet file'
requires :content, type: String, allow_blank: false, desc: 'The content of a snippet file'
end
optional :content, type: String, allow_blank: false, desc: 'The content of a snippet'
given :content do
requires :file_name, type: String, desc: 'The name of a snippet file'
end
mutually_exclusive :files, :content
exactly_one_of :files, :content
end
2020-11-24 15:15:51 +05:30
params :update_file_params do |options|
optional :files, type: Array, desc: 'An array of files to update' do
2022-08-27 11:52:29 +05:30
requires :action,
type: String,
values: SnippetInputAction::ACTIONS.map(&:to_s),
desc: "The type of action to perform on the file, must be one of: #{SnippetInputAction::ACTIONS.join(", ")}"
2020-11-24 15:15:51 +05:30
optional :content, type: String, desc: 'The content of a snippet'
optional :file_path, file_path: true, type: String, desc: 'The file path of a snippet file'
optional :previous_path, file_path: true, type: String, desc: 'The previous path of a snippet file'
end
mutually_exclusive :files, :content
mutually_exclusive :files, :file_name
end
params :minimum_update_params do
at_least_one_of :content, :description, :files, :file_name, :title, :visibility
end
2020-05-24 23:13:21 +05:30
def content_for(snippet)
if snippet.empty_repo?
2020-07-28 23:09:34 +05:30
env['api.format'] = :txt
content_type 'text/plain'
header['Content-Disposition'] = 'attachment'
2020-05-24 23:13:21 +05:30
snippet.content
else
blob = snippet.blobs.first
2020-07-28 23:09:34 +05:30
send_git_blob(blob.repository, blob)
2020-05-24 23:13:21 +05:30
end
end
2020-07-28 23:09:34 +05:30
def file_content_for(snippet)
repo = snippet.repository
commit = repo.commit(params[:ref])
not_found!('Reference') unless commit
blob = repo.blob_at(commit.sha, params[:file_path])
not_found!('File') unless blob
send_git_blob(repo, blob)
end
2020-05-24 23:13:21 +05:30
end
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
def process_create_params(args)
2021-09-30 23:02:18 +05:30
args[:snippet_actions] = args.delete(:files)&.map do |file|
file[:action] = :create
file.symbolize_keys
2020-11-24 15:15:51 +05:30
end
2021-09-30 23:02:18 +05:30
args
2020-11-24 15:15:51 +05:30
end
def process_update_params(args)
2021-09-30 23:02:18 +05:30
args[:snippet_actions] = args.delete(:files)&.map(&:symbolize_keys)
2020-11-24 15:15:51 +05:30
2021-09-30 23:02:18 +05:30
args
2020-11-24 15:15:51 +05:30
end
def validate_params_for_multiple_files(snippet)
return unless params[:content] || params[:file_name]
2021-01-03 14:25:43 +05:30
if snippet.multiple_files?
2020-11-24 15:15:51 +05:30
render_api_error!({ error: _('To update Snippets with multiple files, you must use the `files` parameter') }, 400)
2020-10-24 23:57:45 +05:30
end
end
2020-05-24 23:13:21 +05:30
end
end