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

40 lines
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-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
end
end