Skip to content

Conversation

@Shivam-nagar23
Copy link
Member

@Shivam-nagar23 Shivam-nagar23 commented Nov 7, 2025

Description

Fixes #

Checklist:

  • The title of the PR states what changed and the related issues number (used for the release note).
  • Does this PR requires documentation updates?
  • I've updated documentation as required by this PR.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have tested it for all user roles.
  • I have added all the required unit/api test cases.

Does this PR introduce a user-facing change?


Summary by Bito

  • This pull request introduces validation for environment names to ensure they are alphanumeric, do not start or end with a hyphen, and are between 1 to 16 characters long, which aims to prevent potential errors during environment creation.
  • The changes enforce stricter naming conventions for environment creation.
  • Overall summary: introduces validation for environment names, aiming to prevent errors.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds validation for environment names in the Create endpoint to ensure they follow specific naming conventions: lowercase alphanumeric characters or hyphens, cannot start/end with hyphens, and must be 1-16 characters in length.

Key Changes:

  • Added three compiled regex patterns for environment name validation
  • Implemented validateEnvironmentName function with three validation rules
  • Applied validation in the Create method before processing the request

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

var (
// Regex patterns for environment name validation
envNameAlphanumericRegex = regexp.MustCompile(`^[a-z0-9-]+$`)
envNameNoStartEndHyphen = regexp.MustCompile(`^(?![-]).*[^-]$`)
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern ^(?![-]).*[^-]$ will fail for single-character names (e.g., 'a') because it requires at least two characters: one that doesn't match the negative lookahead and one that isn't a hyphen at the end. Consider using ^[^-].*[^-]$|^[^-]$ to handle single non-hyphen characters, or simplify to ^[^-]([a-z0-9-]*[^-])?$.

Suggested change
envNameNoStartEndHyphen = regexp.MustCompile(`^(?![-]).*[^-]$`)
envNameNoStartEndHyphen = regexp.MustCompile(`^[^-]([a-z0-9-]*[^-])?$`)

Copilot uses AI. Check for mistakes.
Comment on lines +131 to +132
if !envNameLengthRegex.MatchString(envName) {
return errors.New("Minimum 1 and Maximum 16 characters required")
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length validation using regex ^.{1,16}$ is less efficient and less clear than using len(envName). Consider replacing this with a simple length check: if len(envName) < 1 || len(envName) > 16. This is more performant and easier to understand.

Copilot uses AI. Check for mistakes.
Comment on lines 111 to 134
// Regex patterns for environment name validation
envNameAlphanumericRegex = regexp.MustCompile(`^[a-z0-9-]+$`)
envNameNoStartEndHyphen = regexp.MustCompile(`^(?![-]).*[^-]$`)
envNameLengthRegex = regexp.MustCompile(`^.{1,16}$`)
)

// validateEnvironmentName validates the environment name against multiple regex patterns
// Note: Required validation is already handled by struct validation tag
func (impl EnvironmentRestHandlerImpl) validateEnvironmentName(envName string) error {
// Validation 1: Use only lowercase alphanumeric characters or '-'
if !envNameAlphanumericRegex.MatchString(envName) {
return errors.New("Use only lowercase alphanumeric characters or '-'")
}

// Validation 2: Cannot start/end with '-'
if !envNameNoStartEndHyphen.MatchString(envName) {
return errors.New("Cannot start/end with '-'")
}

// Validation 3: Minimum 1 and Maximum 16 characters required
if !envNameLengthRegex.MatchString(envName) {
return errors.New("Minimum 1 and Maximum 16 characters required")
}

Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All three validations could be combined into a single comprehensive regex pattern ^[a-z0-9]([a-z0-9-]{0,14}[a-z0-9])?$, which would be more efficient (single pass) and simpler to maintain. This pattern enforces: starts with alphanumeric, ends with alphanumeric, allows hyphens in the middle, and limits length to 1-16 characters.

Suggested change
// Regex patterns for environment name validation
envNameAlphanumericRegex = regexp.MustCompile(`^[a-z0-9-]+$`)
envNameNoStartEndHyphen = regexp.MustCompile(`^(?![-]).*[^-]$`)
envNameLengthRegex = regexp.MustCompile(`^.{1,16}$`)
)
// validateEnvironmentName validates the environment name against multiple regex patterns
// Note: Required validation is already handled by struct validation tag
func (impl EnvironmentRestHandlerImpl) validateEnvironmentName(envName string) error {
// Validation 1: Use only lowercase alphanumeric characters or '-'
if !envNameAlphanumericRegex.MatchString(envName) {
return errors.New("Use only lowercase alphanumeric characters or '-'")
}
// Validation 2: Cannot start/end with '-'
if !envNameNoStartEndHyphen.MatchString(envName) {
return errors.New("Cannot start/end with '-'")
}
// Validation 3: Minimum 1 and Maximum 16 characters required
if !envNameLengthRegex.MatchString(envName) {
return errors.New("Minimum 1 and Maximum 16 characters required")
}
// Comprehensive regex pattern for environment name validation:
// - Only lowercase alphanumeric and hyphens
// - Must start and end with alphanumeric
// - Hyphens allowed only in the middle
// - Length: 1-16 characters
envNameComprehensiveRegex = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,14}[a-z0-9])?$`)
)
// validateEnvironmentName validates the environment name against multiple regex patterns
// Note: Required validation is already handled by struct validation tag
func (impl EnvironmentRestHandlerImpl) validateEnvironmentName(envName string) error {
// Validate using a single comprehensive regex
if !envNameComprehensiveRegex.MatchString(envName) {
return errors.New("Environment name must be 1-16 characters, use only lowercase alphanumeric characters or '-', cannot start or end with '-'")
}

Copilot uses AI. Check for mistakes.
@sonarqubecloud
Copy link

sonarqubecloud bot commented Nov 7, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants