ADD some api

* api/v1/repos/{owner}/{repo}/pulls/{number}/commits
* api/v1/repos/{owner}/{repo}/pulls/{number}/files
* api/v1/repos/{owner}/{repo}/compare/{base}...{head}
This commit is contained in:
Jasder
2020-11-03 18:37:47 +08:00
parent 70f7b59349
commit 2347697e93
15 changed files with 559 additions and 18 deletions

View File

@@ -759,7 +759,7 @@ class ApplicationController < ActionController::Base
end
def load_repository
@repository ||= load_project.repository
@repository ||= load_project&.repository
end
private

View File

@@ -0,0 +1,15 @@
class CompareController < ApplicationController
# skip_before_action :require_login
before_action :load_repository
def index
end
def show
base_ref = Addressable::URI.unescape(params[:base])
@ref = head_ref = Addressable::URI.unescape(params[:head])
@compare_result = Gitea::Repository::Commits::CompareService.call(@owner.login, @project.identifier, base_ref, head_ref)
# render json: @compare_result
end
end

View File

@@ -1,9 +1,8 @@
class PullRequestsController < ApplicationController
before_action :require_login, except: [:index, :show]
before_action :require_login, except: [:index, :show, :files, :commits]
before_action :load_repository
before_action :set_user, only: [:new, :get_branches]
before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge,:get_branches,:create_merge_infos]
# before_action :get_relatived, only: [:edit]
before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge,:get_branches,:create_merge_infos, :files, :commits]
before_action :load_pull_request, only: [:files, :commits]
include TagChosenHelper
include ApplicationHelper
@@ -24,11 +23,11 @@ class PullRequestsController < ApplicationController
end
def new
@all_branches = PullRequests::BranchesService.new(@user, @project).call
@all_branches = PullRequests::BranchesService.new(@owner, @project).call
@is_fork = @project.forked_from_project_id.present?
@projects_names = [{
project_user_login: @user.try(:login),
project_name: "#{@user.try(:show_real_name)}/#{@repository.try(:identifier)}",
project_user_login: @owner.try(:login),
project_name: "#{@owner.try(:show_real_name)}/#{@repository.try(:identifier)}",
project_id: @project.identifier,
id: @project.id
}]
@@ -45,7 +44,7 @@ class PullRequestsController < ApplicationController
end
def get_branches
branch_result = PullRequests::BranchesService.new(@user, @project).call
branch_result = PullRequests::BranchesService.new(@owner, @project).call
render json: branch_result
# return json: branch_result
end
@@ -235,16 +234,19 @@ class PullRequestsController < ApplicationController
end
private
def set_user
@user = @project.owner
def files
@files_result = Gitea::PullRequest::FilesService.call(@owner.login, @project.identifier, @pull_request.gpid)
# render json: @files_result
end
def set_repository
@repository = @project.repository
@user = @project.owner
normal_status(-1, "仓库不存在") unless @repository.present?
normal_status(-1, "用户不存在") unless @user.present?
def commits
@commits_result = Gitea::PullRequest::CommitsService.call(@owner.login, @project.identifier, @pull_request.gpid)
# render json: @commits_result
end
private
def load_pull_request
@pull_request = PullRequest.find params[:id]
end
def find_pull_request

View File

@@ -0,0 +1,2 @@
module CompareHelper
end

View File

@@ -0,0 +1,40 @@
# List commits on a pull request
class Gitea::PullRequest::CommitsService < Gitea::ClientService
attr_reader :owner, :repo, :pull_number, :token
# GET /repos/{owner}/{repo}/pulls/{pull_number}/commits
# owner: 用户
# repo: 仓库名称/标识
# pull_number: pull request主键id
# eg:
# Gitea::PullRequest::FilesService.call('jasder', 'repo_identifier', 1)
def initialize(owner, repo, pull_number, token=nil)
@owner = owner
@repo = repo
@token = token
@pull_number = pull_number
end
def call
response = get(url, params)
render_result(response)
end
private
def params
Hash.new.merge(token: owner)
end
def url
"/repos/#{owner}/#{repo}/pulls/#{pull_number}/commits".freeze
end
def render_result(response)
case response.status
when 200
JSON.parse(response.body)
else
nil
end
end
end

View File

@@ -0,0 +1,40 @@
# List pull requests files
class Gitea::PullRequest::FilesService < Gitea::ClientService
attr_reader :owner, :repo, :pull_number, :token
# GET /repos/{owner}/{repo}/pulls/{pull_number}/files
# owner: 用户
# repo: 仓库名称/标识
# pull_number: pull request主键id
# eg:
# Gitea::PullRequest::FilesService.call('jasder', 'repo_identifier', 1)
def initialize(owner, repo, pull_number, token=nil)
@owner = owner
@repo = repo
@token = token
@pull_number = pull_number
end
def call
response = get(url, params)
render_result(response)
end
private
def params
Hash.new.merge(token: token)
end
def url
"/repos/#{owner}/#{repo}/pulls/#{pull_number}/files".freeze
end
def render_result(response)
case response.status
when 200
JSON.parse(response.body)
else
nil
end
end
end

View File

@@ -0,0 +1,38 @@
# Compare two commits
class Gitea::Repository::Commits::CompareService < Gitea::ClientService
attr_reader :owner, :repo, :base, :head, :token
# sha: the commit hash
# ex:
# Gitea::Repository::Commits::CompareService.call('owner', 'repo_identifier', 'master', 'jasder/repo_identifier:develop')
def initialize(owner, repo, base, head, token=nil)
@token = token
@owner = owner
@base = base
@repo = repo
@head = head
end
def call
response = get(url, params)
render_result(response)
end
private
def params
Hash.new.merge(token: token)
end
def url
"/repos/#{owner}/#{repo}/compare/#{base}...#{head}".freeze
end
def render_result(response)
case response.status
when 200
JSON.parse(response.body)
else
{status: -1, message: "#{body['message']}"}
end
end
end

View File

View File

@@ -0,0 +1,9 @@
json.commits_count @compare_result['Commits']&.size
json.commits @compare_result['Commits'], partial: 'pull_requests/commit', as: :commit
json.diff do
json.files_count @compare_result['Diff']['NumFiles']
json.total_addition @compare_result['Diff']['TotalAddition']
json.total_deletion @compare_result['Diff']['TotalDeletion']
json.files @compare_result['Diff']['Files'], partial: 'pull_requests/diff_file', as: :file
end

View File

@@ -0,0 +1,13 @@
json.author do
author = User.find_by(login: commit['Author']['Name'])
json.partial! 'repositories/commit_author', locals: { user: author }
end
json.committer do
author = User.find_by(login: commit['Committer']['Name'])
json.partial! 'repositories/commit_author', locals: { user: author }
end
json.timestamp render_unix_time(commit['Committer']['When'])
json.time_from_now time_from_now(commit['Committer']['When'])
json.message commit['CommitMessage']
json.sha commit['Sha']

View File

@@ -0,0 +1,40 @@
json.name file['Name']
json.old_name file['OldName']
json.index file['Index']
json.addition file['Addition']
json.deletion file['Deletion']
json.type file['Type']
json.isCreated file['IsCreated']
json.isDeleted file['IsDeleted']
json.isBin file['IsBin']
json.isLFSFile file['IsLFSFile']
json.isRenamed file['IsRenamed']
json.isSubmodule file['IsSubmodule']
json.isLFSFile file['IsLFSFile']
json.sections do
json.array! file['Sections'] do |section|
json.fileName section['FileName']
json.name section['Name']
json.lines do
json.array! section['Lines'] do |line|
json.leftIdx line['LeftIdx']
json.rightIdx line['RightIdx']
json.type line['Type']
json.content line['Content']
json.sectionInfo do
if line['SectionInfo'].blank?
json.nil!
else
json.path line['SectionInfo']['Path']
json.lastLeftIdx line['SectionInfo']['LastLeftIdx']
json.lastRightIdx line['SectionInfo']['LastRightIdx']
json.leftIdx line['SectionInfo']['LeftIdx']
json.rightIdx line['SectionInfo']['RightIdx']
json.leftHunkSize line['SectionInfo']['LeftHunkSize']
json.rightHunkSize line['SectionInfo']['RightHunkSize']
end
end
end
end
end
end

View File

@@ -0,0 +1,2 @@
json.commits_count @commits_result.size
json.commits @commits_result, partial: 'commit', as: :commit

View File

@@ -0,0 +1,4 @@
json.files_count @files_result['NumFiles']
json.total_addition @files_result['TotalAddition']
json.total_deletion @files_result['TotalDeletion']
json.files @files_result['Files'], partial: 'diff_file', as: :file