Skip to content

Commit

Permalink
ffix(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 1814bc2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
17 changes: 13 additions & 4 deletions packages/init/src/SliceMachineInitProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { assertExists } from "./lib/assertExists";
import {
GIGET_ORGANIZATION,
GIGET_PROVIDER,
REPOSITORY_NAME_VALIDATION,
SLICE_MACHINE_INIT_USER_AGENT,
START_SCRIPT_KEY,
START_SCRIPT_VALUE,
Expand Down Expand Up @@ -848,15 +849,23 @@ export class SliceMachineInitProcess {

const minRule = validation.LessThan4
? 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
? 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
4 changes: 4 additions & 0 deletions packages/init/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export const SLICE_MACHINE_INIT_USER_AGENT = "slice-machine-init";
export const SENTRY_EXPRESS_DSN =
import.meta.env.VITE_SENTRY_EXPRESS_DSN ||
"https://[email protected]/4505917981982720";
export const REPOSITORY_NAME_VALIDATION = {
min: 4,
max: 63,
};
16 changes: 12 additions & 4 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 "../constants";

const abc123 = `abcdefghijklmnopqrstuvwxyz0123456789`;

// 11 characters long or less adjectives
Expand Down Expand Up @@ -137,8 +139,10 @@ export const validateRepositoryDomain = (
*
* @see https://regex101.com/r/OBZ6UH/1
*/
[ValidationErrors.LessThan4]: args.domain.length < 4,
[ValidationErrors.MoreThan30]: args.domain.length > 30,
[ValidationErrors.LessThan4]:
args.domain.length < REPOSITORY_NAME_VALIDATION.min,
[ValidationErrors.MoreThan30]:
args.domain.length > REPOSITORY_NAME_VALIDATION.max,
};

return {
Expand Down Expand Up @@ -192,10 +196,14 @@ export const getErrorMessageForRepositoryDomainValidation = (
const formattedErrors: string[] = [];

if (args.validation.LessThan4) {
formattedErrors.push("must be 4 characters long or more");
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");
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.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const REPOSITORY_NAME_VALIDATION = {
min: 4,
max: 63,
};
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";

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 1814bc2

Please sign in to comment.