ADD get commit diff api

This commit is contained in:
Jasder 2020-05-29 15:33:17 +08:00
parent b266085e56
commit 79a97b4a02
8 changed files with 75 additions and 16 deletions

View File

@ -382,8 +382,8 @@ class ApplicationController < ActionController::Base
def current_user
if Rails.env.development?
User.find(1)
else
User.current = User.find 36480
else
User.current
end
# User.current
@ -710,6 +710,10 @@ class ApplicationController < ActionController::Base
render_not_found("未找到’#{params[:repo_identifier]}’相关的项目") unless @repo
end
def find_repository_by_id
@repo = Repository.find params[:id]
end
def find_project
project_id = params[:project_id] ? params[:project_id] : params[:id]
project = Project.where(identifier: project_id)

View File

@ -2,10 +2,11 @@ class RepositoriesController < ApplicationController
include ApplicationHelper
include OperateProjectAbilityAble
before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror]
before_action :find_project, except: :tags
before_action :authorizate!, except: [:sync_mirror, :tags]
before_action :find_project, except: [:tags, :commit]
before_action :authorizate!, except: [:sync_mirror, :tags, :commit]
before_action :find_repository, only: %i[sync_mirror tags]
before_action :authorizate_user_can_edit_project!, only: %i[sync_mirror]
before_action :find_repository_by_id, only: %i[commit]
def show
@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
end
def single_commit
@commit = Gitea::Repository::Commits::GetService.new(@project.owner, @project.identifier, params[:sha]).call
def commit
@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
def tags

View File

@ -432,4 +432,8 @@ module ApplicationHelper
def render_unix_time(date)
date.to_time.to_i
end
def find_user_by_login(login)
User.find_by_login login
end
end

View File

@ -8,4 +8,9 @@ module RepositoriesHelper
default_type = %w(xlsx xls ppt pptx pdf zip 7z rar exe pdb obj idb)
default_type.include?(str)
end
def render_commit_author(author_json)
return nil if author_json.blank?
find_user_by_login author_json['login']
end
end

View File

@ -1,12 +1,16 @@
# Get a single commit from a repository
class Gitea::Repository::Commits::GetService < Gitea::ClientService
attr_reader :user, :repo_name, :sha
attr_reader :token, :owner, :repo, :sha, :custom
# sha: the commit hash
def initialize(user, repo_name, sha)
@user = user
@sha = sha
@repo_name = repo_name
# ex: Gitea::Repository::Commits::GetService.new(@repo.user.login, repo.identifier, params[:sha], current_user.gitea_token)
# TODO custom参数用于判断调用哪个api
def initialize(owner, repo, sha, token, custom=false)
@token = token
@owner = owner
@sha = sha
@repo = repo
@custom = custom
end
def call
@ -16,11 +20,17 @@ class Gitea::Repository::Commits::GetService < Gitea::ClientService
private
def params
Hash.new.merge(token: user.gitea_token)
Hash.new.merge(token: token)
end
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
def render_result(response)

View File

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

View File

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

View File

@ -44,7 +44,7 @@ Rails.application.routes.draw do
resources :project_languages, only: [:index, :show]
resources :ignores, only: [:index, :show]
resources :licenses, only: [:index, :show]
resources :watchers, only: [:index] do
collection do
post :follow
@ -83,7 +83,7 @@ Rails.application.routes.draw do
post :update_status
end
end
resources :praise_tread, only: [:index] do
collection do
post :like
@ -191,7 +191,6 @@ Rails.application.routes.draw do
get :entries
match :sub_entries, :via => [:get, :put]
get :commits
get :single_commit
post :files
get :tags
post :create_file
@ -199,6 +198,7 @@ Rails.application.routes.draw do
delete :delete_file
post :repo_hook
post :sync_mirror
get 'commits/:sha', to: 'repositories#commit', as: 'commit'
end
end