Skip to content

Commit 0d14b8c

Browse files
committed
feat: enhance DigestCron to integrate search service for indexing and deletion of project, contribution, and contributor items
1 parent 9a4f3d4 commit 0d14b8c

File tree

1 file changed

+74
-29
lines changed

1 file changed

+74
-29
lines changed

api/src/digest/cron.ts

Lines changed: 74 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { captureException, cron } from "@sentry/node";
2-
import { CronJob } from "cron";
2+
33
import { ContributionRepository } from "src/contribution/repository";
44
import { ContributorRepository } from "src/contributor/repository";
5+
import { CronJob } from "cron";
56
import { DataService } from "src/data/service";
67
import { GithubService } from "src/github/service";
78
import { LoggerService } from "src/logger/service";
89
import { ProjectRepository } from "src/project/repository";
910
import { RepositoryRepository } from "src/repository/repository";
11+
import { SearchItem } from "src/search/types";
12+
import { SearchService } from "src/search/service";
1013
import { Service } from "typedi";
1114

1215
@Service()
@@ -22,6 +25,7 @@ export class DigestCron {
2225
private readonly repositoriesRepository: RepositoryRepository,
2326
private readonly contributionsRepository: ContributionRepository,
2427
private readonly contributorsRepository: ContributorRepository,
28+
private readonly searchService: SearchService,
2529
) {
2630
const SentryCronJob = cron.instrumentCron(CronJob, "DigestCron");
2731
new SentryCronJob(
@@ -66,6 +70,10 @@ export class DigestCron {
6670

6771
const projectsFromDataFolder = await this.dataService.listProjects();
6872

73+
const searchProjectItems: SearchItem[] = [];
74+
const searchContributorItems: SearchItem[] = [];
75+
const searchContributionItems: SearchItem[] = [];
76+
6977
for (const project of projectsFromDataFolder) {
7078
const [{ id: projectId }] = await this.projectsRepository.upsert({
7179
...project,
@@ -84,14 +92,20 @@ export class DigestCron {
8492
});
8593

8694
const provider = "github";
87-
const [{ id: repositoryId }] = await this.repositoriesRepository.upsert({
88-
provider,
89-
name: repoInfo.name,
90-
owner: repoInfo.owner.login,
91-
runId,
92-
projectId,
93-
stars: repoInfo.stargazers_count,
95+
const [{ id: repositoryId }] =
96+
await this.repositoriesRepository.upsert({
97+
provider,
98+
name: repoInfo.name,
99+
owner: repoInfo.owner.login,
100+
runId,
101+
projectId,
102+
stars: repoInfo.stargazers_count,
103+
id: `${provider}-${repoInfo.id}`,
104+
});
105+
searchProjectItems.push({
94106
id: `${provider}-${repoInfo.id}`,
107+
title: repoInfo.name,
108+
type: "project",
95109
});
96110
addedRepositoryCount++;
97111

@@ -101,17 +115,25 @@ export class DigestCron {
101115
});
102116

103117
for (const issue of issues) {
104-
const githubUser = await this.githubService.getUser({ username: issue.user.login });
118+
const githubUser = await this.githubService.getUser({
119+
username: issue.user.login,
120+
});
105121

106122
if (githubUser.type !== "User") continue;
107123

108-
const [{ id: contributorId }] = await this.contributorsRepository.upsert({
109-
name: githubUser.name || githubUser.login,
110-
username: githubUser.login,
111-
url: githubUser.html_url,
112-
avatarUrl: githubUser.avatar_url,
113-
runId,
124+
const [{ id: contributorId }] =
125+
await this.contributorsRepository.upsert({
126+
name: githubUser.name || githubUser.login,
127+
username: githubUser.login,
128+
url: githubUser.html_url,
129+
avatarUrl: githubUser.avatar_url,
130+
runId,
131+
id: `${provider}-${githubUser.login}`,
132+
});
133+
searchContributorItems.push({
114134
id: `${provider}-${githubUser.login}`,
135+
title: githubUser.name || githubUser.login,
136+
type: "contributor",
115137
});
116138

117139
await this.contributorsRepository.upsertRelationWithRepository({
@@ -128,17 +150,26 @@ export class DigestCron {
128150
updatedAt: issue.updated_at,
129151
activityCount: issue.comments,
130152
runId,
131-
url: type === "PULL_REQUEST" ? issue.pull_request.html_url : issue.html_url,
153+
url:
154+
type === "PULL_REQUEST"
155+
? issue.pull_request.html_url
156+
: issue.html_url,
132157
repositoryId,
133158
contributorId,
134159
id: `${provider}-${issue.id}`,
135160
});
161+
searchContributionItems.push({
162+
id: `${provider}-${issue.id}`,
163+
title: issue.title,
164+
type: "contribution",
165+
});
136166
}
137167

138-
const repoContributors = await this.githubService.listRepositoryContributors({
139-
owner: repository.owner,
140-
repository: repository.name,
141-
});
168+
const repoContributors =
169+
await this.githubService.listRepositoryContributors({
170+
owner: repository.owner,
171+
repository: repository.name,
172+
});
142173

143174
const repoContributorsFiltered = repoContributors.filter(
144175
(contributor) => contributor.type === "User",
@@ -148,14 +179,15 @@ export class DigestCron {
148179
const contributor = await this.githubService.getUser({
149180
username: repoContributor.login,
150181
});
151-
const [{ id: contributorId }] = await this.contributorsRepository.upsert({
152-
name: contributor.name || contributor.login,
153-
username: contributor.login,
154-
url: contributor.html_url,
155-
avatarUrl: contributor.avatar_url,
156-
runId,
157-
id: `${provider}-${contributor.login}`,
158-
});
182+
const [{ id: contributorId }] =
183+
await this.contributorsRepository.upsert({
184+
name: contributor.name || contributor.login,
185+
username: contributor.login,
186+
url: contributor.html_url,
187+
avatarUrl: contributor.avatar_url,
188+
runId,
189+
id: `${provider}-${contributor.login}`,
190+
});
159191

160192
await this.contributorsRepository.upsertRelationWithRepository({
161193
contributorId,
@@ -179,11 +211,24 @@ export class DigestCron {
179211
}
180212

181213
try {
182-
await this.contributorsRepository.deleteAllRelationWithRepositoryButWithRunId(runId);
214+
await this.contributorsRepository.deleteAllRelationWithRepositoryButWithRunId(
215+
runId,
216+
);
183217
await this.contributionsRepository.deleteAllButWithRunId(runId);
184218
await this.contributorsRepository.deleteAllButWithRunId(runId);
185219
await this.repositoriesRepository.deleteAllButWithRunId(runId);
186220
await this.projectsRepository.deleteAllButWithRunId(runId);
221+
await this.searchService.deleteAllDocuments("project");
222+
await this.searchService.deleteAllDocuments("contribution");
223+
await this.searchService.deleteAllDocuments("contributor");
224+
} catch (error) {
225+
captureException(error, { tags: { type: "CRON" } });
226+
}
227+
228+
try {
229+
await this.searchService.index("project", searchProjectItems);
230+
await this.searchService.index("contribution", searchContributionItems);
231+
await this.searchService.index("contributor", searchContributorItems);
187232
} catch (error) {
188233
captureException(error, { tags: { type: "CRON" } });
189234
}

0 commit comments

Comments
 (0)