From 8df12dfc45f5d913e79fbd42d33d8ef9638b3586 Mon Sep 17 00:00:00 2001 From: boxbeam Date: Thu, 25 Apr 2024 18:55:57 -0400 Subject: [PATCH] Extract constant for repository name regex --- ee/tabby-webserver/src/schema/constants.rs | 6 ++++++ ee/tabby-webserver/src/schema/git_repository.rs | 8 +------- .../src/schema/github_repository_provider.rs | 16 ++++++++-------- .../src/schema/gitlab_repository_provider.rs | 16 ++++++++-------- ee/tabby-webserver/src/schema/mod.rs | 1 + 5 files changed, 24 insertions(+), 23 deletions(-) create mode 100644 ee/tabby-webserver/src/schema/constants.rs diff --git a/ee/tabby-webserver/src/schema/constants.rs b/ee/tabby-webserver/src/schema/constants.rs new file mode 100644 index 000000000000..412b6c16af16 --- /dev/null +++ b/ee/tabby-webserver/src/schema/constants.rs @@ -0,0 +1,6 @@ +use lazy_static::lazy_static; +use regex::Regex; + +lazy_static! { + pub static ref REPOSITORY_NAME_REGEX: Regex = Regex::new("^[\\w-]+$").unwrap(); +} diff --git a/ee/tabby-webserver/src/schema/git_repository.rs b/ee/tabby-webserver/src/schema/git_repository.rs index b70ddb8e5905..9f41e2732029 100644 --- a/ee/tabby-webserver/src/schema/git_repository.rs +++ b/ee/tabby-webserver/src/schema/git_repository.rs @@ -1,21 +1,15 @@ use async_trait::async_trait; use juniper::{GraphQLObject, ID}; -use lazy_static::lazy_static; -use regex::Regex; use validator::Validate; use super::{repository::FileEntrySearchResult, Context, Result}; use crate::juniper::relay::NodeType; -lazy_static! { - static ref REPOSITORY_NAME_REGEX: Regex = Regex::new("^[a-zA-Z][\\w.-]+$").unwrap(); -} - #[derive(Validate)] pub struct CreateGitRepositoryInput { #[validate(regex( code = "name", - path = "self::REPOSITORY_NAME_REGEX", + path = "crate::schema::constants::REPOSITORY_NAME_REGEX", message = "Invalid repository name" ))] pub name: String, diff --git a/ee/tabby-webserver/src/schema/github_repository_provider.rs b/ee/tabby-webserver/src/schema/github_repository_provider.rs index 573e194b8c30..2251126cd215 100644 --- a/ee/tabby-webserver/src/schema/github_repository_provider.rs +++ b/ee/tabby-webserver/src/schema/github_repository_provider.rs @@ -1,20 +1,17 @@ use async_trait::async_trait; use chrono::{DateTime, Utc}; use juniper::{GraphQLInputObject, GraphQLObject, ID}; -use lazy_static::lazy_static; -use regex::Regex; use validator::Validate; use super::Context; use crate::{juniper::relay::NodeType, schema::Result}; -lazy_static! { - static ref GITHUB_REPOSITORY_PROVIDER_NAME_REGEX: Regex = Regex::new("^[\\w-]+$").unwrap(); -} - #[derive(GraphQLInputObject, Validate)] pub struct CreateGithubRepositoryProviderInput { - #[validate(regex(code = "displayName", path = "GITHUB_REPOSITORY_PROVIDER_NAME_REGEX"))] + #[validate(regex( + code = "displayName", + path = "crate::schema::constants::REPOSITORY_NAME_REGEX" + ))] pub display_name: String, #[validate(length(code = "access_token", min = 10))] pub access_token: String, @@ -23,7 +20,10 @@ pub struct CreateGithubRepositoryProviderInput { #[derive(GraphQLInputObject, Validate)] pub struct UpdateGithubRepositoryProviderInput { pub id: ID, - #[validate(regex(code = "displayName", path = "GITHUB_REPOSITORY_PROVIDER_NAME_REGEX"))] + #[validate(regex( + code = "displayName", + path = "crate::schema::constants::REPOSITORY_NAME_REGEX" + ))] pub display_name: String, #[validate(length(code = "access_token", min = 10))] pub access_token: String, diff --git a/ee/tabby-webserver/src/schema/gitlab_repository_provider.rs b/ee/tabby-webserver/src/schema/gitlab_repository_provider.rs index 28e475d6b308..6c33570c3860 100644 --- a/ee/tabby-webserver/src/schema/gitlab_repository_provider.rs +++ b/ee/tabby-webserver/src/schema/gitlab_repository_provider.rs @@ -1,20 +1,17 @@ use async_trait::async_trait; use chrono::{DateTime, Utc}; use juniper::{GraphQLInputObject, GraphQLObject, ID}; -use lazy_static::lazy_static; -use regex::Regex; use validator::Validate; use super::Context; use crate::{juniper::relay::NodeType, schema::Result}; -lazy_static! { - static ref GITLAB_REPOSITORY_PROVIDER_NAME_REGEX: Regex = Regex::new("^[\\w-]+$").unwrap(); -} - #[derive(GraphQLInputObject, Validate)] pub struct CreateGitlabRepositoryProviderInput { - #[validate(regex(code = "displayName", path = "GITLAB_REPOSITORY_PROVIDER_NAME_REGEX"))] + #[validate(regex( + code = "displayName", + path = "crate::schema::constants::REPOSITORY_NAME_REGEX" + ))] pub display_name: String, #[validate(length(code = "access_token", min = 10))] pub access_token: String, @@ -23,7 +20,10 @@ pub struct CreateGitlabRepositoryProviderInput { #[derive(GraphQLInputObject, Validate)] pub struct UpdateGitlabRepositoryProviderInput { pub id: ID, - #[validate(regex(code = "displayName", path = "GITLAB_REPOSITORY_PROVIDER_NAME_REGEX"))] + #[validate(regex( + code = "displayName", + path = "crate::schema::constants::REPOSITORY_NAME_REGEX" + ))] pub display_name: String, #[validate(length(code = "access_token", min = 10))] pub access_token: String, diff --git a/ee/tabby-webserver/src/schema/mod.rs b/ee/tabby-webserver/src/schema/mod.rs index decaa117fbc7..a0845c8b258a 100644 --- a/ee/tabby-webserver/src/schema/mod.rs +++ b/ee/tabby-webserver/src/schema/mod.rs @@ -1,5 +1,6 @@ pub mod analytic; pub mod auth; +pub mod constants; pub mod email; pub mod git_repository; pub mod github_repository_provider;