ADD get commit diff api
This commit is contained in:
parent
b266085e56
commit
79a97b4a02
|
@ -382,8 +382,8 @@ class ApplicationController < ActionController::Base
|
||||||
|
|
||||||
def current_user
|
def current_user
|
||||||
if Rails.env.development?
|
if Rails.env.development?
|
||||||
User.find(1)
|
User.current = User.find 36480
|
||||||
else
|
else
|
||||||
User.current
|
User.current
|
||||||
end
|
end
|
||||||
# User.current
|
# User.current
|
||||||
|
@ -710,6 +710,10 @@ class ApplicationController < ActionController::Base
|
||||||
render_not_found("未找到’#{params[:repo_identifier]}’相关的项目") unless @repo
|
render_not_found("未找到’#{params[:repo_identifier]}’相关的项目") unless @repo
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_repository_by_id
|
||||||
|
@repo = Repository.find params[:id]
|
||||||
|
end
|
||||||
|
|
||||||
def find_project
|
def find_project
|
||||||
project_id = params[:project_id] ? params[:project_id] : params[:id]
|
project_id = params[:project_id] ? params[:project_id] : params[:id]
|
||||||
project = Project.where(identifier: project_id)
|
project = Project.where(identifier: project_id)
|
||||||
|
|
|
@ -2,10 +2,11 @@ class RepositoriesController < ApplicationController
|
||||||
include ApplicationHelper
|
include ApplicationHelper
|
||||||
include OperateProjectAbilityAble
|
include OperateProjectAbilityAble
|
||||||
before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror]
|
before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror]
|
||||||
before_action :find_project, except: :tags
|
before_action :find_project, except: [:tags, :commit]
|
||||||
before_action :authorizate!, except: [:sync_mirror, :tags]
|
before_action :authorizate!, except: [:sync_mirror, :tags, :commit]
|
||||||
before_action :find_repository, only: %i[sync_mirror tags]
|
before_action :find_repository, only: %i[sync_mirror tags]
|
||||||
before_action :authorizate_user_can_edit_project!, only: %i[sync_mirror]
|
before_action :authorizate_user_can_edit_project!, only: %i[sync_mirror]
|
||||||
|
before_action :find_repository_by_id, only: %i[commit]
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@branches_count = Gitea::Repository::BranchesService.new(@project.owner, @project.identifier).call&.size
|
@branches_count = Gitea::Repository::BranchesService.new(@project.owner, @project.identifier).call&.size
|
||||||
|
@ -46,8 +47,9 @@ class RepositoriesController < ApplicationController
|
||||||
@hash_commit = Gitea::Repository::Commits::ListService.new(@project.owner, @project.identifier, sha: params[:sha], page: params[:page]).call
|
@hash_commit = Gitea::Repository::Commits::ListService.new(@project.owner, @project.identifier, sha: params[:sha], page: params[:page]).call
|
||||||
end
|
end
|
||||||
|
|
||||||
def single_commit
|
def commit
|
||||||
@commit = Gitea::Repository::Commits::GetService.new(@project.owner, @project.identifier, params[:sha]).call
|
@commit = Gitea::Repository::Commits::GetService.new(@repo.user.login, @repo.identifier, params[:sha], current_user.gitea_token).call
|
||||||
|
@custom_commit = Gitea::Repository::Commits::GetService.new(@repo.user.login, @repo.identifier, params[:sha], current_user.gitea_token, true).call
|
||||||
end
|
end
|
||||||
|
|
||||||
def tags
|
def tags
|
||||||
|
|
|
@ -432,4 +432,8 @@ module ApplicationHelper
|
||||||
def render_unix_time(date)
|
def render_unix_time(date)
|
||||||
date.to_time.to_i
|
date.to_time.to_i
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def find_user_by_login(login)
|
||||||
|
User.find_by_login login
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,4 +8,9 @@ module RepositoriesHelper
|
||||||
default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb)
|
default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb)
|
||||||
default_type.include?(str)
|
default_type.include?(str)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def render_commit_author(author_json)
|
||||||
|
return nil if author_json.blank?
|
||||||
|
find_user_by_login author_json['login']
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,12 +1,16 @@
|
||||||
# Get a single commit from a repository
|
# Get a single commit from a repository
|
||||||
class Gitea::Repository::Commits::GetService < Gitea::ClientService
|
class Gitea::Repository::Commits::GetService < Gitea::ClientService
|
||||||
attr_reader :user, :repo_name, :sha
|
attr_reader :token, :owner, :repo, :sha, :custom
|
||||||
|
|
||||||
# sha: the commit hash
|
# sha: the commit hash
|
||||||
def initialize(user, repo_name, sha)
|
# ex: Gitea::Repository::Commits::GetService.new(@repo.user.login, repo.identifier, params[:sha], current_user.gitea_token)
|
||||||
@user = user
|
# TODO custom参数用于判断调用哪个api
|
||||||
@sha = sha
|
def initialize(owner, repo, sha, token, custom=false)
|
||||||
@repo_name = repo_name
|
@token = token
|
||||||
|
@owner = owner
|
||||||
|
@sha = sha
|
||||||
|
@repo = repo
|
||||||
|
@custom = custom
|
||||||
end
|
end
|
||||||
|
|
||||||
def call
|
def call
|
||||||
|
@ -16,11 +20,17 @@ class Gitea::Repository::Commits::GetService < Gitea::ClientService
|
||||||
|
|
||||||
private
|
private
|
||||||
def params
|
def params
|
||||||
Hash.new.merge(token: user.gitea_token)
|
Hash.new.merge(token: token)
|
||||||
end
|
end
|
||||||
|
|
||||||
def url
|
def url
|
||||||
"/repos/#{user.login}/#{repo_name}/git/commits/#{sha}".freeze
|
if custom
|
||||||
|
# TODO
|
||||||
|
# 平台自己编写的gitea接口,后续可能会通过提交pr的形式合并到gitea原有的接口上
|
||||||
|
"/repos/#{owner}/#{repo}/commits/diff/#{sha}".freeze
|
||||||
|
else
|
||||||
|
"/repos/#{owner}/#{repo}/git/commits/#{sha}".freeze
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_result(response)
|
def render_result(response)
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
if user
|
||||||
|
json.id user.id
|
||||||
|
json.login user.login
|
||||||
|
json.name user.real_name
|
||||||
|
json.image_url url_to_avatar(user)
|
||||||
|
else
|
||||||
|
json.nil!
|
||||||
|
end
|
|
@ -0,0 +1,26 @@
|
||||||
|
json.key_format! camelize: :lower
|
||||||
|
json.additions @custom_commit['TotalAddition']
|
||||||
|
json.deletions @custom_commit['TotalDeletion']
|
||||||
|
json.sha @commit['sha']
|
||||||
|
json.url request.url
|
||||||
|
json.commit do
|
||||||
|
@commit['commit'].delete('url')
|
||||||
|
json.author @commit['commit']['author']
|
||||||
|
json.committer @commit['commit']['committer']
|
||||||
|
json.tree do
|
||||||
|
@commit['commit']['tree']['sha']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
json.author do
|
||||||
|
json.partial! 'commit_author', user: render_commit_author(@commit['author'])
|
||||||
|
end
|
||||||
|
json.committer do
|
||||||
|
json.partial! 'commit_author', user: render_commit_author(@commit['committer'])
|
||||||
|
end
|
||||||
|
|
||||||
|
json.parents @commit['parents'] do |parent|
|
||||||
|
json.sha parent['sha']
|
||||||
|
json.url EduSetting.get('host_name') + commit_repository_path(@repo, parent['sha'])
|
||||||
|
end
|
||||||
|
|
||||||
|
json.files @custom_commit['Files']
|
|
@ -44,7 +44,7 @@ Rails.application.routes.draw do
|
||||||
resources :project_languages, only: [:index, :show]
|
resources :project_languages, only: [:index, :show]
|
||||||
resources :ignores, only: [:index, :show]
|
resources :ignores, only: [:index, :show]
|
||||||
resources :licenses, only: [:index, :show]
|
resources :licenses, only: [:index, :show]
|
||||||
|
|
||||||
resources :watchers, only: [:index] do
|
resources :watchers, only: [:index] do
|
||||||
collection do
|
collection do
|
||||||
post :follow
|
post :follow
|
||||||
|
@ -83,7 +83,7 @@ Rails.application.routes.draw do
|
||||||
post :update_status
|
post :update_status
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :praise_tread, only: [:index] do
|
resources :praise_tread, only: [:index] do
|
||||||
collection do
|
collection do
|
||||||
post :like
|
post :like
|
||||||
|
@ -191,7 +191,6 @@ Rails.application.routes.draw do
|
||||||
get :entries
|
get :entries
|
||||||
match :sub_entries, :via => [:get, :put]
|
match :sub_entries, :via => [:get, :put]
|
||||||
get :commits
|
get :commits
|
||||||
get :single_commit
|
|
||||||
post :files
|
post :files
|
||||||
get :tags
|
get :tags
|
||||||
post :create_file
|
post :create_file
|
||||||
|
@ -199,6 +198,7 @@ Rails.application.routes.draw do
|
||||||
delete :delete_file
|
delete :delete_file
|
||||||
post :repo_hook
|
post :repo_hook
|
||||||
post :sync_mirror
|
post :sync_mirror
|
||||||
|
get 'commits/:sha', to: 'repositories#commit', as: 'commit'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue