dev_forge
Jasder 5 years ago
parent a74e51dc73
commit 8398324366

@ -1,4 +1,5 @@
class ProjectsController < ApplicationController class ProjectsController < ApplicationController
include ApplicationHelper
before_action :require_login, except: %i[index branches group_type_list] before_action :require_login, except: %i[index branches group_type_list]
before_action :find_project, only: %i[branches] before_action :find_project, only: %i[branches]

@ -1,16 +1,43 @@
class RepositoriesController < ApplicationController class RepositoriesController < ApplicationController
before_action :git_base include ApplicationHelper
before_action :find_user, :find_repository, :authorizate!
def show def entries
repo_path = Shixun.find_by_identifier(params[:identifier]).try(:repo_name) @entries = Gitea::Repository::Entries::ListService.new(@user, @repo.identifier, ref: params[:ref]).call
{repo_path: repo_path, path: params[:path]} end
@entries = @git.file_tree(params)
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 end
private private
def git_base def find_repository
@shixun = Shixun.find_by_identifier(params[:shixun_identifier]) @repo = @user.repositories.find_by_identifier params[:repo_identifier]
@repo_namespace = @shixun.repo_path 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
end end

@ -3,11 +3,6 @@ class UsersController < ApplicationController
before_action :load_user, only: [:show, :homepage_info] before_action :load_user, only: [:show, :homepage_info]
before_action :check_user_exist, only: [:show, :homepage_info] before_action :check_user_exist, only: [:show, :homepage_info]
# 检查是否更新
def system_update
@notice = SystemUpdateNotice.last
end
def show;end def show;end
@ -17,6 +12,11 @@ class UsersController < ApplicationController
render_ok render_ok
end end
def me
@user = current_user
end
# 贴吧获取用户信接口 # 贴吧获取用户信接口
def get_user_info def get_user_info
begin begin

@ -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

@ -4,4 +4,8 @@ class Repository < ApplicationRecord
belongs_to :user belongs_to :user
validates :identifier, presence: true, uniqueness: true validates :identifier, presence: true, uniqueness: true
def to_param
self.identifier.parameterize
end
end end

@ -31,8 +31,14 @@ class Gitea::ClientService < ApplicationService
auth_token = authen_params(params[:token]) auth_token = authen_params(params[:token])
conn(auth_token).get do |req| conn(auth_token).get do |req|
req.url full_url(url) req.url full_url(url)
req.body = params[:data].to_json params.except(:token).each_pair do |key, value|
req.params["#{key}"] = value
end
end end
# response.headers.each do |k,v|
# puts "#{k}:#{v}"
# end #=> 响应头
end end
def delete(url, params={}) def delete(url, params={})

@ -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

@ -0,0 +1,38 @@
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
else
{status: -1, message: "#{body['message']}"}
end
end
end

@ -0,0 +1,35 @@
class Gitea::Repository::Entries::ListService < Gitea::ClientService
attr_reader :user, :repo_name, :args
# ref: The name of the commit/branch/tag. Default the repositorys default branch (usually master)
# repo_name: the name of repository
def initialize(user, repo_name, **args)
@user = user
@repo_name = repo_name
@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".freeze
end
def render_result(response)
body = JSON.parse(response.body)
case response.status
when 200
body
else
{status: -1, message: "#{body['message']}"}
end
end
end

@ -1,3 +1,17 @@
json.array! @branches do |branch| json.array! @branches do |branch|
json.name branch['name'] 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 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

@ -59,7 +59,14 @@ Rails.application.routes.draw do
end end
end end
resources :repositories resources :repositories, path: '/:login/:repo_identifier', only: [:index] do
collection do
get :entries
get :sub_entries
get :commits
get :single_commit
end
end
# resources :memos do # resources :memos do
# member do # member do
@ -129,38 +136,12 @@ Rails.application.routes.draw do
member do member do
get :homepage_info get :homepage_info
end end
get 'me', on: :collection
get :question_banks, on: :collection, to: 'users/question_banks#index'
scope module: :users do
resources :courses, only: [:index]
resources :shixuns, only: [:index]
resources :projects, only: [:index]
resources :subjects, only: [:index]
# hacks :question_banks, only: [:index]
resource :experience_records, only: [:show]
resource :grade_records, only: [:show]
resource :watch, only: [:create, :destroy]
resources :project_packages, only: [:index]
# 私信
resources :private_messages, only: [:index, :create, :destroy]
resources :recent_contacts, only: [:index]
resource :private_message_details, only: [:show]
resource :unread_message_info, only: [:show]
# 视频
resources :videos, only: [:index, :update] do
collection do
get :review
post :batch_publish
post :cancel
end
end
resource :video_auths, only: [:create, :update]
end
resources :repositories do
end
collection do collection do
post :following post :following
post :unfollow post :unfollow
@ -173,6 +154,7 @@ Rails.application.routes.draw do
post :brief_introduction post :brief_introduction
post :attendance post :attendance
get :system_update get :system_update
get :me
resource :trial_apply, only: [:create] resource :trial_apply, only: [:create]
resources :projects, module: :users, only: [] do resources :projects, module: :users, only: [] do

Loading…
Cancel
Save