Skip to content

Commit

Permalink
Make Github stars dynamic and improve database init (#5000)
Browse files Browse the repository at this point in the history
I extracted the init database logic into its own file. 
You can now run it with yarn database:init.
Added database entry for GitHub stars. 

Do you want me to remove the init route or is it used for something else
?

---------

Co-authored-by: Ady Beraud <a.beraud96@gmail.com>
Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
  • Loading branch information
3 people committed Apr 24, 2024
1 parent fda0c3c commit 0a7f823
Show file tree
Hide file tree
Showing 27 changed files with 237 additions and 118 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@
"ts-loader": "^9.2.3",
"ts-node": "10.9.1",
"tsconfig-paths": "^4.2.0",
"tsx": "^4.7.2",
"typescript": "5.3.3",
"vite": "^5.0.0",
"vite-plugin-checker": "^0.6.2",
Expand Down
2 changes: 2 additions & 0 deletions packages/twenty-website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"build": "npx next build",
"start": "npx next start",
"lint": "npx next lint",
"github:sync": "npx tsx src/github-sync/github-sync.ts",
"database:migrate": "npx tsx src/database/migrate-database.ts",
"database:generate:pg": "npx drizzle-kit generate:pg --config=src/database/postgres/drizzle-posgres.config.ts",
"database:generate:sqlite": "npx drizzle-kit generate:sqlite --config=src/database/sqlite/drizzle-sqlite.config.ts"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@ import {
LogoContainer,
} from '@/app/_components/ui/layout/header/styled';
import { Logo } from '@/app/_components/ui/layout/Logo';
import { formatNumberOfStars } from '@/shared-utils/formatNumberOfStars';

export const HeaderDesktop = () => {
type Props = {
numberOfStars: number;
};

export const HeaderDesktop = ({ numberOfStars }: Props) => {
return (
<DesktopNav>
<LogoContainer>
Expand All @@ -26,7 +31,9 @@ export const HeaderDesktop = () => {
Docs <ExternalArrow />
</ListItem>
<ListItem href="https://github.com/twentyhq/twenty">
<GithubIcon color="rgb(71,71,71)" /> 8.3k <ExternalArrow />
<GithubIcon color="rgb(71,71,71)" />
{formatNumberOfStars(numberOfStars)}
<ExternalArrow />
</ListItem>
</LinkList>
<CallToAction />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ import {
NavOpen,
} from '@/app/_components/ui/layout/header/styled';
import { Logo } from '@/app/_components/ui/layout/Logo';
import { formatNumberOfStars } from '@/shared-utils/formatNumberOfStars';

const IBMPlexMono = IBM_Plex_Mono({
weight: '500',
subsets: ['latin'],
display: 'swap',
});

export const HeaderMobile = () => {
type Props = {
numberOfStars: number;
};

export const HeaderMobile = ({ numberOfStars }: Props) => {
const isTwentyDev = false;

const [menuOpen, setMenuOpen] = useState(false);
Expand Down Expand Up @@ -64,7 +69,8 @@ export const HeaderMobile = () => {
Docs <ExternalArrow />
</ListItem>
<ListItem href="https://github.com/twentyhq/twenty">
<GithubIcon color="rgb(71,71,71)" /> 8.3k <ExternalArrow />
<GithubIcon color="rgb(71,71,71)" />{' '}
{formatNumberOfStars(numberOfStars)} <ExternalArrow />
</ListItem>
</MobileLinkList>
<CallToAction />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
'use client';
import { desc } from 'drizzle-orm';

import { HeaderDesktop } from '@/app/_components/ui/layout/header/HeaderDesktop';
import { HeaderMobile } from '@/app/_components/ui/layout/header/HeaderMobile';
import { findOne } from '@/database/database';
import { githubStarsModel } from '@/database/model';

export const AppHeader = async () => {
const githubStars = await findOne(
githubStarsModel,
desc(githubStarsModel.timestamp),
);

export const AppHeader = () => {
return (
<>
<HeaderDesktop />
<HeaderMobile />
<HeaderDesktop numberOfStars={githubStars?.[0]?.numberOfStars} />
<HeaderMobile numberOfStars={githubStars?.[0]?.numberOfStars} />
</>
);
};
26 changes: 26 additions & 0 deletions packages/twenty-website/src/app/api/github-stars/route.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { desc } from 'drizzle-orm';

import { findOne } from '@/database/database';
import { githubStarsModel } from '@/database/model';
import { formatNumberOfStars } from '@/shared-utils/formatNumberOfStars';

export const dynamic = 'force-dynamic';

export async function GET() {
try {
const githubStars = await findOne(
githubStarsModel,
desc(githubStarsModel.timestamp),
);

const formattedGithubNumberOfStars = formatNumberOfStars(
githubStars[0].numberOfStars,
);

return Response.json(formattedGithubNumberOfStars);
} catch (error: any) {
return new Response(`Github stars error: ${error?.message}`, {
status: 500,
});
}
}
46 changes: 0 additions & 46 deletions packages/twenty-website/src/app/contributors/api/init/route.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { graphql } from '@octokit/graphql';
import { desc } from 'drizzle-orm';

import { fetchAssignableUsers } from '@/app/contributors/api/fetch-assignable-users';
import { saveIssuesToDB } from '@/app/contributors/api/save-issues-to-db';
import { savePRsToDB } from '@/app/contributors/api/save-prs-to-db';
import { searchIssuesPRs } from '@/app/contributors/api/search-issues-prs';
import { IssueNode, PullRequestNode } from '@/app/contributors/api/types';
import { findOne } from '@/database/database';
import { issueModel, pullRequestModel } from '@/database/model';
import { fetchAssignableUsers } from '@/github-sync/contributors/fetch-assignable-users';
import { saveIssuesToDB } from '@/github-sync/contributors/save-issues-to-db';
import { savePRsToDB } from '@/github-sync/contributors/save-prs-to-db';
import { searchIssuesPRs } from '@/github-sync/contributors/search-issues-prs';
import { IssueNode, PullRequestNode } from '@/github-sync/contributors/types';

export async function GET() {
if (!global.process.env.GITHUB_TOKEN) {
Expand Down
8 changes: 8 additions & 0 deletions packages/twenty-website/src/database/migrate-database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { migrate } from '@/database/database';

export const migrateDatabase = async () => {
await migrate();
process.exit(0);
};

migrateDatabase();
29 changes: 16 additions & 13 deletions packages/twenty-website/src/database/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
pgGithubStars,
pgIssueLabels,
pgIssues,
pgLabels,
Expand Down Expand Up @@ -31,17 +32,19 @@ export const issueLabelModel = isSqliteDriver
? sqlLiteIssueLabels
: pgIssueLabels;

export type User = typeof sqlLiteUsers.$inferSelect;
export type PullRequest = typeof sqlLitePullRequests.$inferSelect;
export type Issue = typeof sqlLiteIssues.$inferSelect;
export type Label = typeof sqlLiteLabels.$inferSelect;
export type PullRequestLabel = typeof sqlLitePullRequestLabels.$inferSelect;
export type IssueLabel = typeof sqlLiteIssueLabels.$inferSelect;
export const githubStarsModel = pgGithubStars;

export type UserInsert = typeof sqlLiteUsers.$inferInsert;
export type PullRequestInsert = typeof sqlLitePullRequests.$inferInsert;
export type IssueInsert = typeof sqlLiteIssues.$inferInsert;
export type LabelInsert = typeof sqlLiteLabels.$inferInsert;
export type PullRequestLabelInsert =
typeof sqlLitePullRequestLabels.$inferInsert;
export type IssueLabelInsert = typeof sqlLiteIssueLabels.$inferInsert;
export type User = typeof pgUsers.$inferSelect;
export type PullRequest = typeof pgPullRequests.$inferSelect;
export type Issue = typeof pgIssues.$inferSelect;
export type Label = typeof pgLabels.$inferSelect;
export type PullRequestLabel = typeof pgPullRequestLabels.$inferSelect;
export type IssueLabel = typeof pgIssueLabels.$inferSelect;

export type UserInsert = typeof pgUsers.$inferInsert;
export type PullRequestInsert = typeof pgPullRequests.$inferInsert;
export type IssueInsert = typeof pgIssues.$inferInsert;
export type LabelInsert = typeof pgLabels.$inferInsert;
export type PullRequestLabelInsert = typeof pgPullRequestLabels.$inferInsert;
export type IssueLabelInsert = typeof pgIssueLabels.$inferInsert;
export type GithubStars = typeof pgGithubStars.$inferInsert;
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ DO $$ BEGIN
ALTER TABLE "pullRequests" ADD CONSTRAINT "pullRequests_authorId_users_id_fk" FOREIGN KEY ("authorId") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
END $$;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS "githubStars" (
"timestamp" timestamp DEFAULT now() NOT NULL,
"numberOfStars" integer
);
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,17 @@
"name": "issueLabels_issueId_issues_id_fk",
"tableFrom": "issueLabels",
"tableTo": "issues",
"columnsFrom": [
"issueId"
],
"columnsTo": [
"id"
],
"columnsFrom": ["issueId"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
},
"issueLabels_labelId_labels_id_fk": {
"name": "issueLabels_labelId_labels_id_fk",
"tableFrom": "issueLabels",
"tableTo": "labels",
"columnsFrom": [
"labelId"
],
"columnsTo": [
"id"
],
"columnsFrom": ["labelId"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
Expand Down Expand Up @@ -118,12 +110,8 @@
"name": "issues_authorId_users_id_fk",
"tableFrom": "issues",
"tableTo": "users",
"columnsFrom": [
"authorId"
],
"columnsTo": [
"id"
],
"columnsFrom": ["authorId"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
Expand Down Expand Up @@ -194,25 +182,17 @@
"name": "pullRequestLabels_pullRequestExternalId_pullRequests_id_fk",
"tableFrom": "pullRequestLabels",
"tableTo": "pullRequests",
"columnsFrom": [
"pullRequestExternalId"
],
"columnsTo": [
"id"
],
"columnsFrom": ["pullRequestExternalId"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
},
"pullRequestLabels_labelId_labels_id_fk": {
"name": "pullRequestLabels_labelId_labels_id_fk",
"tableFrom": "pullRequestLabels",
"tableTo": "labels",
"columnsFrom": [
"labelId"
],
"columnsTo": [
"id"
],
"columnsFrom": ["labelId"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
Expand Down Expand Up @@ -285,12 +265,8 @@
"name": "pullRequests_authorId_users_id_fk",
"tableFrom": "pullRequests",
"tableTo": "users",
"columnsFrom": [
"authorId"
],
"columnsTo": [
"id"
],
"columnsFrom": ["authorId"],
"columnsTo": ["id"],
"onDelete": "no action",
"onUpdate": "no action"
}
Expand Down Expand Up @@ -340,4 +316,4 @@
"schemas": {},
"tables": {}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
"when": 1707921820164,
"tag": "0000_absent_giant_man",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1713792223113,
"tag": "0001_marvelous_eddie_brock",
"breakpoints": true
}
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pgTable, text } from 'drizzle-orm/pg-core';
import { integer, pgTable, text, timestamp } from 'drizzle-orm/pg-core';

export const pgUsers = pgTable('users', {
id: text('id').primaryKey(),
Expand Down Expand Up @@ -50,3 +50,8 @@ export const pgIssueLabels = pgTable('issueLabels', {
issueId: text('issueId').references(() => pgIssues.id),
labelId: text('labelId').references(() => pgLabels.id),
});

export const pgGithubStars = pgTable('githubStars', {
timestamp: timestamp('timestamp').notNull().defaultNow(),
numberOfStars: integer('numberOfStars'),
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@
"breakpoints": true
}
]
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { graphql } from '@octokit/graphql';

import { Repository } from '@/app/contributors/api/types';
import { Repository } from '@/github-sync/contributors/types';

export async function fetchAssignableUsers(
query: typeof graphql,
Expand Down
Loading

0 comments on commit 0a7f823

Please sign in to comment.