Skip to content

Commit

Permalink
Extract constant for repository name regex
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Apr 25, 2024
1 parent 8d9d1db commit 8df12df
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
6 changes: 6 additions & 0 deletions ee/tabby-webserver/src/schema/constants.rs
Original file line number Diff line number Diff line change
@@ -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();
}
8 changes: 1 addition & 7 deletions ee/tabby-webserver/src/schema/git_repository.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
16 changes: 8 additions & 8 deletions ee/tabby-webserver/src/schema/github_repository_provider.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
16 changes: 8 additions & 8 deletions ee/tabby-webserver/src/schema/gitlab_repository_provider.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-webserver/src/schema/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 8df12df

Please sign in to comment.