FIX 升级完善项目相关路由

This commit is contained in:
Jasder 2020-08-11 23:20:04 +08:00
parent 25a3c8a372
commit 6bd8095258
17 changed files with 284 additions and 246 deletions

View File

@ -532,17 +532,18 @@ curl -X POST http://localhost:3000/api/repositories/1244/sync_mirror | jq
#### 项目详情
```
GET api/projects/:id
GET /api/:namespace_id/:id
```
*示例*
```
curl -X GET http://localhost:3000/api/projects/3263 | jq
curl -X GET http://localhost:3000/api/jasder/jasder_test | jq
```
*请求参数说明:*
|参数名|必选|类型|说明|
|-|-|-|-|
|id |是|int |项目id |
|namespace_id |是|string |用户登录名 |
|id |是|string |项目标识identifier |
*返回参数说明:*
@ -571,6 +572,50 @@ curl -X GET http://localhost:3000/api/projects/3263 | jq
```
---
#### 项目详情(简版)
```
GET /api/:namespace_id/:id/simple
```
*示例*
```
curl -X GET http://localhost:3000/api/jasder/jasder_test/simple | jq
```
*请求参数说明:*
|参数名|必选|类型|说明|
|-|-|-|-|
|id |是|int |项目id |
*返回参数说明:*
|参数名|类型|说明|
|-|-|-|
|id |int |id |
|name |string|项目名称|
|identifier |string|项目标识|
|is_public |boolean|项目是否公开, true:公开false:私有|
|description |string|项目简介|
|repo_id |int|仓库id|
|repo_identifier|string|仓库标识|
返回值
```
{
"identifier": "jasder_test",
"name": "jasder的测试项目",
"id": 4967,
"type": 0,
"author": {
"login": "jasder",
"name": "姓名",
"image_url": "avatars/User/b"
}
}
```
---
#### 编辑仓库信息
```
GET /api/repositories/:id/edit.json
@ -898,13 +943,13 @@ curl -X POST http://localhost:3000/api/projects/3297/forks | jq
#### 获取代码目录列表
```
POST /api/repositories/:id/entries.json
POST /api/:namespace_id/:project_id/repository/entries
```
*示例*
```
curl -X GET \
-d "ref=develop" \
http://localhost:3000//api/repositories/3687/entries.json | jq
http://localhost:3000//api/jasder/jasder_test/repository/entries | jq
```
*请求参数说明:*
@ -1321,11 +1366,11 @@ http://localhost:3000/api/projects | jq
### 获取分支列表
```
GET /api/projects/:id/branches
GET /api/:namespace_id/:id/branches
```
*示例*
```
curl -X GET http://localhost:3000/api/projects/4797/branches | jq
curl -X GET http://localhost:3000/api/jasder/jasder_test/branches | jq
```
*请求参数说明:*
@ -1488,18 +1533,19 @@ http://localhost:3000/api/repositories/5836/tags.json | jq
## 仓库详情
```
GET /api/repositories/:id
GET /api/:namespace_id/:project_id/repository
```
*示例*
```
curl -X GET \
http://localhost:3000/api/repositories/23.json | jq
http://192.168.2.230:3000/api/jasder/forgeplus/repository | jq
```
*请求参数说明:*
|参数名|必选|类型|说明|
|-|-|-|-|
|id |是|string |项目id |
|namespace_id |是|string |用户登录名 |
|project_id |是|string |项目标识identifier |
*返回参数说明:*

View File

@ -384,7 +384,7 @@ class ApplicationController < ActionController::Base
def current_user
if Rails.env.development?
User.current = User.find 1
User.current = User.find 36480
else
User.current
end
@ -727,11 +727,6 @@ class ApplicationController < ActionController::Base
render_not_found("未找到’#{project}’相关的项目") unless @project
end
def find_project_with_identifier
@project = Project.find_by_identifier! params[:id]
render_not_found("未找到’#{params[:id]}’相关的项目") unless @project
end
def find_project_with_id
@project = Project.find(params[:project_id] || params[:id])
rescue Exception => e
@ -743,6 +738,32 @@ class ApplicationController < ActionController::Base
interactor.success? ? render_ok : render_error(interactor.error)
end
# projects
def load_project
namespace = params[:namespace_id]
id = params[:project_id] || params[:id]
@project = Project.find_with_namespace(namespace, id)
if @project and current_user.can_read_project?(@project)
logger.info "########### has project and can read project"
@project
elsif current_user.is_a?(AnonymousUser)
logger.info "###########This is AnonymousUser"
@project = nil if !@project.is_public?
render_forbidden and return
else
logger.info "###########project not found"
@project = nil
render_not_found and return
end
@project
end
def load_repository
@repository ||= load_project.repository
end
private
def object_not_found
uid_logger("Missing template or cant't find record, responding with 404")

View File

@ -1,5 +1,6 @@
class ForksController < ApplicationController
before_action :require_login, :find_project_with_id
before_action :require_login
before_action :load_project
before_action :authenticate_project!, :authenticate_user!
def create

View File

@ -1,7 +1,7 @@
class IssueTagsController < ApplicationController
before_action :require_login, except: [:index]
before_action :find_project_with_id
before_action :set_project
before_action :load_repository
before_action :set_user
before_action :check_issue_permission, except: :index
before_action :set_issue_tag, only: [:edit, :update, :destroy]
@ -121,13 +121,8 @@ class IssueTagsController < ApplicationController
private
def set_project
# @project = Project.find_by_identifier! params[:project_id]
@repository = @project.repository
def set_user
@user = @project.owner
normal_status(-1, "项目不存在") unless @project.present?
normal_status(-1, "仓库不存在") unless @repository.present?
normal_status(-1, "用户不存在") unless @user.present?
end
def check_issue_permission
@ -143,4 +138,4 @@ class IssueTagsController < ApplicationController
end
end
end
end

View File

@ -1,7 +1,7 @@
class IssuesController < ApplicationController
before_action :require_login, except: [:index, :show, :index_chosen]
before_action :find_project_with_id
before_action :set_project_and_user
before_action :load_project
before_action :set_user
before_action :check_issue_permission
before_action :check_project_public, only: [:index ,:show, :copy, :index_chosen, :close_issue]
@ -15,7 +15,7 @@ class IssuesController < ApplicationController
@user_admin_or_member = current_user.present? && current_user.logged? && (current_user.admin || @project.member?(current_user))
issues = @project.issues.issue_issue.issue_index_includes
issues = issues.where(is_private: false) unless @user_admin_or_member
@all_issues_size = issues.size
@open_issues_size = issues.where.not(status_id: 5).size
@close_issues_size = issues.where(status_id: 5).size
@ -347,11 +347,8 @@ class IssuesController < ApplicationController
end
private
def set_project_and_user
# @project = Project.find_by_identifier(params[:project_id]) || (Project.find params[:project_id]) || (Project.find params[:id])
def set_user
@user = @project&.owner
# normal_status(-1, "项目不存在") unless @project.present?
normal_status(-1, "用户不存在") unless @user.present?
end
def check_project_public

View File

@ -1,6 +1,6 @@
class MembersController < ApplicationController
before_action :require_login
before_action :find_project_with_id
before_action :load_project
before_action :find_user_with_id, only: %i[create remove change_role]
before_action :operate!, except: %i[index]
before_action :check_member_exists!, only: %i[create]

View File

@ -1,5 +1,5 @@
class ProjectTrendsController < ApplicationController
before_action :find_project_with_id
before_action :load_repository
before_action :check_project_public
def index
@ -44,4 +44,4 @@ class ProjectTrendsController < ApplicationController
normal_status(-1, "您没有权限")
end
end
end
end

View File

@ -1,5 +1,7 @@
class Projects::BaseController < ApplicationController
include PaginateHelper
before_action :require_login, :check_auth
before_action :load_project
before_action :load_repository
end

View File

@ -2,8 +2,8 @@ class ProjectsController < ApplicationController
include ApplicationHelper
include OperateProjectAbilityAble
include ProjectsHelper
before_action :require_login, except: %i[index branches group_type_list simple]
before_action :find_project_with_id, only: %i[show branches update destroy fork_users praise_users watch_users]
before_action :require_login, except: %i[index branches group_type_list simple, show]
before_action :load_project
before_action :authorizate_user_can_edit_project!, only: %i[update]
before_action :project_public?, only: %i[fork_users praise_users watch_users]
@ -100,8 +100,7 @@ class ProjectsController < ApplicationController
end
def simple
project = Project.includes(:owner, :repository).select(:id, :name, :identifier, :user_id, :project_type).find params[:id]
json_response(project)
json_response(@project)
end
private

View File

@ -1,7 +1,6 @@
class PullRequestsController < ApplicationController
before_action :require_login, except: [:index, :show]
before_action :find_project_with_id
before_action :set_repository
before_action :load_repository
before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge,:get_branches,:create_merge_infos]
# before_action :get_relatived, only: [:edit]
include TagChosenHelper
@ -234,14 +233,6 @@ class PullRequestsController < ApplicationController
private
def set_repository
@repository = @project.repository
@user = @project.owner
normal_status(-1, "仓库不存在") unless @repository.present?
normal_status(-1, "用户不存在") unless @user.present?
end
def find_pull_request
@pull_request = PullRequest.find_by_id(params[:id])
@issue = @pull_request&.issue

View File

@ -1,11 +1,11 @@
class RepositoriesController < ApplicationController
include ApplicationHelper
include OperateProjectAbilityAble
before_action :require_login, only: %i[edit update create_file update_file delete_file sync_mirror]
before_action :find_project_with_includes, only: :show
before_action :find_project, except: [:tags, :commit, :sync_mirror, :show]
before_action :load_project
before_action :authorizate!, except: [:sync_mirror, :tags, :commit]
before_action :find_repository_by_id, only: %i[commit sync_mirror tags]
before_action :find_repository_by_id, only: %i[commit sync_mirror]
before_action :authorizate_user_can_edit_repo!, only: %i[sync_mirror]
before_action :get_ref, only: %i[entries sub_entries top_counts]
before_action :get_latest_commit, only: %i[entries sub_entries top_counts]
@ -60,7 +60,7 @@ class RepositoriesController < ApplicationController
end
def tags
@tags = Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @repo.user.login, @repo.identifier, {page: params[:page], limit: params[:limit]}).call
@tags = Gitea::Repository::Tags::ListService.new(current_user&.gitea_token, @project.owner.login, @project.identifier, {page: params[:page], limit: params[:limit]}).call
end
def edit

View File

@ -1,6 +1,6 @@
class VersionReleasesController < ApplicationController
before_action :find_project_with_id
before_action :set_user_and_project
before_action :load_repository
before_action :set_user
before_action :require_login, except: [:index]
before_action :find_version , only: [:edit, :update, :destroy]
@ -78,14 +78,14 @@ class VersionReleasesController < ApplicationController
ActiveRecord::Base.transaction do
begin
version_params = releases_params
if @version.update_attributes!(version_params)
create_attachments(params[:attachment_ids], @version) if params[:attachment_ids].present?
git_version_release = Gitea::Versions::UpdateService.new(@user.gitea_token, @user.try(:login), @repository.try(:identifier), version_params, @version.try(:version_gid)).call
unless git_version_release
raise Error, "更新失败"
end
normal_status(0, "更新成功")
else
normal_status(-1, "更新失败")
@ -123,14 +123,8 @@ class VersionReleasesController < ApplicationController
private
def set_user_and_project
# @project = Project.find_by_id(params[:project_id])
@repository = @project.repository #项目的仓库
@user = @project.owner
unless @user.present? && @project.present? && @repository.present?
normal_status(-1, "仓库不存在")
end
def set_user
@user = @repository.user
end
def find_version
@ -140,7 +134,7 @@ class VersionReleasesController < ApplicationController
end
end
def releases_params
def releases_params
{
body: params[:body],
draft: params[:draft] || false,
@ -151,7 +145,7 @@ class VersionReleasesController < ApplicationController
}
end
def create_attachments(attachment_ids, target)
def create_attachments(attachment_ids, target)
attachment_ids.each do |id|
attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id)
unless attachment.blank?

View File

@ -1,6 +1,6 @@
class VersionsController < ApplicationController
before_action :require_login, except: [:index, :show]
before_action :find_project_with_id
before_action :load_repository
before_action :check_issue_permission, except: [:show, :index]
before_action :set_version, only: [:edit, :update, :destroy, :show,:update_status]
@ -166,4 +166,4 @@ class VersionsController < ApplicationController
end
end
end
end

View File

@ -0,0 +1,14 @@
module ProjectAbility
extend ActiveSupport::Concern
included do
end
def can_read_project?(project)
return true if self.admin?
return false if !project.is_public? && !project.member?(self.id)
true
end
end

View File

@ -160,7 +160,7 @@ class Project < ApplicationRecord
member&.roles&.last&.name || permission
end
def fork_project
def fork_project
Project.find_by(id: self.forked_from_project_id)
end
@ -168,4 +168,15 @@ class Project < ApplicationRecord
joins(:members).where(members: { user_id: member_user_id})
end
def self.find_with_namespace(namespace_path, identifier)
logger.info "########namespace_path: #{namespace_path} ########identifier: #{identifier} "
user = User.find_by_login namespace_path
return nil if user.blank?
project = user.projects.find_by(identifier: identifier)
return nil if project.blank?
project
end
end

View File

@ -5,6 +5,7 @@ class User < ApplicationRecord
include Likeable
include BaseModel
include ProjectOperable
include ProjectAbility
# include Searchable::Dependents::User
# Account statuses
@ -70,8 +71,8 @@ class User < ApplicationRecord
# 关注
has_many :be_watchers, foreign_key: :user_id, dependent: :destroy # 我的关注
has_many :be_watcher_users, through: :be_watchers, dependent: :destroy # 我关注的用户
has_many :watchers, as: :watchable, dependent: :destroy
has_many :watchers, as: :watchable, dependent: :destroy
# 认证
has_many :apply_user_authentication

View File

@ -16,8 +16,8 @@ Rails.application.routes.draw do
resources :edu_settings
scope '/api' do
resources :sync_forge, only: [:create] do
collection do
resources :sync_forge, only: [:create] do
collection do
post :sync_users
end
end
@ -67,41 +67,6 @@ Rails.application.routes.draw do
end
end
resources :projects do
resources :hooks
resources :pull_requests, except: [:destroy] do
member do
post :pr_merge
# post :check_merge
post :refuse_merge
end
collection do
post :check_can_merge
get :create_merge_infos
get :get_branches
end
end
resources :version_releases, only: [:index,:new, :create, :edit, :update, :destroy]
resources :project_trends, only: [:index, :create]
resources :issues do
collection do
get :commit_issues
get :index_chosen
post :clean
post :series_update
end
member do
post :copy
post :close_issue
post :lock_issue
end
end
resources :issue_tags, only: [:create, :edit, :update, :destroy, :index]
resources :versions do
member do
post :update_status
end
end
resources :praise_tread, only: [:index] do
collection do
post :like
@ -109,28 +74,147 @@ Rails.application.routes.draw do
get :check_like
end
end
resources :members, only: [:index, :create] do
collection do
delete :remove
put :change_role
end
end
resources :forks, only: [:create]
collection do
post :migrate
get :group_type_list
post :watch
end
member do
get :branches
post :watch
get :watch_users
get :praise_users
get :fork_users
get :simple
end
end
# Project Area START
resources :namespaces, path: '/', constraints: { id: /[a-zA-Z.0-9_\-]+/ }, only: [] do
resources(:projects, constraints: { id: /[a-zA-Z.0-9_\-]+(?<!\.atom)/ }, except:
[:new, :create, :index], path: "/") do
member do
get :activity
get :branches
get :simple
get :branches
post :watch
get :watch_users, :path => :watchers
get :praise_users, :path => :stargazers
get :fork_users, :path => :members
end
resource :repository, only: [:show, :create, :edit] do
member do
get 'archive'
get 'top_counts'
get 'entries'
get 'sub_entries'
get 'commits'
get 'tags'
end
end
resources :issues do
collection do
get :commit_issues
get :index_chosen
post :clean
post :series_update
end
member do
post :copy
post :close_issue
post :lock_issue
end
end
resources :pull_requests, :path => :pulls, except: [:destroy] do
member do
post :pr_merge
# post :check_merge
post :refuse_merge
end
collection do
post :check_can_merge
get :create_merge_infos
get :get_branches
end
end
resources :versions, :path => :milestones do
member do
post :update_status
end
end
resources :members, :path => :collaborators, only: [:index, :create] do
collection do
delete :remove
put :change_role
end
end
resources :hooks
resources :forks, only: [:create]
resources :project_trends, :path => :activity, only: [:index, :create]
resources :issue_tags, :path => :labels, only: [:create, :edit, :update, :destroy, :index]
resources :version_releases, :path => :releases, only: [:index,:new, :create, :edit, :update, :destroy]
scope module: :projects do
scope do
get(
'/blob/*id/diff',
to: 'blob#diff',
constraints: { id: /.+/, format: false },
as: :blob_diff
)
get(
'/blob/*id',
to: 'blob#show',
constraints: { id: /.+/, format: false },
as: :blob
)
delete(
'/blob/*id',
to: 'blob#destroy',
constraints: { id: /.+/, format: false }
)
put(
'/blob/*id',
to: 'blob#update',
constraints: { id: /.+/, format: false }
)
post(
'/blob/*id',
to: 'blob#create',
constraints: { id: /.+/, format: false }
)
end
scope do
get(
'/raw/*id',
to: 'raw#show',
constraints: { id: /.+/, format: /(html|js)/ },
as: :raw
)
end
scope do
get(
'/blame/*id',
to: 'blame#show',
constraints: { id: /.+/, format: /(html|js)/ },
as: :blame
)
end
scope do
get(
'/tree/*id',
to: 'tree#show',
constraints: { id: /.+/, format: /(html|js)/ },
as: :tree
)
end
end
end
end
# Project Area END
resources :accounts do
collection do
post :login
@ -176,7 +260,7 @@ Rails.application.routes.draw do
scope module: :users do
# resources :courses, only: [:index]
resources :projects, only: [:index]
# resources :projects, only: [:index]
# resources :subjects, only: [:index]
resources :project_packages, only: [:index]
# 私信
@ -186,11 +270,6 @@ Rails.application.routes.draw do
# resource :unread_message_info, only: [:show]
end
resources :projects, module: :users, only: [] do
get :search, on: :collection
end
resources :tidings, only: [:index]
scope module: :users do
@ -245,119 +324,6 @@ Rails.application.routes.draw do
end
end
resources :courses do
member do
get 'settings', :action => 'settings', :as => 'settings'
post 'set_invite_code_halt'
post 'set_public_or_private'
post 'search_teacher_candidate'
post 'add_teacher'
post 'create_graduation_group'
post 'join_graduation_group'
post 'set_course_group'
post 'change_course_admin'
post 'change_member_role'
post 'change_course_teacher'
post 'delete_course_teacher'
post 'teacher_application_review'
post 'transfer_to_course_group'
post 'delete_from_course'
post 'add_students_by_search'
post 'create_group_by_importing_file'
post 'duplicate_course'
post 'visits_plus_one'
get 'get_historical_courses'
get 'get_historical_course_students'
get 'course_group_list'
get 'add_teacher_popup'
get 'teachers'
get 'apply_teachers'
get 'graduation_group_list'
get 'top_banner'
get 'left_banner'
get 'students'
get 'all_course_groups'
get 'search_users'
get 'base_info'
get 'attahcment_category_list'
get 'export_member_scores_excel' #导出课堂信息
get 'export_couser_info'
get 'export_member_act_score'
post 'switch_to_teacher'
post 'switch_to_assistant'
post 'switch_to_student'
post 'exit_course'
get 'informs'
post 'update_informs'
post 'new_informs'
delete 'delete_informs'
get 'online_learning'
post 'join_excellent_course'
get 'tasks_list'
post 'update_task_position'
get 'course_groups'
post 'join_course_group'
get 'work_score'
get 'act_score'
get 'statistics'
get 'course_videos'
delete 'delete_course_video'
post :inform_up
post :inform_down
end
collection do
post 'apply_to_join_course'
post 'search_course_list'
get 'board_list'
get 'mine'
get 'search_slim'
end
resources :course_stages, shallow: true do
member do
post :up_position
post :down_position
end
end
resources :course_groups, shallow: true do
member do
post 'rename_group'
post 'move_category'
post 'set_invite_code_halt'
end
end
end
resources :course_modules, shallow: true do
member do
get 'sticky_module'
get 'hidden_module'
post 'rename_module'
post 'add_second_category'
end
collection do
post 'unhidden_modules'
end
end
resources :course_second_categories, shallow: true do
member do
post 'rename_category'
post 'move_category'
end
end
resources :repertoires, only: [:index]
scope module: :projects do
resources :project_applies, only: [:create]
end
namespace :wechats do
resource :js_sdk_signature, only: [:create]
end