Create congratulations bot (#5404)
- Created congratulations bot : <img width="939" alt="Screenshot 2024-05-14 at 12 47 13" src="https://github.com/twentyhq/twenty/assets/102751374/5138515f-fe4d-4c6d-9c7a-0240accbfca9"> - Modified OG image - Added png extension to OG image route To be noted: The bot will not work until the new API route is not deployed. Please check OG image with Cloudflare cache. --------- Co-authored-by: Ady Beraud <a.beraud96@gmail.com>
This commit is contained in:
@ -6,22 +6,17 @@ import {
|
||||
Repository,
|
||||
} from '@/github/contributors/types';
|
||||
|
||||
// TODO: We should implement a true partial sync instead of using pageLimit.
|
||||
// Check search-issues-prs.tsx and modify "updated:>2024-02-27" to make it dynamic
|
||||
|
||||
export async function fetchIssuesPRs(
|
||||
query: typeof graphql,
|
||||
cursor: string | null = null,
|
||||
isIssues = false,
|
||||
accumulatedData: Array<PullRequestNode | IssueNode> = [],
|
||||
pageLimit: number,
|
||||
currentPage = 0,
|
||||
): Promise<Array<PullRequestNode | IssueNode>> {
|
||||
const { repository } = await query<Repository>(
|
||||
`
|
||||
query ($cursor: String) {
|
||||
repository(owner: "twentyhq", name: "twenty") {
|
||||
pullRequests(first: 30, after: $cursor, orderBy: {field: CREATED_AT, direction: DESC}) @skip(if: ${isIssues}) {
|
||||
pullRequests(first: 100, after: $cursor, orderBy: {field: CREATED_AT, direction: DESC}) @skip(if: ${isIssues}) {
|
||||
nodes {
|
||||
id
|
||||
title
|
||||
@ -94,16 +89,12 @@ export async function fetchIssuesPRs(
|
||||
? repository.issues.pageInfo
|
||||
: repository.pullRequests.pageInfo;
|
||||
|
||||
const newCurrentPage = currentPage + 1;
|
||||
|
||||
if ((!pageLimit || newCurrentPage < pageLimit) && pageInfo.hasNextPage) {
|
||||
if (pageInfo.hasNextPage) {
|
||||
return fetchIssuesPRs(
|
||||
query,
|
||||
pageInfo.endCursor,
|
||||
isIssues,
|
||||
newAccumulatedData,
|
||||
pageLimit,
|
||||
currentPage + 1,
|
||||
);
|
||||
} else {
|
||||
return newAccumulatedData;
|
||||
|
||||
@ -0,0 +1,15 @@
|
||||
import { desc } from 'drizzle-orm';
|
||||
|
||||
import { findOne } from '@/database/database';
|
||||
import { issueModel, pullRequestModel } from '@/database/model';
|
||||
|
||||
export async function getLatestUpdate() {
|
||||
const latestPR = await findOne(
|
||||
pullRequestModel,
|
||||
desc(pullRequestModel.updatedAt),
|
||||
);
|
||||
const latestIssue = await findOne(issueModel, desc(issueModel.updatedAt));
|
||||
const prDate = new Date(latestPR[0].updatedAt);
|
||||
const issueDate = new Date(latestIssue[0].updatedAt);
|
||||
return (prDate > issueDate ? prDate : issueDate).toISOString();
|
||||
}
|
||||
@ -42,7 +42,10 @@ export async function saveIssuesToDB(
|
||||
authorId: issue.author.login,
|
||||
},
|
||||
],
|
||||
{ onConflictKey: 'id' },
|
||||
{
|
||||
onConflictKey: 'id',
|
||||
onConflictUpdateObject: { updatedAt: issue.updatedAt },
|
||||
},
|
||||
);
|
||||
|
||||
for (const label of issue.labels.nodes) {
|
||||
|
||||
@ -44,7 +44,10 @@ export async function savePRsToDB(
|
||||
authorId: pr.author.login,
|
||||
},
|
||||
],
|
||||
{ onConflictKey: 'id', onConflictUpdateObject: { title: pr.title } },
|
||||
{
|
||||
onConflictKey: 'id',
|
||||
onConflictUpdateObject: { title: pr.title, updatedAt: pr.updatedAt },
|
||||
},
|
||||
);
|
||||
|
||||
for (const label of pr.labels.nodes) {
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { graphql } from '@octokit/graphql';
|
||||
|
||||
import { getLatestUpdate } from '@/github/contributors/get-latest-update';
|
||||
import {
|
||||
IssueNode,
|
||||
PullRequestNode,
|
||||
@ -12,12 +13,13 @@ export async function searchIssuesPRs(
|
||||
isIssues = false,
|
||||
accumulatedData: Array<PullRequestNode | IssueNode> = [],
|
||||
): Promise<Array<PullRequestNode | IssueNode>> {
|
||||
const since = await getLatestUpdate();
|
||||
const { search } = await query<SearchIssuesPRsQuery>(
|
||||
`
|
||||
query searchPullRequestsAndIssues($cursor: String) {
|
||||
search(query: "repo:twentyhq/twenty ${
|
||||
isIssues ? 'is:issue' : 'is:pr'
|
||||
} updated:>2024-02-27", type: ISSUE, first: 100, after: $cursor) {
|
||||
} updated:>${since}", type: ISSUE, first: 100, after: $cursor) {
|
||||
edges {
|
||||
node {
|
||||
... on PullRequest {
|
||||
@ -80,6 +82,7 @@ export async function searchIssuesPRs(
|
||||
cursor,
|
||||
},
|
||||
);
|
||||
|
||||
const newAccumulatedData: Array<PullRequestNode | IssueNode> = [
|
||||
...accumulatedData,
|
||||
...search.edges.map(({ node }) => node),
|
||||
|
||||
46
packages/twenty-website/src/github/execute-partial-sync.ts
Normal file
46
packages/twenty-website/src/github/execute-partial-sync.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import { graphql } from '@octokit/graphql';
|
||||
|
||||
import { fetchAssignableUsers } from '@/github/contributors/fetch-assignable-users';
|
||||
import { saveIssuesToDB } from '@/github/contributors/save-issues-to-db';
|
||||
import { savePRsToDB } from '@/github/contributors/save-prs-to-db';
|
||||
import { searchIssuesPRs } from '@/github/contributors/search-issues-prs';
|
||||
import { IssueNode, PullRequestNode } from '@/github/contributors/types';
|
||||
import { fetchAndSaveGithubReleases } from '@/github/github-releases/fetch-and-save-github-releases';
|
||||
import { fetchAndSaveGithubStars } from '@/github/github-stars/fetch-and-save-github-stars';
|
||||
|
||||
export const executePartialSync = async () => {
|
||||
if (!global.process.env.GITHUB_TOKEN) {
|
||||
return new Error('No GitHub token provided');
|
||||
}
|
||||
|
||||
console.log('Synching data..');
|
||||
|
||||
const query = graphql.defaults({
|
||||
headers: {
|
||||
Authorization: 'bearer ' + global.process.env.GITHUB_TOKEN,
|
||||
},
|
||||
});
|
||||
|
||||
await fetchAndSaveGithubStars(query);
|
||||
await fetchAndSaveGithubReleases(query);
|
||||
|
||||
const assignableUsers = await fetchAssignableUsers(query);
|
||||
|
||||
const fetchedPRs = (await searchIssuesPRs(
|
||||
query,
|
||||
null,
|
||||
false,
|
||||
[],
|
||||
)) as Array<PullRequestNode>;
|
||||
const fetchedIssues = (await searchIssuesPRs(
|
||||
query,
|
||||
null,
|
||||
true,
|
||||
[],
|
||||
)) as Array<IssueNode>;
|
||||
|
||||
await savePRsToDB(fetchedPRs, assignableUsers);
|
||||
await saveIssuesToDB(fetchedIssues, assignableUsers);
|
||||
|
||||
console.log('data synched!');
|
||||
};
|
||||
@ -9,11 +9,7 @@ import { IssueNode, PullRequestNode } from '@/github/contributors/types';
|
||||
import { fetchAndSaveGithubReleases } from '@/github/github-releases/fetch-and-save-github-releases';
|
||||
import { fetchAndSaveGithubStars } from '@/github/github-stars/fetch-and-save-github-stars';
|
||||
|
||||
export const fetchAndSaveGithubData = async ({
|
||||
pageLimit,
|
||||
}: {
|
||||
pageLimit: number;
|
||||
}) => {
|
||||
export const fetchAndSaveGithubData = async () => {
|
||||
if (!global.process.env.GITHUB_TOKEN) {
|
||||
return new Error('No GitHub token provided');
|
||||
}
|
||||
@ -35,14 +31,12 @@ export const fetchAndSaveGithubData = async ({
|
||||
null,
|
||||
false,
|
||||
[],
|
||||
pageLimit,
|
||||
)) as Array<PullRequestNode>;
|
||||
const fetchedIssues = (await fetchIssuesPRs(
|
||||
query,
|
||||
null,
|
||||
true,
|
||||
[],
|
||||
pageLimit,
|
||||
)) as Array<IssueNode>;
|
||||
|
||||
await savePRsToDB(fetchedPRs, assignableUsers);
|
||||
|
||||
@ -1,14 +1,16 @@
|
||||
import { executePartialSync } from '@/github/execute-partial-sync';
|
||||
import { fetchAndSaveGithubData } from '@/github/fetch-and-save-github-data';
|
||||
|
||||
export const githubSync = async () => {
|
||||
const pageLimitFlagIndex = process.argv.indexOf('--pageLimit');
|
||||
let pageLimit = 0;
|
||||
const isFullSyncFlagIndex = process.argv.indexOf('--isFullSync');
|
||||
const isFullSync = isFullSyncFlagIndex > -1;
|
||||
|
||||
if (pageLimitFlagIndex > -1) {
|
||||
pageLimit = parseInt(process.argv[pageLimitFlagIndex + 1], 10);
|
||||
if (isFullSync) {
|
||||
await fetchAndSaveGithubData();
|
||||
} else {
|
||||
await executePartialSync();
|
||||
}
|
||||
|
||||
await fetchAndSaveGithubData({ pageLimit });
|
||||
process.exit(0);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user