| @@ -139,4 +139,4 @@ gem 'doorkeeper' | |||||
| gem 'doorkeeper-jwt' | gem 'doorkeeper-jwt' | ||||
| gem 'gitea-client', '~> 0.11.1' | |||||
| gem 'gitea-client', '~> 0.11.6' | |||||
| @@ -1,16 +1,40 @@ | |||||
| class Api::V1::Projects::BranchesController < Api::V1::BaseController | class Api::V1::Projects::BranchesController < Api::V1::BaseController | ||||
| before_action :require_public_and_member_above, only: [:all] | |||||
| before_action :require_public_and_member_above, only: [:index, :all] | |||||
| def index | |||||
| @result_object = Api::V1::Projects::Branches::ListService.call(@project, {name: params[:keyword], page: page, limit: limit}, current_user&.gitea_token) | |||||
| end | |||||
| def all | def all | ||||
| @result_object = Api::V1::Projects::Branches::AllListService.call(@project, current_user&.gitea_token) | @result_object = Api::V1::Projects::Branches::AllListService.call(@project, current_user&.gitea_token) | ||||
| end | end | ||||
| before_action :require_operate_above, only: [:create] | |||||
| before_action :require_operate_above, only: [:create, :destroy] | |||||
| def create | def create | ||||
| @result_object = Api::V1::Projects::Branches::CreateService.call(@project, branch_params, current_user&.gitea_token) | @result_object = Api::V1::Projects::Branches::CreateService.call(@project, branch_params, current_user&.gitea_token) | ||||
| end | end | ||||
| def destroy | |||||
| @result_object = Api::V1::Projects::Branches::DeleteService.call(@project, params[:name], current_user&.gitea_token) | |||||
| if @result_object | |||||
| return render_ok | |||||
| else | |||||
| return render_error('删除分支失败!') | |||||
| end | |||||
| end | |||||
| before_action :require_manager_above, only: [:update_default_branch] | |||||
| def update_default_branch | |||||
| @result_object = Api::V1::Projects::Branches::UpdateDefaultBranchService.call(@project, params[:name], current_user&.gitea_token) | |||||
| if @result_object | |||||
| return render_ok | |||||
| else | |||||
| return render_error('更新默认分支失败!') | |||||
| end | |||||
| end | |||||
| private | private | ||||
| def branch_params | def branch_params | ||||
| params.require(:branch).permit(:new_branch_name, :old_branch_name) | params.require(:branch).permit(:new_branch_name, :old_branch_name) | ||||
| @@ -0,0 +1,19 @@ | |||||
| class Api::V1::Projects::TagsController < Api::V1::BaseController | |||||
| before_action :require_public_and_member_above, only: [:index] | |||||
| def index | |||||
| @release_tags = @repository.version_releases.pluck(:tag_name) | |||||
| @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[:name], current_user&.gitea_token) | |||||
| if @result_object | |||||
| return render_ok | |||||
| else | |||||
| return render_error('删除标签失败!') | |||||
| end | |||||
| end | |||||
| end | |||||
| @@ -6,7 +6,8 @@ module Api::ProjectHelper | |||||
| repo = params[:repo] | repo = params[:repo] | ||||
| @project, @owner = Project.find_with_namespace(namespace, repo) | @project, @owner = Project.find_with_namespace(namespace, repo) | ||||
| @repository = @project&.repository | |||||
| if @project | if @project | ||||
| logger.info "###########:project founded" | logger.info "###########:project founded" | ||||
| @project | @project | ||||
| @@ -18,7 +18,7 @@ class Api::V1::Projects::Branches::CreateService < ApplicationService | |||||
| def call | def call | ||||
| raise Error, errors.full_messages.join(",") unless valid? | raise Error, errors.full_messages.join(",") unless valid? | ||||
| check_new_branch_exist | |||||
| check_branch_exist | |||||
| excute_data_to_gitea | excute_data_to_gitea | ||||
| gitea_data | gitea_data | ||||
| @@ -43,9 +43,10 @@ class Api::V1::Projects::Branches::CreateService < ApplicationService | |||||
| raise Error, '创建分支失败!' unless @gitea_data.is_a?(Hash) | raise Error, '创建分支失败!' unless @gitea_data.is_a?(Hash) | ||||
| end | end | ||||
| def check_new_branch_exist | |||||
| def check_branch_exist | |||||
| result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo, {query: request_params}) rescue nil | result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo, {query: request_params}) rescue nil | ||||
| raise Error, '查询分支名称失败!' unless result.is_a?(Hash) | raise Error, '查询分支名称失败!' unless result.is_a?(Hash) | ||||
| raise Error, '分支已存在!' if result['branch_name'].include?(@new_branch_name) | |||||
| raise Error, '旧分支不存在!' if !result['branch_name'].include?(@old_branch_name) | |||||
| raise Error, '新分支已存在!' if result['branch_name'].include?(@new_branch_name) | |||||
| end | end | ||||
| end | end | ||||
| @@ -0,0 +1,47 @@ | |||||
| class Api::V1::Projects::Branches::DeleteService < ApplicationService | |||||
| include ActiveModel::Model | |||||
| attr_accessor :project, :token, :owner, :repo, :branch_name | |||||
| attr_accessor :gitea_data | |||||
| validates :branch_name, presence: true | |||||
| def initialize(project, branch_name, token=nil) | |||||
| @project = project | |||||
| @owner = project&.owner.login | |||||
| @repo = project&.identifier | |||||
| @branch_name = branch_name | |||||
| @token = token | |||||
| end | |||||
| def call | |||||
| raise Error, errors.full_messages.join(",") unless valid? | |||||
| check_branch_exist | |||||
| excute_data_to_gitea | |||||
| true | |||||
| end | |||||
| private | |||||
| def request_params | |||||
| { | |||||
| access_token: token | |||||
| } | |||||
| end | |||||
| def excute_data_to_gitea | |||||
| begin | |||||
| @gitea_data = $gitea_client.delete_repos_branches_by_owner_repo_branch(owner, repo, branch_name, {query: request_params}) | |||||
| rescue => e | |||||
| raise Error, '保护分支无法删除!' if e.to_s.include?("branch protected") | |||||
| raise Error, '删除分支失败!' | |||||
| end | |||||
| end | |||||
| def check_branch_exist | |||||
| result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo, {query: request_params}) rescue nil | |||||
| raise Error, '查询分支名称失败!' unless result.is_a?(Hash) | |||||
| raise Error, '分支不存在!' if !result['branch_name'].include?(@branch_name) | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,40 @@ | |||||
| class Api::V1::Projects::Branches::ListService < ApplicationService | |||||
| attr_accessor :project, :token, :owner, :repo, :name, :page, :limit | |||||
| attr_accessor :gitea_data | |||||
| def initialize(project, params, token=nil) | |||||
| @project = project | |||||
| @owner = project&.owner.login | |||||
| @repo = project&.identifier | |||||
| @token = token | |||||
| @name = params[:name] | |||||
| @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.merge!({name: name}) if name.present? | |||||
| params | |||||
| end | |||||
| def load_gitea_data | |||||
| puts request_params | |||||
| @gitea_data = $gitea_client.get_repos_branches_by_owner_repo(owner, repo, {query: request_params}) rescue nil | |||||
| puts @gitea_data | |||||
| raise Error, '获取分支列表失败!' unless @gitea_data.is_a?(Hash) | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,55 @@ | |||||
| class Api::V1::Projects::Branches::UpdateDefaultBranchService < ApplicationService | |||||
| include ActiveModel::Model | |||||
| attr_accessor :project, :token, :owner, :repo, :new_default_branch | |||||
| attr_accessor :gitea_data | |||||
| validates :new_default_branch, presence: true | |||||
| def initialize(project, new_default_branch, token=nil) | |||||
| @project = project | |||||
| @owner = project&.owner.login | |||||
| @repo = project&.identifier | |||||
| @new_default_branch = new_default_branch | |||||
| @token = token | |||||
| end | |||||
| def call | |||||
| raise Error, errors.full_messages.join(",") unless valid? | |||||
| check_branch_exist | |||||
| update_project | |||||
| excute_data_to_gitea | |||||
| gitea_data | |||||
| end | |||||
| private | |||||
| def request_params | |||||
| { | |||||
| access_token: token | |||||
| } | |||||
| end | |||||
| def request_body | |||||
| { | |||||
| default_branch: new_default_branch | |||||
| } | |||||
| end | |||||
| def update_project | |||||
| @project.attributes = request_body | |||||
| raise Error, '更新默认分支失败!' unless @project.save | |||||
| end | |||||
| def excute_data_to_gitea | |||||
| @gitea_data = $gitea_client.patch_repos_by_owner_repo(owner, repo, {body: request_body.to_json, query: request_params}) rescue nil | |||||
| raise Error, '更新默认分支失败!' unless @gitea_data.is_a?(Hash) | |||||
| end | |||||
| def check_branch_exist | |||||
| result = $gitea_client.get_repos_branch_name_set_by_owner_repo(owner, repo, {query: request_params}) rescue nil | |||||
| raise Error, '查询分支名称失败!' unless result.is_a?(Hash) | |||||
| raise Error, '新默认分支不存在!' if !result['branch_name'].include?(@new_default_branch) | |||||
| end | |||||
| end | |||||
| @@ -0,0 +1,47 @@ | |||||
| 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 | |||||
| begin | |||||
| @gitea_data = $gitea_client.delete_repos_tags_by_owner_repo_tag(owner, repo, tag_name, {query: request_params}) | |||||
| rescue => e | |||||
| raise Error, '请先删除发行版!' if e.to_s.include?("409") | |||||
| raise Error, '删除标签失败!' | |||||
| end | |||||
| 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 | |||||
| @@ -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 | |||||
| @@ -18,7 +18,7 @@ json.protected branch['protected'] | |||||
| json.user_can_push branch['user_can_push'] | json.user_can_push branch['user_can_push'] | ||||
| json.user_can_merge branch['user_can_merge'] | json.user_can_merge branch['user_can_merge'] | ||||
| json.commit_id branch['commit_id'] | json.commit_id branch['commit_id'] | ||||
| json.commit_time_from_now time_from_now(branch['commit_time'].to_time) | |||||
| json.commit_time_from_now time_from_now(branch['commit']['timestamp'].to_time) | |||||
| json.commit_time branch['commit_time'] | json.commit_time branch['commit_time'] | ||||
| json.default_branch branch['default_branch'] | json.default_branch branch['default_branch'] | ||||
| json.http_url render_http_url(@project) | json.http_url render_http_url(@project) | ||||
| @@ -0,0 +1,4 @@ | |||||
| json.total_count @result_object[:total_data].to_i | |||||
| json.branches @result_object[:data].each do |branch| | |||||
| json.partial! "api/v1/projects/branches/simple_gitea_detail", branch: branch | |||||
| end | |||||
| @@ -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! 'api/v1/users/commit_user', 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! 'api/v1/users/commit_user', user: render_cache_commit_author(tag['commit']['commiter']), name: tag['commit']['commiter']['name'] | |||||
| end | |||||
| json.author do | |||||
| json.partial! 'api/v1/users/commit_user', user: render_cache_commit_author(tag['commit']['author']), name: tag['commit']['author']['name'] | |||||
| end | |||||
| end | |||||
| else | |||||
| json.name tag | |||||
| end | |||||
| @@ -0,0 +1,5 @@ | |||||
| 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 | |||||
| json.has_release @release_tags.blank? ? false : @release_tags.include?(tag['name']) | |||||
| end | |||||
| @@ -46,11 +46,17 @@ defaults format: :json do | |||||
| get :hooktasks | get :hooktasks | ||||
| end | end | ||||
| end | end | ||||
| resources :branches, only:[:index, :create] do | |||||
| resources :branches, param: :name, only:[:index, :create, :destroy] do | |||||
| collection do | collection do | ||||
| get :all | |||||
| get :all | |||||
| patch :update_default_branch | |||||
| end | end | ||||
| end | end | ||||
| match 'branches/*name', to: "branches#destroy", via: :all | |||||
| resources :tags, param: :name, only: [:index, :destroy] | |||||
| match 'tags/*name', to: "tags#destroy", via: :all | |||||
| resources :commits, only: [:index] | resources :commits, only: [:index] | ||||
| resources :code_stats, only: [:index] | resources :code_stats, only: [:index] | ||||
| get '/commits/:sha/diff', to: 'commits#diff' | get '/commits/:sha/diff', to: 'commits#diff' | ||||