fix: gracefully degrade when widget_secret table is missing or access denied
The releases widget was crashing because throwIfActionForbiddenAsync or the widgetSecrets query would throw before DB migrations are run. Wrap in try/catch so the widget works without tokens in that case.
This commit is contained in:
1 file changed
+20
-16
@@ -48,26 +48,30 @@ export const releasesRouter = createTRPCRouter({
|
|||||||
let allowedRepoIds: Set<string> | null = null;
|
let allowedRepoIds: Set<string> | null = null;
|
||||||
|
|
||||||
if (input.itemId) {
|
if (input.itemId) {
|
||||||
const item = await ctx.db.query.items.findFirst({ where: eq(items.id, input.itemId) });
|
try {
|
||||||
if (item && item.kind === "releases") {
|
const item = await ctx.db.query.items.findFirst({ where: eq(items.id, input.itemId) });
|
||||||
await throwIfActionForbiddenAsync(ctx, eq(boards.id, item.boardId), "view");
|
if (item && item.kind === "releases") {
|
||||||
|
await throwIfActionForbiddenAsync(ctx, eq(boards.id, item.boardId), "view");
|
||||||
|
|
||||||
const options = SuperJSON.parse<Record<string, unknown>>(item.options);
|
const options = SuperJSON.parse<Record<string, unknown>>(item.options);
|
||||||
const repos = options.repositories as Array<{ id?: string }> | undefined;
|
const repos = options.repositories as Array<{ id?: string }> | undefined;
|
||||||
if (repos) {
|
if (repos) {
|
||||||
allowedRepoIds = new Set(repos.map((r) => r.id).filter((id): id is string => Boolean(id)));
|
allowedRepoIds = new Set(repos.map((r) => r.id).filter((id): id is string => Boolean(id)));
|
||||||
}
|
}
|
||||||
|
|
||||||
const secrets = await ctx.db.query.widgetSecrets.findMany({
|
const secrets = await ctx.db.query.widgetSecrets.findMany({
|
||||||
where: eq(widgetSecrets.itemId, input.itemId),
|
where: eq(widgetSecrets.itemId, input.itemId),
|
||||||
});
|
});
|
||||||
for (const secret of secrets) {
|
for (const secret of secrets) {
|
||||||
try {
|
try {
|
||||||
tokensByProvider.set(secret.kind, decryptSecret(secret.value));
|
tokensByProvider.set(secret.kind, decryptSecret(secret.value));
|
||||||
} catch {
|
} catch {
|
||||||
// Skip corrupt secrets
|
// Skip corrupt secrets
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch {
|
||||||
|
// Access denied or table not yet migrated -- proceed without tokens
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user