mirror of
https://github.com/Dokploy/dokploy
synced 2025-06-26 18:27:59 +00:00
Fixed compose bug and formatted. Updated the refresh token to check the expired time.
This commit is contained in:
parent
5927c7c3c5
commit
66d6cb5710
@ -1,7 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import type { Schema } from "@dokploy/server/templates";
|
||||
import type { CompleteTemplate } from "@dokploy/server/templates/processors";
|
||||
import { processTemplate } from "@dokploy/server/templates/processors";
|
||||
import type { Schema } from "@dokploy/server/templates";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
describe("processTemplate", () => {
|
||||
// Mock schema for testing
|
||||
|
@ -496,4 +496,4 @@ export const SaveGiteaProvider = ({ applicationId }: Props) => {
|
||||
</Form>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
@ -71,7 +71,6 @@ interface Props {
|
||||
export const SaveGiteaProviderCompose = ({ composeId }: Props) => {
|
||||
const { data: giteaProviders } = api.gitea.giteaProviders.useQuery();
|
||||
const { data, refetch } = api.compose.one.useQuery({ composeId });
|
||||
|
||||
const { mutateAsync, isLoading: isSavingGiteaProvider } =
|
||||
api.compose.update.useMutation();
|
||||
|
||||
@ -97,11 +96,9 @@ export const SaveGiteaProviderCompose = ({ composeId }: Props) => {
|
||||
{ giteaId },
|
||||
{
|
||||
enabled: !!giteaId,
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
console.log(repository);
|
||||
|
||||
const {
|
||||
data: repositories,
|
||||
isLoading: isLoadingRepositories,
|
||||
@ -133,24 +130,55 @@ export const SaveGiteaProviderCompose = ({ composeId }: Props) => {
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
// Find the repository in the repositories list to get the correct ID
|
||||
const currentRepo = repositories?.find(
|
||||
(repo) => repo.name === data.giteaRepository && repo.owner.username === data.giteaOwner
|
||||
);
|
||||
|
||||
form.reset({
|
||||
branch: data.giteaBranch || "",
|
||||
repository: {
|
||||
repo: data.giteaRepository || "",
|
||||
owner: data.giteaOwner || "",
|
||||
id: currentRepo?.id || null,
|
||||
},
|
||||
composePath: data.composePath,
|
||||
giteaId: data.giteaId || "",
|
||||
watchPaths: data.watchPaths || [],
|
||||
});
|
||||
console.log("Setting form data from API:", data);
|
||||
|
||||
// Only reset form on initial load, not after user interactions
|
||||
if (!form.formState.isDirty && !form.formState.dirtyFields.giteaId) {
|
||||
console.log("Initial form reset from API data");
|
||||
form.reset({
|
||||
branch: data.giteaBranch || "",
|
||||
repository: {
|
||||
repo: data.giteaRepository || "",
|
||||
owner: data.giteaOwner || "",
|
||||
id: null,
|
||||
},
|
||||
composePath: data.composePath || "./docker-compose.yml",
|
||||
giteaId: data.giteaId || "",
|
||||
watchPaths: data.watchPaths || [],
|
||||
});
|
||||
} else {
|
||||
console.log(
|
||||
"Skipping form reset because form has been modified by user",
|
||||
);
|
||||
}
|
||||
}
|
||||
}, [data, form, repositories]);
|
||||
}, [data]);
|
||||
|
||||
// Add this separate effect to update repository ID if needed
|
||||
useEffect(() => {
|
||||
const values = form.getValues();
|
||||
// If we have a repository selected but no ID, try to find it
|
||||
if (
|
||||
values.repository.owner &&
|
||||
values.repository.repo &&
|
||||
!values.repository.id &&
|
||||
repositories?.length
|
||||
) {
|
||||
const matchingRepo = repositories.find(
|
||||
(repo) =>
|
||||
repo.name === values.repository.repo &&
|
||||
repo.owner.username === values.repository.owner,
|
||||
);
|
||||
|
||||
if (matchingRepo) {
|
||||
console.log("Found matching repository ID:", matchingRepo.id);
|
||||
form.setValue("repository", {
|
||||
...values.repository,
|
||||
id: matchingRepo.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
}, [repositories]);
|
||||
|
||||
const onSubmit = async (data: GiteaProvider) => {
|
||||
await mutateAsync({
|
||||
@ -165,7 +193,7 @@ export const SaveGiteaProviderCompose = ({ composeId }: Props) => {
|
||||
watchPaths: data.watchPaths,
|
||||
} as any)
|
||||
.then(async () => {
|
||||
toast.success("Service Provided Saved");
|
||||
toast.success("Service Provider Saved");
|
||||
await refetch();
|
||||
})
|
||||
.catch(() => {
|
||||
@ -191,14 +219,20 @@ export const SaveGiteaProviderCompose = ({ composeId }: Props) => {
|
||||
<FormLabel>Gitea Account</FormLabel>
|
||||
<Select
|
||||
onValueChange={(value) => {
|
||||
console.log("Select changed to:", value);
|
||||
field.onChange(value);
|
||||
form.setValue("repository", {
|
||||
owner: "",
|
||||
repo: "",
|
||||
id: null,
|
||||
});
|
||||
form.setValue("branch", "");
|
||||
form.setValue(
|
||||
"repository",
|
||||
{
|
||||
owner: "",
|
||||
repo: "",
|
||||
id: null,
|
||||
},
|
||||
{ shouldValidate: false },
|
||||
);
|
||||
form.setValue("branch", "", { shouldValidate: false });
|
||||
}}
|
||||
defaultValue={field.value}
|
||||
value={field.value}
|
||||
>
|
||||
<FormControl>
|
||||
@ -281,6 +315,10 @@ export const SaveGiteaProviderCompose = ({ composeId }: Props) => {
|
||||
key={repo.url}
|
||||
value={repo.name}
|
||||
onSelect={() => {
|
||||
console.log(
|
||||
"Repository selected:",
|
||||
repo.name,
|
||||
);
|
||||
form.setValue("repository", {
|
||||
owner: repo.owner.username,
|
||||
repo: repo.name,
|
||||
|
@ -66,7 +66,7 @@ export const ShowConvertedCompose = ({ composeId }: Props) => {
|
||||
Preview your docker-compose file with added domains. Note: At least
|
||||
one domain must be specified for this conversion to take effect.
|
||||
</AlertBlock>
|
||||
|
||||
|
||||
<div className="flex flex-row gap-2 justify-end">
|
||||
<Button
|
||||
variant="secondary"
|
||||
@ -89,18 +89,19 @@ export const ShowConvertedCompose = ({ composeId }: Props) => {
|
||||
</div>
|
||||
|
||||
{compose !== null ? (
|
||||
<pre>
|
||||
<CodeEditor
|
||||
value={compose || ""}
|
||||
language="yaml"
|
||||
readOnly
|
||||
height="50rem"
|
||||
/>
|
||||
</pre>
|
||||
<pre>
|
||||
<CodeEditor
|
||||
value={compose || ""}
|
||||
language="yaml"
|
||||
readOnly
|
||||
height="50rem"
|
||||
/>
|
||||
</pre>
|
||||
) : (
|
||||
<div className="py-4 text-center text-muted-foreground">
|
||||
No compose file available. Make sure at least one domain is configured for this project.
|
||||
</div>
|
||||
<div className="py-4 text-center text-muted-foreground">
|
||||
No compose file available. Make sure at least one domain is
|
||||
configured for this project.
|
||||
</div>
|
||||
)}
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
|
@ -220,27 +220,27 @@ export const giteaRouter = createTRPCRouter({
|
||||
getGiteaUrl: protectedProcedure
|
||||
.input(apiFindOneGitea)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { giteaId } = input;
|
||||
|
||||
if (!giteaId) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Gitea provider ID is required.",
|
||||
});
|
||||
}
|
||||
|
||||
const giteaProvider = await findGiteaById(giteaId);
|
||||
if (
|
||||
giteaProvider.gitProvider.organizationId !==
|
||||
ctx.session.activeOrganizationId
|
||||
) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You are not allowed to access this Gitea provider",
|
||||
});
|
||||
}
|
||||
|
||||
// Return the base URL of the Gitea instance
|
||||
return giteaProvider.giteaUrl;
|
||||
const { giteaId } = input;
|
||||
|
||||
if (!giteaId) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: "Gitea provider ID is required.",
|
||||
});
|
||||
}
|
||||
|
||||
const giteaProvider = await findGiteaById(giteaId);
|
||||
if (
|
||||
giteaProvider.gitProvider.organizationId !==
|
||||
ctx.session.activeOrganizationId
|
||||
) {
|
||||
throw new TRPCError({
|
||||
code: "UNAUTHORIZED",
|
||||
message: "You are not allowed to access this Gitea provider",
|
||||
});
|
||||
}
|
||||
|
||||
// Return the base URL of the Gitea instance
|
||||
return giteaProvider.giteaUrl;
|
||||
}),
|
||||
});
|
||||
|
@ -2,16 +2,16 @@ import type { IncomingMessage } from "node:http";
|
||||
import * as bcrypt from "bcrypt";
|
||||
import { betterAuth } from "better-auth";
|
||||
import { drizzleAdapter } from "better-auth/adapters/drizzle";
|
||||
import { organization, twoFactor, apiKey } from "better-auth/plugins";
|
||||
import { APIError } from "better-auth/api";
|
||||
import { apiKey, organization, twoFactor } from "better-auth/plugins";
|
||||
import { and, desc, eq } from "drizzle-orm";
|
||||
import { IS_CLOUD } from "../constants";
|
||||
import { db } from "../db";
|
||||
import * as schema from "../db/schema";
|
||||
import { sendEmail } from "../verification/send-verification-email";
|
||||
import { IS_CLOUD } from "../constants";
|
||||
import { getPublicIpWithFallback } from "../wss/utils";
|
||||
import { updateUser } from "../services/user";
|
||||
import { getUserByToken } from "../services/admin";
|
||||
import { APIError } from "better-auth/api";
|
||||
import { updateUser } from "../services/user";
|
||||
import { sendEmail } from "../verification/send-verification-email";
|
||||
import { getPublicIpWithFallback } from "../wss/utils";
|
||||
|
||||
const { handler, api } = betterAuth({
|
||||
database: drizzleAdapter(db, {
|
||||
|
@ -19,8 +19,7 @@ export const getErrorCloneRequirements = (entity: {
|
||||
giteaBranch?: string | null;
|
||||
}) => {
|
||||
const reasons: string[] = [];
|
||||
const { giteaBranch, giteaOwner, giteaRepository } =
|
||||
entity;
|
||||
const { giteaBranch, giteaOwner, giteaRepository } = entity;
|
||||
|
||||
if (!giteaRepository) reasons.push("1. Repository not assigned.");
|
||||
if (!giteaOwner) reasons.push("2. Owner not specified.");
|
||||
@ -41,6 +40,20 @@ export const refreshGiteaToken = async (giteaProviderId: string) => {
|
||||
return giteaProvider?.accessToken || null;
|
||||
}
|
||||
|
||||
// Check if token is still valid (add some buffer time, e.g., 5 minutes)
|
||||
const currentTimeSeconds = Math.floor(Date.now() / 1000);
|
||||
const bufferTimeSeconds = 300; // 5 minutes
|
||||
|
||||
if (
|
||||
giteaProvider.expiresAt &&
|
||||
giteaProvider.expiresAt > currentTimeSeconds + bufferTimeSeconds &&
|
||||
giteaProvider.accessToken
|
||||
) {
|
||||
// Token is still valid, no need to refresh
|
||||
return giteaProvider.accessToken;
|
||||
}
|
||||
|
||||
// Token is expired or about to expire, refresh it
|
||||
const tokenEndpoint = `${giteaProvider.giteaUrl}/login/oauth/access_token`;
|
||||
const params = new URLSearchParams({
|
||||
grant_type: "refresh_token",
|
||||
@ -409,6 +422,8 @@ export const getGiteaBranches = async (input: {
|
||||
return [];
|
||||
}
|
||||
|
||||
await refreshGiteaToken(input.giteaId);
|
||||
|
||||
const giteaProvider = await findGiteaById(input.giteaId);
|
||||
|
||||
const baseUrl = giteaProvider.giteaUrl.replace(/\/+$/, "");
|
||||
|
Loading…
Reference in New Issue
Block a user