forgeplus/app/services/gitea/repository/entries/get_service.rb

41 lines
1.1 KiB
Ruby
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

class Gitea::Repository::Entries::GetService < Gitea::ClientService
attr_reader :user, :repo_name, :filepath, :args
# ref: The name of the commit/branch/tag. Default the repositorys default branch (usually master)
# filepath: path of the dir, file, symlink or submodule in the repo
# repo_name: the name of repository
# ref: The name of the commit/branch/tag. Default the repositorys default branch (usually master)
def initialize(user, repo_name, filepath, **args)
@user = user
@repo_name = repo_name
@filepath = filepath
@args = {ref: 'master'}.merge(args.compact)
end
def call
response = get(url, params)
render_result(response)
end
private
def params
@args.merge(token: user.gitea_token)
end
def url
"/repos/#{user.login}/#{repo_name}/contents/#{filepath}".freeze
end
def render_result(response)
body = JSON.parse(response.body)
case response.status
when 200
body
when 404
raise '你访问的文件不存在'
else
raise body['message']
end
end
end