Skip to content

Commit

Permalink
fix(repository name): Repository name length can be up to 63 and not …
Browse files Browse the repository at this point in the history
…only 30 characters
  • Loading branch information
xrutayisire committed Oct 19, 2023
1 parent 7019711 commit a64c55f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
23 changes: 16 additions & 7 deletions packages/init/src/SliceMachineInitProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
SliceMachineManager,
PackageManager,
StarterId,
REPOSITORY_NAME_VALIDATION,
} from "@slicemachine/manager";

import pkg from "../package.json";
Expand Down Expand Up @@ -683,7 +684,7 @@ export class SliceMachineInitProcess {
this.manager.prismicRepository.checkExists({ domain }),
});

if (validation.LessThan4 || validation.MoreThan30) {
if (validation.Min || validation.Max) {
const errorMessage = getErrorMessageForRepositoryDomainValidation({
validation: {
...validation,
Expand Down Expand Up @@ -846,17 +847,25 @@ export class SliceMachineInitProcess {
const domain = formatRepositoryDomain(rawDomain);
const validation = validateRepositoryDomain({ domain });

const minRule = validation.LessThan4
const minRule = validation.Min
? chalk.red(
`1. Name must be ${chalk.bold("4 characters long or more")}`,
`1. Name must be ${chalk.bold(
REPOSITORY_NAME_VALIDATION.min + " characters long or more",
)}`,
)
: `1. Name must be ${chalk.cyan("4 characters long or more")}`;
: `1. Name must be ${chalk.cyan(
REPOSITORY_NAME_VALIDATION.min + " characters long or more",
)}`;

const maxRule = validation.MoreThan30
const maxRule = validation.Max
? chalk.red(
`1. Name must be ${chalk.bold("30 characters long or less")}`,
`1. Name must be ${chalk.bold(
REPOSITORY_NAME_VALIDATION.max + " characters long or less",
)}`,
)
: `1. Name must be ${chalk.cyan("30 characters long or less")}`;
: `1. Name must be ${chalk.cyan(
REPOSITORY_NAME_VALIDATION.max + " characters long or less",
)}`;

this.msg = chalk.reset(
`
Expand Down
22 changes: 14 additions & 8 deletions packages/init/src/lib/repositoryDomain.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { REPOSITORY_NAME_VALIDATION } from "@slicemachine/manager";

const abc123 = `abcdefghijklmnopqrstuvwxyz0123456789`;

// 11 characters long or less adjectives
Expand Down Expand Up @@ -114,8 +116,8 @@ const assertFormattedRepositoryDomain = (
};

export const ValidationErrors = {
LessThan4: "LessThan4",
MoreThan30: "MoreThan30",
Min: "Min",
Max: "Max",
AlreadyExists: "AlreadyExists",
} as const;
type ValidationErrors =
Expand All @@ -137,8 +139,8 @@ export const validateRepositoryDomain = (
*
* @see https://regex101.com/r/OBZ6UH/1
*/
[ValidationErrors.LessThan4]: args.domain.length < 4,
[ValidationErrors.MoreThan30]: args.domain.length > 30,
[ValidationErrors.Min]: args.domain.length < REPOSITORY_NAME_VALIDATION.Min,
[ValidationErrors.Max]: args.domain.length > REPOSITORY_NAME_VALIDATION.Max,
};

return {
Expand Down Expand Up @@ -191,11 +193,15 @@ export const getErrorMessageForRepositoryDomainValidation = (
if (args.validation.hasErrors) {
const formattedErrors: string[] = [];

if (args.validation.LessThan4) {
formattedErrors.push("must be 4 characters long or more");
if (args.validation.Min) {
formattedErrors.push(
`must be ${REPOSITORY_NAME_VALIDATION.Min} characters long or more`,
);
}
if (args.validation.MoreThan30) {
formattedErrors.push("must be 30 characters long or less");
if (args.validation.Max) {
formattedErrors.push(
`must be ${REPOSITORY_NAME_VALIDATION.Max} characters long or less`,
);
}
if (
ValidationErrors.AlreadyExists in args.validation &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ it("throws if repository name is too long", async () => {
return initProcess.useRepositoryFlag();
}),
).rejects.toThrowErrorMatchingInlineSnapshot(
'"Repository name lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum must be 30 characters long or less"',
'"Repository name lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum must be 63 characters long or less"',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ it("gets an error message when there is validation errors for the repository dom
"lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum",
}),
).toMatchInlineSnapshot(
'"Repository name lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum must be 30 characters long or less"',
'"Repository name lorem-ipsum-dolor-sit-amet-consectetur-adipisicing-elit-officiis-incidunt-ex-harum must be 63 characters long or less"',
);

const existsValidation = await validateRepositoryDomainAndAvailability({
Expand Down
4 changes: 4 additions & 0 deletions packages/manager/src/constants/REPOSITORY_NAME_VALIDATION.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const REPOSITORY_NAME_VALIDATION = {
Min: 4,
Max: 63,
};
2 changes: 2 additions & 0 deletions packages/manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ export { DecodeError } from "./lib/DecodeError";

export type { SliceMachineConfig, PackageManager } from "./types";
export type { APIEndpoints } from "./constants/API_ENDPOINTS";

export { REPOSITORY_NAME_VALIDATION } from "./constants/REPOSITORY_NAME_VALIDATION";
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { serializeCookies } from "../../lib/serializeCookies";

import { SLICE_MACHINE_USER_AGENT } from "../../constants/SLICE_MACHINE_USER_AGENT";
import { API_ENDPOINTS } from "../../constants/API_ENDPOINTS";
import { REPOSITORY_NAME_VALIDATION } from "../../constants/REPOSITORY_NAME_VALIDATION";

import { BaseManager } from "../BaseManager";

Expand Down Expand Up @@ -173,9 +174,13 @@ export class PrismicRepositoryManager extends BaseManager {
});
const text = await res.text();

// Endpoint returns repository name on success, which must be more than 4 characters and less than 30
// Endpoint returns repository name on success, that should be within the validation range
// Even when there is an error, we get a 200 success and so we have to check the name thanks to that
if (!res.ok || text.length < 4 || text.length > 63) {
if (
!res.ok ||
text.length < REPOSITORY_NAME_VALIDATION.Min ||
text.length > REPOSITORY_NAME_VALIDATION.Max
) {
throw new Error(`Failed to create repository \`${args.domain}\``, {
cause: text,
});
Expand Down

0 comments on commit a64c55f

Please sign in to comment.