Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default function GenerateTokenModal({
projectId,
tokenType,
isAdmin,
existingTokenNames = [],
}: GenerateTokenModalProps): JSX.Element {
const [robotName, setRobotName] = useState("");
const [robotNameError, setRobotNameError] = useState<string | null>(null);
Expand All @@ -59,6 +60,7 @@ export default function GenerateTokenModal({
const [createdForError, setCreatedForError] = useState<string | null>(null);

const [secret, setSecret] = useState<string | null>(null);
const [fullTokenName, setFullTokenName] = useState<string | null>(null);
const [showSecret, setShowSecret] = useState(false);
const [copiedSecret, setCopiedSecret] = useState(false);
const [copiedName, setCopiedName] = useState(false);
Expand All @@ -74,6 +76,8 @@ export default function GenerateTokenModal({
if (!value.trim()) return "Token name is required.";
if (!REGISTRY_TOKEN_ROBOT_NAME_REGEX.test(value))
return "Only lowercase letters, numbers, and dashes are allowed.";
if (existingTokenNames.some((n) => n.toLowerCase() === value.trim().toLowerCase()))
return "A token with this name already exists.";
return null;
}

Expand Down Expand Up @@ -101,6 +105,7 @@ export default function GenerateTokenModal({
{
onSuccess: (data) => {
setSecret(data.secret);
setFullTokenName(data.name ?? null);
},
},
);
Expand All @@ -113,6 +118,7 @@ export default function GenerateTokenModal({
setSelectedUser(null);
setCreatedForError(null);
setSecret(null);
setFullTokenName(null);
setShowSecret(false);
setCopiedSecret(false);
setCopiedName(false);
Expand Down Expand Up @@ -165,14 +171,14 @@ export default function GenerateTokenModal({
<TextField
label="Token Name"
fullWidth
value={robotName}
value={fullTokenName ?? robotName}
InputProps={{
readOnly: true,
endAdornment: (
<InputAdornment position="end">
<IconButton
size="small"
onClick={() => handleCopy(robotName, "name")}
onClick={() => handleCopy(fullTokenName ?? robotName, "name")}
color={copiedName ? "success" : "default"}
aria-label="Copy token name"
>
Expand Down Expand Up @@ -243,8 +249,7 @@ export default function GenerateTokenModal({
onChange={(e) => {
const value = e.target.value.toLowerCase();
setRobotName(value);
if (robotNameError)
setRobotNameError(validateRobotName(value));
setRobotNameError(validateRobotName(value));
}}
onBlur={() => setRobotNameError(validateRobotName(robotName))}
error={!!robotNameError}
Expand Down Expand Up @@ -319,7 +324,7 @@ export default function GenerateTokenModal({
variant="contained"
color="warning"
onClick={handleGenerate}
disabled={createMutation.isPending}
disabled={createMutation.isPending || !!robotNameError || (tokenType === RegistryTokenType.SERVICE && !selectedUser)}
startIcon={
createMutation.isPending ? (
<CircularProgress size={16} color="inherit" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default function RegenerateTokenModal({
token,
}: RegenerateTokenModalProps): JSX.Element {
const [secret, setSecret] = useState<string | null>(null);
const [fullTokenName, setFullTokenName] = useState<string | null>(null);
const [showSecret, setShowSecret] = useState(false);
const [copiedSecret, setCopiedSecret] = useState(false);
const [copiedName, setCopiedName] = useState(false);
Expand All @@ -57,12 +58,14 @@ export default function RegenerateTokenModal({
regenerateMutation.mutate(token.id, {
onSuccess: (data) => {
setSecret(data.secret);
setFullTokenName(data.name ?? null);
},
});
}

function handleClose() {
setSecret(null);
setFullTokenName(null);
setShowSecret(false);
setCopiedSecret(false);
setCopiedName(false);
Expand Down Expand Up @@ -115,7 +118,7 @@ export default function RegenerateTokenModal({
<TextField
label="Token Name"
fullWidth
value={token?.displayName ?? token?.name}
value={fullTokenName ?? token?.name}
InputProps={{
readOnly: true,
endAdornment: (
Expand All @@ -124,7 +127,7 @@ export default function RegenerateTokenModal({
size="small"
onClick={() =>
handleCopy(
token?.displayName ?? token?.name ?? "",
fullTokenName ?? token?.name ?? "",
"name",
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,7 @@ export default function SettingsRegistryTokens({
: RegistryTokenType.USER
}
isAdmin={isAdmin}
existingTokenNames={allTokens.map((t) => t.displayName ?? t.name)}
/>

<DeleteTokenModal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type RegistryToken = AuditMetadata & {
// Response type for creating a registry token.
export type RegistryTokenCreationResponse = {
secret: string;
name?: string;
};

// Request type for creating a registry token.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export type GenerateTokenModalProps = {
projectId: string;
tokenType: RegistryTokenType;
isAdmin: boolean;
existingTokenNames?: string[];
};

export type DeleteTokenModalProps = {
Expand Down