diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 8e82a814a..09dfdbdb7 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -169,6 +169,7 @@ class AccountsController < ApplicationController # 用户登录 def login + Users::LoginForm.new(account_params).validate! @user = User.try_to_login(params[:login], params[:password]) return normal_status(-2, "错误的账号或密码") if @user.blank? @@ -345,4 +346,7 @@ class AccountsController < ApplicationController params.require(:user).permit(:login, :email, :phone) end + def account_params + params.require(:account).permit(:login, :password) + end end diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index a3b5fe923..3584d6bf5 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -101,51 +101,46 @@ class IssuesController < ApplicationController end def create - if params[:subject].blank? - normal_status(-1, "标题不能为空") - elsif params[:subject].to_s.size > 255 - normal_status(-1, "标题不能超过255个字符") - else - issue_params = issue_send_params(params) - - @issue = Issue.new(issue_params) - if @issue.save! - if params[:attachment_ids].present? - params[:attachment_ids].each do |id| - attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id) - unless attachment.blank? - attachment.container = @issue - attachment.author_id = current_user.id - attachment.description = "" - attachment.save - end + issue_params = issue_send_params(params) + Issues::CreateForm.new({subject:issue_params[:subject]}).validate! + @issue = Issue.new(issue_params) + if @issue.save! + if params[:attachment_ids].present? + params[:attachment_ids].each do |id| + attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id) + unless attachment.blank? + attachment.container = @issue + attachment.author_id = current_user.id + attachment.description = "" + attachment.save end end - if params[:issue_tag_ids].present? - params[:issue_tag_ids].each do |tag| - IssueTagsRelate.create!(issue_id: @issue.id, issue_tag_id: tag) - end + end + if params[:issue_tag_ids].present? + params[:issue_tag_ids].each do |tag| + IssueTagsRelate.create!(issue_id: @issue.id, issue_tag_id: tag) end - if params[:assigned_to_id].present? - Tiding.create!(user_id: params[:assigned_to_id], trigger_user_id: current_user.id, - container_id: @issue.id, container_type: 'Issue', - parent_container_id: @project.id, parent_container_type: "Project", - tiding_type: 'issue', status: 0) - end - - #为悬赏任务时, 扣除当前用户的积分 - if params[:issue_type].to_s == "2" - post_to_chain("minus", params[:token].to_i, current_user.try(:login)) - end - - @issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create") - render json: {status: 0, message: "创建成", id: @issue.id} - else - normal_status(-1, "创建失败") + end + if params[:assigned_to_id].present? + Tiding.create!(user_id: params[:assigned_to_id], trigger_user_id: current_user.id, + container_id: @issue.id, container_type: 'Issue', + parent_container_id: @project.id, parent_container_type: "Project", + tiding_type: 'issue', status: 0) end - end + #为悬赏任务时, 扣除当前用户的积分 + if params[:issue_type].to_s == "2" + post_to_chain("minus", params[:token].to_i, current_user.try(:login)) + end + @issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create") + render json: {status: 0, message: "创建成", id: @issue.id} + else + normal_status(-1, "创建失败") + end + rescue Exception => exception + puts exception.message + normal_status(-1, exception.message) end def edit @@ -199,7 +194,7 @@ class IssuesController < ApplicationController normal_status(-1, "不允许修改为关闭状态") else issue_params = issue_send_params(params).except(:issue_classify, :author_id, :project_id) - + Issues::UpdateForm.new({subject:issue_params[:subject]}).validate! if @issue.update_attributes(issue_params) if params[:status_id].to_i == 5 #任务由非关闭状态到关闭状态时 @issue.issue_times.update_all(end_time: Time.now) @@ -225,6 +220,9 @@ class IssuesController < ApplicationController normal_status(-1, "更新失败") end end + rescue Exception => exception + puts exception.message + normal_status(-1, exception.message) end def show diff --git a/app/controllers/organizations/organizations_controller.rb b/app/controllers/organizations/organizations_controller.rb index 09c12fd5a..c8f4f4aa0 100644 --- a/app/controllers/organizations/organizations_controller.rb +++ b/app/controllers/organizations/organizations_controller.rb @@ -36,6 +36,7 @@ class Organizations::OrganizationsController < Organizations::BaseController def update ActiveRecord::Base.transaction do + Organizations::CreateForm.new(organization_params).validate! login = @organization.login @organization.login = organization_params[:name] if organization_params[:name].present? @organization.nickname = organization_params[:nickname] if organization_params[:nickname].present? diff --git a/app/controllers/organizations/teams_controller.rb b/app/controllers/organizations/teams_controller.rb index a8d06ce46..c599ac420 100644 --- a/app/controllers/organizations/teams_controller.rb +++ b/app/controllers/organizations/teams_controller.rb @@ -43,6 +43,7 @@ class Organizations::TeamsController < Organizations::BaseController end def update + Organizations::CreateTeamForm.new(team_params).validate! @team = Organizations::Teams::UpdateService.call(current_user, @team, team_params) rescue Exception => e uid_logger_error(e.message) diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index ed63ab1d1..500c584cd 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -106,7 +106,7 @@ class ProjectsController < ApplicationController def update ActiveRecord::Base.transaction do - # Projects::CreateForm.new(project_params).validate! + Projects::UpdateForm.new(project_params).validate! private = params[:private] || false new_project_params = project_params.except(:private).merge(is_public: !private) diff --git a/app/forms/issues/create_form.rb b/app/forms/issues/create_form.rb new file mode 100644 index 000000000..7dde8ecda --- /dev/null +++ b/app/forms/issues/create_form.rb @@ -0,0 +1,11 @@ +class Issues::CreateForm + include ActiveModel::Model + + attr_accessor :subject + + validates :subject, presence: { message: "不能为空" } + + validates :subject, length: { maximum: 80, too_long: "不能超过80个字符" } + + +end diff --git a/app/forms/issues/update_form.rb b/app/forms/issues/update_form.rb new file mode 100644 index 000000000..7447c8cc0 --- /dev/null +++ b/app/forms/issues/update_form.rb @@ -0,0 +1,10 @@ +class Issues::UpdateForm + include ActiveModel::Model + + attr_accessor :subject + + validates :subject, presence: { message: "不能为空" } + + validates :subject, length: { maximum: 80, too_long: "不能超过80个字符" } + +end \ No newline at end of file diff --git a/app/forms/organizations/create_form.rb b/app/forms/organizations/create_form.rb index 00c1dd15e..d92a99040 100644 --- a/app/forms/organizations/create_form.rb +++ b/app/forms/organizations/create_form.rb @@ -3,6 +3,9 @@ class Organizations::CreateForm < BaseForm attr_accessor :name, :description, :website, :location, :repo_admin_change_team_access, :visibility, :max_repo_creation, :nickname validates :name, :nickname, :visibility, presence: true + validates :name, :nickname, length: { maximum: 100 } + validates :location, length: { maximum: 50 } + validates :description, length: { maximum: 200 } validates :name, format: { with: NAME_REGEX, multiline: true, message: "只能含有数字、字母、下划线且不能以下划线开头和结尾" } end diff --git a/app/forms/organizations/create_team_form.rb b/app/forms/organizations/create_team_form.rb index 898d69a1d..6d816b0ed 100644 --- a/app/forms/organizations/create_team_form.rb +++ b/app/forms/organizations/create_team_form.rb @@ -2,7 +2,9 @@ class Organizations::CreateTeamForm < BaseForm NAME_REGEX = /^(?!_)(?!.*?_$)[a-zA-Z0-9_-]+$/ #只含有数字、字母、下划线不能以下划线开头和结尾 attr_accessor :name, :nickname, :description, :authorize, :includes_all_project, :can_create_org_project, :unit_types - validates :name, :nickname, :authorize, presence: true + validates :name, :nickname, presence: true + validates :name, :nickname, length: { maximum: 100 } + validates :description, length: { maximum: 200 } validates :name, format: { with: NAME_REGEX, multiline: true, message: "只能含有数字、字母、下划线且不能以下划线开头和结尾" } end diff --git a/app/forms/projects/create_form.rb b/app/forms/projects/create_form.rb index 2838996b6..0b57f215b 100644 --- a/app/forms/projects/create_form.rb +++ b/app/forms/projects/create_form.rb @@ -2,11 +2,15 @@ class Projects::CreateForm < BaseForm REPOSITORY_NAME_REGEX = /^(?!_)(?!.*?_$)[a-zA-Z0-9_-]+$/ #只含有数字、字母、下划线不能以下划线开头和结尾 attr_accessor :user_id, :name, :description, :repository_name, :project_category_id, :project_language_id, :ignore_id, :license_id, :private, :owner - + validates :user_id, :name, :description,:repository_name, :project_category_id, :project_language_id, presence: true validates :repository_name, format: { with: REPOSITORY_NAME_REGEX, multiline: true, message: "只能含有数字、字母、下划线且不能以下划线开头和结尾" } + validates :name, length: { maximum: 50 } + validates :repository_name, length: { maximum: 100 } + validates :description, length: { maximum: 200 } + validate :check_ignore, :check_license, :check_owner, :check_max_repo_creation validate do check_project_category(project_category_id) diff --git a/app/forms/projects/update_form.rb b/app/forms/projects/update_form.rb index 4696beaa3..65810a820 100644 --- a/app/forms/projects/update_form.rb +++ b/app/forms/projects/update_form.rb @@ -1,4 +1,11 @@ class Projects::UpdateForm < BaseForm - attr_reader :name, :description, :repository_name, :project_category_id + attr_accessor :name, :description, :project_category_id, :project_language_id, :private + validates :name, :description, :project_category_id, :project_language_id, presence: true + validates :name, length: { maximum: 50 } + validates :description, length: { maximum: 200 } + validate do + check_project_category(project_category_id) + check_project_language(project_language_id) + end end diff --git a/app/forms/users/login_form.rb b/app/forms/users/login_form.rb new file mode 100644 index 000000000..2634a36cc --- /dev/null +++ b/app/forms/users/login_form.rb @@ -0,0 +1,8 @@ +class Users::LoginForm + include ActiveModel::Model + + attr_accessor :password, :login + + validates :login, presence: true + validates :password, presence: true, length: { minimum: 8, maximum: 16 }, format: { with: CustomRegexp::PASSWORD, message: "8~16位,支持字母数字和符号" } +end \ No newline at end of file diff --git a/app/models/project_unit.rb b/app/models/project_unit.rb index 1cb74fc4a..a07f62b4e 100644 --- a/app/models/project_unit.rb +++ b/app/models/project_unit.rb @@ -20,13 +20,16 @@ class ProjectUnit < ApplicationRecord validates :unit_type, uniqueness: { scope: :project_id} - def self.init_types(project_id) - ProjectUnit::unit_types.each do |_, v| + def self.init_types(project_id, project_type='common') + unit_types = project_type == 'sync_mirror' ? ProjectUnit::unit_types.except("pulls") : ProjectUnit::unit_types + unit_types.each do |_, v| self.create!(project_id: project_id, unit_type: v) end end def self.update_by_unit_types!(project, types) + # 同步镜像项目不能有合并请求模块 + types.delete("pulls") if project.sync_mirror? # 默认code类型自动创建 types << "code" project.project_units.where.not(unit_type: types).each(&:destroy!) diff --git a/app/models/team.rb b/app/models/team.rb index d17827da8..c25963905 100644 --- a/app/models/team.rb +++ b/app/models/team.rb @@ -33,9 +33,10 @@ class Team < ApplicationRecord enum authorize: {common: 0, read: 1, write: 2, admin: 3, owner: 4} - def self.build(organization_id, name, description, authorize, includes_all_project, can_create_org_project) + def self.build(organization_id, name, nickname, description, authorize, includes_all_project, can_create_org_project) self.create!(organization_id: organization_id, name: name, + nickname: nickname, description: description, authorize: authorize, includes_all_project: includes_all_project, diff --git a/app/services/organizations/teams/create_service.rb b/app/services/organizations/teams/create_service.rb index 81ad78772..c3fc5f599 100644 --- a/app/services/organizations/teams/create_service.rb +++ b/app/services/organizations/teams/create_service.rb @@ -28,6 +28,10 @@ class Organizations::Teams::CreateService < ApplicationService params[:name] end + def nickname + params[:nickname] + end + def description params[:description] end @@ -45,7 +49,7 @@ class Organizations::Teams::CreateService < ApplicationService end def create_team - @team = Team.build(org.id, name, description, authorize, + @team = Team.build(org.id, name, nickname, description, authorize, includes_all_project, can_create_org_project) end diff --git a/app/services/projects/migrate_service.rb b/app/services/projects/migrate_service.rb index 85648eda9..7df08f9eb 100644 --- a/app/services/projects/migrate_service.rb +++ b/app/services/projects/migrate_service.rb @@ -10,7 +10,7 @@ class Projects::MigrateService < ApplicationService def call @project = Project.new(project_params) if @project.save! - ProjectUnit.init_types(@project.id) + ProjectUnit.init_types(@project.id, project.project_type) Project.update_mirror_projects_count! @project.set_owner_permission(user) Repositories::MigrateService.new(user, @project, repository_params).call diff --git a/app/views/repositories/detail.json.jbuilder b/app/views/repositories/detail.json.jbuilder index 644a472ab..75b474ee0 100644 --- a/app/views/repositories/detail.json.jbuilder +++ b/app/views/repositories/detail.json.jbuilder @@ -80,7 +80,7 @@ json.contributors do total_count = @result[:contributor].size json.list @result[:contributor].each do |contributor| user = User.find_by(gitea_uid: contributor["id"]) - if contributor["login"] == "root" + if contributor["login"] == "root" || user.nil? total_count -= 1 next end diff --git a/config/locales/forms/create_issuse_form.zh-CN.yml b/config/locales/forms/create_issuse_form.zh-CN.yml new file mode 100644 index 000000000..643c68667 --- /dev/null +++ b/config/locales/forms/create_issuse_form.zh-CN.yml @@ -0,0 +1,7 @@ +'zh-CN': + activemodel: + attributes: + issues/create_form: + subject: 标题 + issues/update_form: + subject: 标题 \ No newline at end of file diff --git a/config/locales/forms/organizations_create_form.zh-CN.yml b/config/locales/forms/organizations_create_form.zh-CN.yml new file mode 100644 index 000000000..ab43b0941 --- /dev/null +++ b/config/locales/forms/organizations_create_form.zh-CN.yml @@ -0,0 +1,9 @@ +'zh-CN': + activemodel: + attributes: + organizations/create_form: + name: 组织账号 + nickname: 组织名称 + location: 组织所在地区 + description: 组织简介 + visibility: 组织可见性 \ No newline at end of file diff --git a/config/locales/forms/organizations_create_team_form.zh-CN.yml b/config/locales/forms/organizations_create_team_form.zh-CN.yml new file mode 100644 index 000000000..ebf40dde0 --- /dev/null +++ b/config/locales/forms/organizations_create_team_form.zh-CN.yml @@ -0,0 +1,7 @@ +'zh-CN': + activemodel: + attributes: + organizations/create_team_form: + name: 团队标识 + nickname: 团队名称 + description: 团队描述 \ No newline at end of file diff --git a/config/locales/forms/projects_create_form.zh-CN.yml b/config/locales/forms/projects_create_form.zh-CN.yml new file mode 100644 index 000000000..e36a0520a --- /dev/null +++ b/config/locales/forms/projects_create_form.zh-CN.yml @@ -0,0 +1,7 @@ +'zh-CN': + activemodel: + attributes: + projects/create_form: + name: 项目名称 + repository_name: 仓库名称 + description: 项目简介 \ No newline at end of file diff --git a/config/locales/forms/projects_update_form.zh-CN.yml b/config/locales/forms/projects_update_form.zh-CN.yml new file mode 100644 index 000000000..74804f6af --- /dev/null +++ b/config/locales/forms/projects_update_form.zh-CN.yml @@ -0,0 +1,8 @@ +'zh-CN': + activemodel: + attributes: + projects/update_form: + name: 项目名称 + description: 项目简介 + project_category_id: 项目类别 + project_language_id: 项目语言 \ No newline at end of file diff --git a/config/locales/forms/users_login_form.zh-CN.yml b/config/locales/forms/users_login_form.zh-CN.yml new file mode 100644 index 000000000..8b94db42b --- /dev/null +++ b/config/locales/forms/users_login_form.zh-CN.yml @@ -0,0 +1,6 @@ +'zh-CN': + activemodel: + attributes: + users/login_form: + login: 用户名 + password: 密码 \ No newline at end of file