新增: pr版本列表和diff接口

This commit is contained in:
yystopf 2022-07-22 17:33:08 +08:00
parent b21f44b59e
commit 8ba70a33be
9 changed files with 164 additions and 2 deletions

View File

@ -135,4 +135,4 @@ gem 'doorkeeper'
gem 'doorkeeper-jwt'
gem 'gitea-client', '~> 0.10.2'
gem 'gitea-client', '~> 0.10.4'

View File

@ -0,0 +1,20 @@
class Api::V1::Projects::PullRequests::BaseController < Api::V1::BaseController
before_action :require_public_and_member_above
before_action :load_pull_request
def load_pull_request
pull_request_id = params[:pull_request_id] || params[:id]
@pull_request = @project.pull_requests.where(gitea_number: pull_request_id).where.not(id: pull_request_id).take || PullRequest.find_by_id(pull_request_id)
@issue = @pull_request&.issue
if @pull_request
logger.info "###########pull_request founded"
@pull_request
else
logger.info "###########pull_request not found"
@pull_request = nil
render_not_found and return
end
@pull_request
end
end

View File

@ -0,0 +1,10 @@
class Api::V1::Projects::PullRequests::VersionsController < Api::V1::Projects::PullRequests::BaseController
def index
@result_object = Api::V1::Projects::PullRequests::Versions::ListService.call(@project, @pull_request, {page: page, limit: limit}, current_user&.gitea_token)
end
def diff
@result_object = Api::V1::Projects::PullRequests::Versions::GetDiffService.call(@project, @pull_request, params[:id], {filepath: params[:filepath]}, current_user&.gitea_token)
end
end

View File

@ -0,0 +1,35 @@
class Api::V1::Projects::PullRequests::Versions::GetDiffService < ApplicationService
attr_reader :project, :pull_request, :version_id, :owner, :repo, :index, :filepath, :token
attr_accessor :gitea_data
def initialize(project, pull_request, version_id, params, token=nil)
@project = project
@pull_request = pull_request
@version_id = version_id
@owner = project&.owner.login
@repo = project&.identifier
@index = pull_request.gitea_number
@filepath = params[:filepath]
@token = token
end
def call
load_gitea_data
gitea_data
end
private
def request_params
params = {
access_token: token
}
params.merge!(filepath: filepath) if filepath.present?
end
def load_gitea_data
@gitea_data = $gitea_client.get_repos_pulls_versions_diff_by_owner_repo_index_version_id(owner, repo, index, version_id, {query: request_params})
raise Error, '获取合并请求版本diff失败' unless @gitea_data.is_a?(Hash)
end
end

View File

@ -0,0 +1,36 @@
class Api::V1::Projects::PullRequests::Versions::ListService < ApplicationService
attr_reader :project, :pull_request, :token, :owner, :repo, :index, :page, :limit
attr_accessor :gitea_data
def initialize(project, pull_request, params, token = nil)
@project = project
@pull_request = pull_request
@owner = project&.owner.login
@repo = project&.identifier
@page = params[:page] || 1
@limit = params[:limit] || 15
@index = pull_request.gitea_number
@token = token
end
def call
load_gitea_data
gitea_data
end
private
def request_params
{
access_token: token,
page: page,
limit: limit
}
end
def load_gitea_data
@gitea_data = $gitea_client.get_repos_pulls_versions_by_owner_repo_index(owner, repo, index, {query: request_params})
raise Error, '获取合并请求版本失败!' unless @gitea_data.is_a?(Hash)
end
end

View File

@ -0,0 +1,35 @@
json.name file['Name']
json.oldname file['OldName']
json.addition file['Addition']
json.deletion file['Deletion']
json.type file['Type']
json.is_created file['IsCreated']
json.is_deleted file['IsDeleted']
json.is_bin file['IsBin']
json.is_lfs_file file['IsLFSFile']
json.is_renamed file['IsRenamed']
json.is_ambiguous file['IsAmbiguous']
json.is_submodule file['IsSubmodule']
json.sections file['Sections'] do |section|
json.file_name section['FileName']
json.name section['Name']
json.lines section['Lines'] do |line|
json.left_index line['LeftIdx']
json.right_index line['RightIdx']
json.match line['Match']
json.type line['Type']
json.content line['Content']
unless line['SectionInfo'].blank?
json.section_path line['SectionInfo']['Path']
json.section_last_left_index line['SectionInfo']['LastLeftIdx']
json.section_last_right_index line['SectionInfo']['LastRightIdx']
json.section_left_index line['SectionInfo']['LeftIdx']
json.section_right_index line['SectionInfo']['RightIdx']
json.section_left_hunk_size line['SectionInfo']['LeftHunkSize']
json.section_right_hunk_size line['SectionInfo']['RightHunkSize']
end
end
end
json.is_incomplete file['IsIncomplete']
json.is_incomplete_line_too_long file['IsIncompleteLineTooLong']
json.is_protected file['IsProtected']

View File

@ -0,0 +1,6 @@
if @result_object.has_key?("NumFiles")
json.partial! "api/v1/projects/simple_gitea_diff_detail", diff: @result_object
else
json.partial! "api/v1/projects/simple_gitea_diff_file_detail", file: @result_object
end

View File

@ -0,0 +1,13 @@
json.total_count @result_object[:total_data].to_i
json.versions @result_object[:data].each do |version|
json.id version['id']
json.add_line_num version['add_line_num']
json.del_line_num version['del_line_num']
json.commits_count version['commits_count']
json.files_count version['files_count']
json.base_commit_sha version['base_commit_sha']
json.head_commit_sha version['head_commit_sha']
json.start_commit_sha version['start_commit_sha']
json.created_time render_unix_time(version['created_at'])
json.updated_time render_unix_time(version['updated_at'])
end

View File

@ -19,7 +19,14 @@ defaults format: :json do
# projects文件夹下的
scope module: :projects do
resources :issues
resources :pull_requests
resources :pull_requests, module: 'pull_requests' do
resources :versions, only: [:index] do
member do
get :diff
end
end
end
resources :versions
resources :release_versions
resources :webhooks do