parent
a74e51dc73
commit
8398324366
@ -1,16 +1,43 @@
|
||||
class RepositoriesController < ApplicationController
|
||||
before_action :git_base
|
||||
include ApplicationHelper
|
||||
before_action :find_user, :find_repository, :authorizate!
|
||||
|
||||
def show
|
||||
repo_path = Shixun.find_by_identifier(params[:identifier]).try(:repo_name)
|
||||
{repo_path: repo_path, path: params[:path]}
|
||||
@entries = @git.file_tree(params)
|
||||
def entries
|
||||
@entries = Gitea::Repository::Entries::ListService.new(@user, @repo.identifier, ref: params[:ref]).call
|
||||
end
|
||||
|
||||
def sub_entries
|
||||
interactor = Repositories::EntriesInteractor.call(@user, @repo.identifier, params[:filepath], ref: params[:ref])
|
||||
if interactor.success?
|
||||
@sub_entries = interactor.result
|
||||
else
|
||||
render_error(interactor.error)
|
||||
end
|
||||
end
|
||||
|
||||
def commits
|
||||
@hash_commit = Gitea::Repository::Commits::ListService.new(@user, @repo.identifier, sha: params[:sha], page: params[:page]).call
|
||||
end
|
||||
|
||||
def single_commit
|
||||
@commit = Gitea::Repository::Commits::GetService.new(@user, @repo.identifier, params[:sha]).call
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def git_base
|
||||
@shixun = Shixun.find_by_identifier(params[:shixun_identifier])
|
||||
@repo_namespace = @shixun.repo_path
|
||||
def find_repository
|
||||
@repo = @user.repositories.find_by_identifier params[:repo_identifier]
|
||||
render_not_found unless @repo
|
||||
end
|
||||
|
||||
def find_user
|
||||
@user = User.find_by_login params[:login]
|
||||
render_not_found unless @user
|
||||
end
|
||||
|
||||
def authorizate!
|
||||
if @repo.hidden? && @repo.user != current_user
|
||||
render_forbidden
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -0,0 +1,5 @@
|
||||
class Repositories::SearchSubEntriesForm < BaseForm
|
||||
attr_accessor :filepath, :login, :repo_identifier
|
||||
|
||||
validates :filepath, :login, :repo_identifier, presence: true
|
||||
end
|
@ -0,0 +1,47 @@
|
||||
module Repositories
|
||||
class EntriesInteractor
|
||||
def self.call(user, identifier, filepath, **args)
|
||||
interactor = new(user, identifier, filepath, **args)
|
||||
interactor.run
|
||||
interactor
|
||||
end
|
||||
|
||||
attr_reader :error, :result
|
||||
|
||||
def initialize(user, identifier, filepath, **args)
|
||||
@user = user
|
||||
@identifier = identifier
|
||||
@filepath = filepath
|
||||
@args = args
|
||||
end
|
||||
|
||||
def success?
|
||||
@error.nil?
|
||||
end
|
||||
|
||||
def result
|
||||
@result
|
||||
end
|
||||
|
||||
def run
|
||||
Repositories::SearchSubEntriesForm.new({login: user.login, repo_identifier: identifier, filepath: filepath}).validate!
|
||||
sub_entries = Gitea::Repository::Entries::GetService.new(@user, @identifier, @filepath, @args).call
|
||||
render_result(sub_entries)
|
||||
rescue Exception => exception
|
||||
fail!(exception.message)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
attr_reader :user, :identifier, :filepath, :args
|
||||
|
||||
def fail!(error)
|
||||
@error = error
|
||||
end
|
||||
|
||||
def render_result(response)
|
||||
@result = response
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,35 @@
|
||||
# Get a single commit from a repository
|
||||
class Gitea::Repository::Commits::GetService < Gitea::ClientService
|
||||
attr_reader :user, :repo_name, :sha
|
||||
|
||||
# sha: the commit hash
|
||||
def initialize(user, repo_name, sha)
|
||||
@user = user
|
||||
@sha = sha
|
||||
@repo_name = repo_name
|
||||
end
|
||||
|
||||
def call
|
||||
response = get(url, params)
|
||||
render_result(response)
|
||||
end
|
||||
|
||||
private
|
||||
def params
|
||||
Hash.new.merge(token: user.gitea_token)
|
||||
end
|
||||
|
||||
def url
|
||||
"/repos/#{user.login}/#{repo_name}/git/commits/#{sha}".freeze
|
||||
end
|
||||
|
||||
def render_result(response)
|
||||
body = JSON.parse(response.body)
|
||||
case response.status
|
||||
when 200
|
||||
JSON.parse(response.body)
|
||||
else
|
||||
{status: -1, message: "#{body['message']}"}
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,39 @@
|
||||
# Get a list of all commits from a repository
|
||||
class Gitea::Repository::Commits::ListService < Gitea::ClientService
|
||||
attr_reader :user, :repo_name, :args
|
||||
|
||||
# sha: SHA or branch to start listing commits from (usually 'master')
|
||||
def initialize(user, repo_name, **args)
|
||||
@user = user
|
||||
@repo_name = repo_name
|
||||
@args = { sha: 'master', page: 1 }.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}/commits".freeze
|
||||
end
|
||||
|
||||
def render_result(response)
|
||||
body = JSON.parse(response.body)
|
||||
case response.status
|
||||
when 200
|
||||
result = {}
|
||||
headers = response.headers.to_hash
|
||||
body = JSON.parse(response.body)
|
||||
total_count = headers["x-total"]
|
||||
result.merge(total_count: total_count.to_i, body: body)
|
||||
else
|
||||
{status: -1, message: "#{body['message']}"}
|
||||
end
|
||||
end
|
||||
end
|
@ -1,3 +1,17 @@
|
||||
json.array! @branches do |branch|
|
||||
json.name branch['name']
|
||||
json.user_can_push branch['user_can_push']
|
||||
json.user_can_merge branch['user_can_merge']
|
||||
json.protected branch['protected']
|
||||
json.last_commit do
|
||||
json.id branch['commit']['id']
|
||||
json.message branch['commit']['message']
|
||||
json.timestamp render_unix_time(branch['commit']['timestamp'])
|
||||
json.time_from_now time_from_now(branch['commit']['timestamp'])
|
||||
end
|
||||
json.author do
|
||||
user = User.find_by_login branch['commit']['author']['name']
|
||||
json.login user.login
|
||||
json.image_url url_to_avatar(user)
|
||||
end
|
||||
end
|
||||
|
@ -0,0 +1,4 @@
|
||||
json.author do
|
||||
json.name user.login
|
||||
json.image_url url_to_avatar(user)
|
||||
end
|
@ -0,0 +1,7 @@
|
||||
json.name entry['name']
|
||||
json.sha entry['sha']
|
||||
json.path entry['path']
|
||||
json.type entry['type']
|
||||
json.size entry['size']
|
||||
json.content entry['content']
|
||||
json.target entry['target']
|
@ -0,0 +1,10 @@
|
||||
json.total_count @hash_commit[:total_count]
|
||||
json.commits do
|
||||
json.array! @hash_commit[:body] do |commit|
|
||||
json.sha commit['sha']
|
||||
json.message commit['commit']['message']
|
||||
json.timestamp render_unix_time(commit['commit']['author']['date'])
|
||||
json.time_from_now time_from_now(commit['commit']['author']['date'])
|
||||
json.partial! 'author', user: @repo.user
|
||||
end
|
||||
end
|
@ -0,0 +1,11 @@
|
||||
json.array! @entries do |entry|
|
||||
json.name entry['name']
|
||||
json.path entry['path']
|
||||
json.sha entry['sha']
|
||||
json.type entry['type']
|
||||
json.size entry['size']
|
||||
json.content entry['content']
|
||||
json.target entry['target']
|
||||
json.content entry['content']
|
||||
json.commit entry['commit']
|
||||
end
|
@ -0,0 +1,7 @@
|
||||
if @sub_entries.is_a? Array
|
||||
json.array! @sub_entries do |entry|
|
||||
json.partial! 'repositories/simple_entry', locals: { entry: entry }
|
||||
end
|
||||
else
|
||||
json.partial! 'repositories/simple_entry', locals: { entry: @sub_entries }
|
||||
end
|
Loading…
Reference in new issue