From e38539445d97cd2605805cd451b8268a8e97c27c Mon Sep 17 00:00:00 2001 From: jasder Date: Thu, 9 Sep 2021 14:34:29 +0800 Subject: [PATCH] FIX delete some mandatory field with migrate repo api --- api_document.md | 4 +--- app/controllers/projects_controller.rb | 1 - app/forms/base_form.rb | 2 +- app/forms/projects/create_form.rb | 3 +-- app/forms/projects/migrate_form.rb | 11 ++++------- app/libs/custom_regexp.rb | 4 ++++ app/services/repositories/migrate_service.rb | 2 +- 7 files changed, 12 insertions(+), 15 deletions(-) diff --git a/api_document.md b/api_document.md index ddb79195..8a579f8e 100644 --- a/api_document.md +++ b/api_document.md @@ -374,9 +374,7 @@ curl -X POST \ -d "user_id=36408" \ -d "clone_addr=https://gitea.com/mx8090alex/golden.git" \ -d "name=golden_mirror1" \ --d "description=golden_mirror" \ --d "project_category_id=1" \ --d "project_language_id=2" \ +-d "repository_name=golden_mirror1" \ http://localhost:3000/api/projects/migrate.json | jq ``` *请求参数说明:* diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index bdc96e90..9b390969 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -56,7 +56,6 @@ class ProjectsController < ApplicationController end def migrate - tip_exception("无法使用以下关键词:#{mirror_params[:repository_name]},请重新命名") if ReversedKeyword.is_reversed(mirror_params[:repository_name]).present? Projects::MigrateForm.new(mirror_params).validate! @project = diff --git a/app/forms/base_form.rb b/app/forms/base_form.rb index 1f601490..a939b199 100644 --- a/app/forms/base_form.rb +++ b/app/forms/base_form.rb @@ -19,7 +19,7 @@ class BaseForm end def check_reversed_keyword(repository_name) - raise "仓库名称已被使用." if ReversedKeyword.is_reversed(repository_name).exists? + raise "仓库名称已被占用." if ReversedKeyword.is_reversed(repository_name).exists? end end diff --git a/app/forms/projects/create_form.rb b/app/forms/projects/create_form.rb index 7014da2b..a45da1a8 100644 --- a/app/forms/projects/create_form.rb +++ b/app/forms/projects/create_form.rb @@ -1,10 +1,9 @@ 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, :repository_name, presence: true - validates :repository_name, format: { with: REPOSITORY_NAME_REGEX, multiline: true, message: "只能含有数字、字母、下划线且不能以下划线开头和结尾" } + validates :repository_name, format: { with: CustomRegexp::REPOSITORY_NAME_REGEX, multiline: true, message: "只能含有数字、字母、下划线且不能以下划线开头和结尾" } validates :name, length: { maximum: 50 } validates :repository_name, length: { maximum: 100 } diff --git a/app/forms/projects/migrate_form.rb b/app/forms/projects/migrate_form.rb index 51116b2b..13a3af2c 100644 --- a/app/forms/projects/migrate_form.rb +++ b/app/forms/projects/migrate_form.rb @@ -1,12 +1,9 @@ class Projects::MigrateForm < BaseForm - REPOSITORY_NAME_REGEX = /^(?!_)(?!.*?_$)[a-zA-Z0-9_-]+$/ #只含有数字、字母、下划线不能以下划线开头和结尾 - URL_REGEX = /\A(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?\z/i + attr_accessor :user_id, :name, :repository_name, :project_category_id, :project_language_id, :clone_addr, :private, :is_mirror, :auth_username, :auth_password, :owner - attr_accessor :user_id, :name, :description, :repository_name, :project_category_id, :project_language_id, :clone_addr, :private, :is_mirror, :auth_username, :auth_password, :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 :clone_addr, format: { with: URL_REGEX, multiline: true, message: "地址格式不正确" } + validates :user_id, :name, :repository_name, :clone_addr, presence: true + validates :repository_name, format: { with: CustomRegexp::REPOSITORY_NAME_REGEX, multiline: true, message: "只能含有数字、字母、下划线且不能以下划线开头和结尾" } + validates :clone_addr, format: { with: CustomRegexp::URL_REGEX, multiline: true, message: "地址格式不正确" } validate do check_project_name(user_id, name) unless name.blank? check_repository_name(user_id, repository_name) unless repository_name.blank? diff --git a/app/libs/custom_regexp.rb b/app/libs/custom_regexp.rb index 4aa6fac2..c7b5e7a1 100644 --- a/app/libs/custom_regexp.rb +++ b/app/libs/custom_regexp.rb @@ -6,4 +6,8 @@ module CustomRegexp PASSWORD = /\A[a-z_A-Z0-9\-\.!@#\$%\\\^&\*\)\(\+=\{\}\[\]\/",'_<>~\·`\?:;|]{8,16}\z/ URL = /\Ahttps?:\/\/[-A-Za-z0-9+&@#\/%?=~_|!:,.;]+[-A-Za-z0-9+&@#\/%=~_|]\z/ IP = /^((\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$/ + + URL_REGEX = /\A(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?\z/i + REPOSITORY_NAME_REGEX = /^(?!_)(?!.*?_$)[a-zA-Z0-9_-]+$/ #只含有数字、字母、下划线不能以下划线开头和结尾 + end diff --git a/app/services/repositories/migrate_service.rb b/app/services/repositories/migrate_service.rb index 2870faba..cccfaed0 100644 --- a/app/services/repositories/migrate_service.rb +++ b/app/services/repositories/migrate_service.rb @@ -32,7 +32,7 @@ class Repositories::MigrateService < ApplicationService private: params[:hidden], mirror: wrapper_mirror || false, auth_username: params[:login], - auth_password: Base64.decode64(params[:password]) + auth_password: Base64.decode64(params[:password] || "") } end