diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f16050151..dc56fb47a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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) diff --git a/app/controllers/repositories_controller.rb b/app/controllers/repositories_controller.rb index 4447b1d78..f19eb7bf1 100644 --- a/app/controllers/repositories_controller.rb +++ b/app/controllers/repositories_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 431f4eabb..2dc8c897d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/helpers/repositories_helper.rb b/app/helpers/repositories_helper.rb index 0923b0e76..440f833aa 100644 --- a/app/helpers/repositories_helper.rb +++ b/app/helpers/repositories_helper.rb @@ -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 diff --git a/app/services/gitea/repository/commits/get_service.rb b/app/services/gitea/repository/commits/get_service.rb index fea12493d..019658d1f 100644 --- a/app/services/gitea/repository/commits/get_service.rb +++ b/app/services/gitea/repository/commits/get_service.rb @@ -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) diff --git a/app/views/repositories/_commit_author.json.jbuilder b/app/views/repositories/_commit_author.json.jbuilder new file mode 100644 index 000000000..77d9b655c --- /dev/null +++ b/app/views/repositories/_commit_author.json.jbuilder @@ -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 diff --git a/app/views/repositories/commit.json.jbuilder b/app/views/repositories/commit.json.jbuilder new file mode 100644 index 000000000..7a547316f --- /dev/null +++ b/app/views/repositories/commit.json.jbuilder @@ -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'] diff --git a/config/routes.rb b/config/routes.rb index f9f932cdd..9542c5ae4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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