Browse Source

新增:最新提交列表接口

pull/347/head
yystopf 2 years ago
parent
commit
6a4f63d8ea
5 changed files with 61 additions and 3 deletions
  1. +1
    -1
      Gemfile
  2. +5
    -1
      app/controllers/api/v1/projects/commits_controller.rb
  3. +37
    -0
      app/services/api/v1/projects/commits/recent_service.rb
  4. +13
    -0
      app/views/api/v1/projects/commits/recent.json.jbuilder
  5. +5
    -1
      config/routes/api.rb

+ 1
- 1
Gemfile View File

@@ -143,4 +143,4 @@ gem 'doorkeeper'

gem 'doorkeeper-jwt'

gem 'gitea-client', '~> 1.4.4'
gem 'gitea-client', '~> 1.4.5'

+ 5
- 1
app/controllers/api/v1/projects/commits_controller.rb View File

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

+ 37
- 0
app/services/api/v1/projects/commits/recent_service.rb View File

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

+ 13
- 0
app/views/api/v1/projects/commits/recent.json.jbuilder View File

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

+ 5
- 1
config/routes/api.rb View File

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


Loading…
Cancel
Save