Skip to content

Commit

Permalink
Merge pull request #6 from Design-System-Project/fix/db-scripts
Browse files Browse the repository at this point in the history
fix db migration script on github action
  • Loading branch information
tomasfrancisco authored Aug 21, 2024
2 parents b75dd22 + 7db10cb commit 31bfcd1
Show file tree
Hide file tree
Showing 25 changed files with 174 additions and 399 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/migrate-db.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
uses: ./tools/github/setup

- name: Run Drizzle Migration
working-directory: apps/dashboard
run: pnpm db:migrate
working-directory: packages/database
run: pnpm migrate
env: # Or as an environment variable
POSTGRES_URL: ${{ secrets.POSTGRES_URL }}
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './request-tokens.action';
export * from './select-repository.action';
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {
Text,
} from '@ds-project/components';
import { JsonBlock } from '@/components';
import { requestTokens } from './_actions';
import { api } from '@ds-project/api/rsc';

export default async function Tokens() {
const tokens = await requestTokens();
const tokens = await api.github.tokens();
return (
<Card className="w-full">
<CardHeader>
Expand Down
17 changes: 0 additions & 17 deletions apps/dashboard/src/app/api/figma/design-systems/link/route.ts

This file was deleted.

27 changes: 0 additions & 27 deletions apps/dashboard/src/app/api/figma/design-systems/list/route.ts

This file was deleted.

65 changes: 0 additions & 65 deletions apps/dashboard/src/app/api/figma/design-tokens/route.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/dashboard/src/lib/figma/figma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { and, eq } from 'drizzle-orm';
import { kv } from '@vercel/kv';
import { config } from '@/config';
import type { KVOAuthState } from '@/types/kv-types';
import { api } from '@ds-project/api/rsc';

import type { SelectFigmaIntegration } from '@ds-project/database/schema';
import {
Expand All @@ -16,6 +15,7 @@ import {
} from '@ds-project/database/schema';
import { database } from '@ds-project/database/client';
import { cache } from 'react';
import { api } from '@ds-project/api/rsc';

class Figma {
private apiUrl = 'https://api.figma.com';
Expand Down
2 changes: 2 additions & 0 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"style-dictionary": "catalog:",
"@ds-project/services": "workspace:*",
"@ds-project/auth": "workspace:*",
"@ds-project/database": "workspace:*",
"@tanstack/react-query": "^5.51.24",
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/root.ts → packages/api/src/app-router.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { accountsRouter } from './router/accounts';
import { githubRouter } from './router/github';
import { integrationsRouter } from './router/integrations';
import { projectsRouter } from './router/projects';
import { resourcesRouter } from './router/resources';
Expand All @@ -9,6 +10,7 @@ export const appRouter = createTRPCRouter({
integrations: integrationsRouter,
resources: resourcesRouter,
projects: projectsRouter,
github: githubRouter,
});

// export type definition of API
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { inferRouterInputs, inferRouterOutputs } from '@trpc/server';

import type { AppRouter } from './root';
import { appRouter } from './root';
import type { AppRouter } from './app-router';
import { appRouter } from './app-router';
import { createTRPCContext, createCallerFactory } from './trpc';

/**
Expand Down
36 changes: 36 additions & 0 deletions packages/api/src/queries/integrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { eq } from '@ds-project/database';
import type { DSContext } from '../types/context';

export const selectGithubIntegration = async ({ ctx }: { ctx: DSContext }) => {
const queryResult = await ctx.database.query.Integrations.findFirst({
columns: {
data: true,
},
where: (integrations) => eq(integrations.type, 'github'),
with: {
project: {
columns: {
id: true,
},
with: {
accountsToProjects: {
columns: {
projectId: true,
},
where: (accountsToProjects) =>
eq(accountsToProjects.accountId, ctx.account.id),
},
},
},
},
});

if (queryResult?.data?.type === 'github') {
return {
...queryResult,
data: queryResult.data,
};
}

return undefined;
};
2 changes: 1 addition & 1 deletion packages/api/src/react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { createTRPCReact } from '@trpc/react-query';
import SuperJSON from 'superjson';

import { createQueryClient } from './query-client';
import type { AppRouter } from './root';
import type { AppRouter } from './app-router';

let clientQueryClientSingleton: QueryClient | undefined = undefined;
const getQueryClient = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,12 @@
'use server';

import type { DesignTokens } from 'style-dictionary/types';
import type { Octokit } from '@octokit/core';
import type { RequestError } from '@octokit/types';

import { config } from '@/config';
import { api } from '@ds-project/api/rsc';
import { createTRPCRouter, protectedProcedure } from '../trpc';
import { getInstallationOctokit } from '@ds-project/services/github';
import { getRef } from '../../../services/src/github/utils/get-ref';
import type { DesignTokens } from 'style-dictionary/types';
import { selectGithubIntegration } from '../queries/integrations';

async function getRef({
branchName,
mainBranch,
octokit,
owner,
repo,
}: {
mainBranch: string;
branchName: string;
octokit: Octokit;
owner: string;
repo: string;
}) {
// Check if the branch exists
try {
await octokit.request('GET /repos/{owner}/{repo}/git/ref/{ref}', {
owner,
repo,
ref: `heads/${branchName}`,
});

// If the branch exists, use it
return branchName;
} catch (error) {
if ((error as RequestError).status !== 404) {
throw error; // Re-throw if it's not a 404 error
}
// If the branch does not exist, default to the main branch
return mainBranch;
}
}

export async function requestTokens() {
try {
const githubIntegration = await api.integrations.github();
export const githubRouter = createTRPCRouter({
tokens: protectedProcedure.query(async ({ ctx }) => {
const githubIntegration = await selectGithubIntegration({ ctx });

if (!githubIntegration) {
throw new Error('No GitHub integration found');
Expand All @@ -63,8 +27,8 @@ export async function requestTokens() {
if (!repository) throw new Error('No repository found');

const ref = await getRef({
branchName: 'ds-project/sync-tokens',
mainBranch: 'main',
targetBranchName: 'ds-project/sync-tokens',
baseBranchName: 'main',
octokit: octokit,
owner: repository.owner.login,
repo: repository.name,
Expand All @@ -75,7 +39,7 @@ export async function requestTokens() {
{
owner: repository.owner.login,
repo: repository.name,
path: `${config.gitTokensPath}/tokens.json`,
path: `packages/generator/tokens/tokens.json`,
ref,
}
);
Expand All @@ -91,8 +55,5 @@ export async function requestTokens() {
) as DesignTokens)
: (JSON.parse(response.data.content) as DesignTokens);
}
} catch (error) {
console.error('Error requesting tokens', error);
return null;
}
}
}),
});
33 changes: 2 additions & 31 deletions packages/api/src/router/integrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { eq } from '@ds-project/database';

import { createTRPCRouter, protectedProcedure } from '../trpc';
import { Resources } from '@ds-project/database/schema';
import { selectGithubIntegration } from '../queries/integrations';

export const integrationsRouter = createTRPCRouter({
byId: protectedProcedure
Expand All @@ -15,37 +16,7 @@ export const integrationsRouter = createTRPCRouter({
}),

github: protectedProcedure.query(async ({ ctx }) => {
const queryResult = await ctx.database.query.Integrations.findFirst({
columns: {
data: true,
},
where: (integrations) => eq(integrations.type, 'github'),
with: {
project: {
columns: {
id: true,
},
with: {
accountsToProjects: {
columns: {
projectId: true,
},
where: (accountsToProjects) =>
eq(accountsToProjects.accountId, ctx.account.id),
},
},
},
},
});

if (queryResult?.data?.type === 'github') {
return {
...queryResult,
data: queryResult.data,
};
}

return undefined;
return selectGithubIntegration({ ctx });
}),

figma: protectedProcedure.query(async ({ ctx }) => {
Expand Down
Loading

0 comments on commit 31bfcd1

Please sign in to comment.