新增:标签列表和删除接口

This commit is contained in:
yystopf 2023-02-03 18:03:57 +08:00
parent 8b90164247
commit 2772b97e32
8 changed files with 129 additions and 2 deletions

View File

@ -139,4 +139,4 @@ gem 'doorkeeper'
gem 'doorkeeper-jwt'
gem 'gitea-client', '~> 0.11.4'
gem 'gitea-client', '~> 0.11.5'

View File

@ -0,0 +1,18 @@
class Api::V1::Projects::TagsController < Api::V1::BaseController
before_action :require_public_and_member_above, only: [:index]
def index
@result_object = Api::V1::Projects::Tags::ListService.call(@project, {page: page, limit: limit}, current_user&.gitea_token)
end
before_action :require_operate_above, only: [:destroy]
def destroy
@result_object = Api::V1::Projects::Tags::DeleteService.call(@project, params[:id], current_user&.gitea_token)
if @result_object
return render_ok
else
return render_error('删除标签失败!')
end
end
end

View File

@ -17,7 +17,7 @@ class Api::V1::Projects::Branches::DeleteService < ApplicationService
def call
raise Error, errors.full_messages.join(",") unless valid?
# check_branch_exist
check_branch_exist
excute_data_to_gitea
true

View File

@ -0,0 +1,42 @@
class Api::V1::Projects::Tags::DeleteService < ApplicationService
include ActiveModel::Model
attr_accessor :project, :token, :owner, :repo, :tag_name
attr_accessor :gitea_data
validates :tag_name, presence: true
def initialize(project, tag_name, token=nil)
@project = project
@owner = project&.owner.login
@repo = project&.identifier
@tag_name = tag_name
@token = token
end
def call
raise Error, errors.full_messages.join(",") unless valid?
check_tag_exist
excute_data_to_gitea
true
end
private
def request_params
{
access_token: token
}
end
def excute_data_to_gitea
@gitea_data = $gitea_client.delete_repos_tags_by_owner_repo_tag(owner, repo, tag_name, {query: request_params}) rescue nil
end
def check_tag_exist
result = $gitea_client.get_repos_tag_name_set_by_owner_repo(owner, repo, {query: request_params}) rescue nil
raise Error, '查询标签名称失败!' unless result.is_a?(Array)
raise Error, '标签不存在!' if !result.include?(@tag_name)
end
end

View File

@ -0,0 +1,36 @@
class Api::V1::Projects::Tags::ListService < ApplicationService
attr_accessor :project, :token, :owner, :repo, :page, :limit
attr_accessor :gitea_data
def initialize(project, params, token=nil)
@project = project
@owner = project&.owner.login
@repo = project&.identifier
@token = token
@page = params[:page]
@limit = params[:limit]
end
def call
load_gitea_data
gitea_data
end
private
def request_params
params = {
access_token: token,
page: page,
limit: limit
}
params
end
def load_gitea_data
@gitea_data = $gitea_client.get_repos_tags_by_owner_repo(owner, repo, {query: request_params}) rescue nil
raise Error, '获取标签列表失败!' unless @gitea_data.is_a?(Hash)
end
end

View File

@ -0,0 +1,26 @@
if tag.present? && tag.is_a?(Hash)
json.name tag['name']
json.id tag['id']
json.zipball_url render_zip_url(@owner, @repository, tag['name'])
json.tarball_url render_tar_url(@owner, @repository, tag['name'])
json.tagger do
json.partial! 'commit_author', user: render_cache_commit_author(tag['tagger']), name: tag['tagger']['name']
end
json.time_ago time_from_now(tag['tagger']['date'].to_time)
json.created_at_unix tag['tagger']['date'].to_time.to_i
json.message tag['message']
json.commit do
json.sha tag['commit']['sha']
json.message tag['commit']['message']
json.time_ago time_from_now(tag['commit']['commiter']['date'].to_time)
json.created_at_unix tag['commit']['commiter']['date'].to_time.to_i
json.committer do
json.partial! 'commit_author', user: render_cache_commit_author(tag['commit']['commiter']), name: tag['commit']['commiter']['name']
end
json.author do
json.partial! 'commit_author', user: render_cache_commit_author(tag['commit']['author']), name: tag['commit']['author']['name']
end
end
else
json.name tag
end

View File

@ -0,0 +1,4 @@
json.total_count @result_object[:total_data].to_i
json.tags @result_object[:data].each do |tag|
json.partial! "api/v1/projects/tags/simple_gitea_index_detail", tag: tag
end

View File

@ -52,6 +52,7 @@ defaults format: :json do
patch :update_default_branch
end
end
resources :tags, only: [:index, :destroy]
resources :commits, only: [:index]
resources :code_stats, only: [:index]
get '/commits/:sha/diff', to: 'commits#diff'