Skip to content

Commit

Permalink
Merge pull request #561 from SquirrelCorporation/555-invalid-project-…
Browse files Browse the repository at this point in the history
…name-error-in-deploy-custom-stack-compose

[BUG] Improve stack naming and validation logic.
  • Loading branch information
SquirrelDeveloper authored Dec 10, 2024
2 parents 71536d3 + b77e7b4 commit b4b4450
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
8 changes: 7 additions & 1 deletion client/src/components/ComposeEditor/StackPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ const StackPanel: React.FC<StackPanelProps> = ({
name={'name'}
label={'Stack name'}
initialValue={generateReadableName()}
rules={[{ required: true, message: 'Please enter a name' }]}
rules={[
{ required: true, message: 'Please enter a name' },
{
pattern: /^(?!.*[:\/\\_.]).*$/,
message: 'Characters not allowed',
},
]}
fieldProps={{
addonBefore: (
<StackIconSelector
Expand Down
8 changes: 7 additions & 1 deletion client/src/pages/ComposeEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,13 @@ const ComposeEditor = () => {
<ProFormText
name={'name'}
placeholder={'Name'}
rules={[{ required: true, message: 'Please enter a name' }]}
rules={[
{ required: true, message: 'Please enter a name' },
{
pattern: /^(?!.*[:\/\\_.]).*$/,
message: 'Characters not allowed',
},
]}
fieldProps={{
ref: nameRef,
defaultValue:
Expand Down
14 changes: 14 additions & 0 deletions server/src/helpers/docker/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import yaml from 'js-yaml';
import logger from '../../logger';

export function extractTopLevelName(content: string): string | undefined {
try {
const parsedYaml = yaml.load(content);
if (typeof parsedYaml === 'object' && parsedYaml !== null) {
return (parsedYaml as { [key: string]: any }).name;
}
} catch (err: any) {
logger.error('Error parsing YAML:' + err.message);
}
return undefined;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ContainerCustomStack from '../../data/database/model/ContainerCustomStack
import ContainerCustomStackRepository from '../../data/database/model/ContainerCustomStackRepository';
import ContainerCustomStackRepo from '../../data/database/repository/ContainerCustomStackRepo';
import ContainerStacksRepositoryRepo from '../../data/database/repository/ContainerCustomStackRepositoryRepo';
import { extractTopLevelName } from '../../helpers/docker/utils';
import { FileInfo, getMatchingFiles } from '../../helpers/files/recursive-find';
import logger from '../../logger';
import { NotFoundError } from '../../middlewares/api/ApiError';
Expand Down Expand Up @@ -152,11 +153,18 @@ class ContainerCustomStacksRepositoryComponent extends EventManager {
`Processing stack ${JSON.stringify(foundStack)} - In database: ${stackFoundInDatabase ? 'true' : 'false'}`,
);
const stackContent = FileSystemManager.readFile(foundStack.fullPath as string);
const embeddedProjectName = extractTopLevelName(stackContent);
const stackData: ContainerCustomStack = {
path: foundStack.fullPath,
name:
stackFoundInDatabase?.name ||
foundStack.fullPath.split(`${this.uuid}/`)[1].replaceAll('/', '_').toLowerCase(),
embeddedProjectName ||
foundStack.fullPath
.split(`${this.uuid}/`)[1]
.replaceAll('/', '-')
.replaceAll(':', '-')
.replaceAll('.', '-')
.toLowerCase(),
containerCustomStackRepository: containerCustomStackRepository,
uuid: stackFoundInDatabase?.uuid || uuidv4(),
lockJson: true,
Expand Down
1 change: 1 addition & 0 deletions shared-lib/src/types/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ export type ContainerCustomStack = {
json: object;
yaml: string;
name: string;
type: string;
rawStackValue: object;
lockJson: boolean;
icon: string;
Expand Down

0 comments on commit b4b4450

Please sign in to comment.