diff --git a/Gemfile b/Gemfile index 891c636c6..c73a66428 100644 --- a/Gemfile +++ b/Gemfile @@ -143,4 +143,4 @@ gem 'doorkeeper' gem 'doorkeeper-jwt' -gem 'gitea-client', '~> 1.4.4' +gem 'gitea-client', '~> 1.4.5' diff --git a/app/controllers/api/v1/projects/commits_controller.rb b/app/controllers/api/v1/projects/commits_controller.rb index a1545ae6f..9fd8de1c2 100644 --- a/app/controllers/api/v1/projects/commits_controller.rb +++ b/app/controllers/api/v1/projects/commits_controller.rb @@ -1,5 +1,5 @@ class Api::V1::Projects::CommitsController < Api::V1::BaseController - before_action :require_public_and_member_above, only: [:index, :diff] + before_action :require_public_and_member_above, only: [:index, :diff, :recent] def index @result_object = Api::V1::Projects::Commits::ListService.call(@project, {page: page, limit: limit, sha: params[:sha]}, current_user&.gitea_token) @@ -9,4 +9,8 @@ class Api::V1::Projects::CommitsController < Api::V1::BaseController def diff @result_object = Api::V1::Projects::Commits::DiffService.call(@project, params[:sha], current_user&.gitea_token) end + + def recent + @result_object = Api::V1::Projects::Commits::RecentService.call(@project, {page: page, limit: limit}, current_user&.gitea_token) + end end \ No newline at end of file diff --git a/app/services/api/v1/projects/commits/recent_service.rb b/app/services/api/v1/projects/commits/recent_service.rb new file mode 100644 index 000000000..fa4f65b43 --- /dev/null +++ b/app/services/api/v1/projects/commits/recent_service.rb @@ -0,0 +1,37 @@ +class Api::V1::Projects::Commits::RecentService < ApplicationService + + attr_reader :project, :page, :limit, :owner, :repo, :token + attr_accessor :gitea_data + + def initialize(project, params, token=nil) + @project = project + @page = params[:page] || 1 + @limit = params[:limit] || 15 + @owner = project&.owner&.login + @repo = project&.identifier + @token = token + end + + def call + load_gitea_data + + gitea_data + end + + private + def request_params + param = { + access_token: token, + page: page, + limit: limit + } + + param + end + + def load_gitea_data + @gitea_data = $gitea_hat_client.get_repos_recent_commits_by_owner_repo(owner, repo, {query: request_params}) rescue nil + raise Error, "获取最近提交列表失败" unless @gitea_data.is_a?(Hash) + end + +end \ No newline at end of file diff --git a/app/views/api/v1/projects/commits/recent.json.jbuilder b/app/views/api/v1/projects/commits/recent.json.jbuilder new file mode 100644 index 000000000..2834b10d4 --- /dev/null +++ b/app/views/api/v1/projects/commits/recent.json.jbuilder @@ -0,0 +1,13 @@ +json.total_count @result_object[:total_data].to_i +json.commits @result_object[:data].each do |commit| + json.sha commit['sha'] + json.author do + json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(commit['commit']['author']), name: commit['commit']['author']['name'] } + end + + json.committer do + json.partial! 'api/v1/users/commit_user', locals: { user: render_cache_commit_author(commit['commit']['committer']), name: commit['commit']['committer']['name'] } + end + json.commit_message commit['commit']['message'] + json.parent_shas commit['parents'].map{|x|x['sha']} +end \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index 1133c8b36..f487f8b44 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -90,7 +90,11 @@ defaults format: :json do delete 'tags/*name', to: "tags#destroy", via: :all get 'tags/*name', to: "tags#show", via: :all - resources :commits, only: [:index] + resources :commits, only: [:index] do + collection do + get :recent + end + end resources :code_stats, only: [:index] resources :contributors, only: [:index] do collection do