11import { captureException , cron } from "@sentry/node" ;
2- import { CronJob } from "cron" ;
2+
33import { ContributionRepository } from "src/contribution/repository" ;
4+ import { ContributionRow } from "src/contribution/table" ;
45import { ContributorRepository } from "src/contributor/repository" ;
6+ import { ContributorRow } from "src/contributor/table" ;
7+ import { CronJob } from "cron" ;
58import { DataService } from "src/data/service" ;
69import { GithubService } from "src/github/service" ;
710import { LoggerService } from "src/logger/service" ;
811import { ProjectRepository } from "src/project/repository" ;
12+ import { ProjectRow } from "src/project/table" ;
913import { RepositoryRepository } from "src/repository/repository" ;
14+ import { SearchService } from "src/search/service" ;
1015import { Service } from "typedi" ;
1116
1217@Service ( )
@@ -22,6 +27,7 @@ export class DigestCron {
2227 private readonly repositoriesRepository : RepositoryRepository ,
2328 private readonly contributionsRepository : ContributionRepository ,
2429 private readonly contributorsRepository : ContributorRepository ,
30+ private readonly searchService : SearchService ,
2531 ) {
2632 const SentryCronJob = cron . instrumentCron ( CronJob , "DigestCron" ) ;
2733 new SentryCronJob (
@@ -67,11 +73,14 @@ export class DigestCron {
6773 const projectsFromDataFolder = await this . dataService . listProjects ( ) ;
6874
6975 for ( const project of projectsFromDataFolder ) {
70- const [ { id : projectId } ] = await this . projectsRepository . upsert ( {
71- ...project ,
76+ const projectEntity : ProjectRow = {
7277 runId,
73- id : project . slug ,
74- } ) ;
78+ id : project . slug . replace ( / [ . ] / g, "-" ) , // NOTE-OB: MeiliSearch doesn't allow dots in ids
79+ name : project . name ,
80+ } ;
81+ const [ { id : projectId } ] =
82+ await this . projectsRepository . upsert ( projectEntity ) ;
83+ await this . searchService . upsert ( "project" , projectEntity ) ;
7584
7685 let addedRepositoryCount = 0 ;
7786 try {
@@ -84,15 +93,16 @@ export class DigestCron {
8493 } ) ;
8594
8695 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 ,
94- id : `${ provider } -${ repoInfo . id } ` ,
95- } ) ;
96+ const [ { id : repositoryId } ] =
97+ await this . repositoriesRepository . upsert ( {
98+ provider,
99+ name : repoInfo . name ,
100+ owner : repoInfo . owner . login ,
101+ runId,
102+ projectId,
103+ stars : repoInfo . stargazers_count ,
104+ id : `${ provider } -${ repoInfo . id } ` ,
105+ } ) ;
96106 addedRepositoryCount ++ ;
97107
98108 const issues = await this . githubService . listRepositoryIssues ( {
@@ -101,18 +111,24 @@ export class DigestCron {
101111 } ) ;
102112
103113 for ( const issue of issues ) {
104- const githubUser = await this . githubService . getUser ( { username : issue . user . login } ) ;
114+ const githubUser = await this . githubService . getUser ( {
115+ username : issue . user . login ,
116+ } ) ;
105117
106118 if ( githubUser . type !== "User" ) continue ;
107119
108- const [ { id : contributorId } ] = await this . contributorsRepository . upsert ( {
120+ const contributorEntity : ContributorRow = {
109121 name : githubUser . name || githubUser . login ,
110122 username : githubUser . login ,
111123 url : githubUser . html_url ,
112124 avatarUrl : githubUser . avatar_url ,
113125 runId,
114126 id : `${ provider } -${ githubUser . login } ` ,
115- } ) ;
127+ } ;
128+
129+ const [ { id : contributorId } ] =
130+ await this . contributorsRepository . upsert ( contributorEntity ) ;
131+ await this . searchService . upsert ( "contributor" , contributorEntity ) ;
116132
117133 await this . contributorsRepository . upsertRelationWithRepository ( {
118134 contributorId,
@@ -122,23 +138,32 @@ export class DigestCron {
122138 } ) ;
123139
124140 const type = issue . pull_request ? "PULL_REQUEST" : "ISSUE" ;
125- await this . contributionsRepository . upsert ( {
141+ const contributionEntity : ContributionRow = {
126142 title : issue . title ,
127143 type,
128144 updatedAt : issue . updated_at ,
129145 activityCount : issue . comments ,
130146 runId,
131- url : type === "PULL_REQUEST" ? issue . pull_request . html_url : issue . html_url ,
147+ url :
148+ type === "PULL_REQUEST"
149+ ? issue . pull_request . html_url
150+ : issue . html_url ,
132151 repositoryId,
133152 contributorId,
134153 id : `${ provider } -${ issue . id } ` ,
135- } ) ;
154+ } ;
155+ await this . contributionsRepository . upsert ( contributionEntity ) ;
156+ await this . searchService . upsert (
157+ "contribution" ,
158+ contributionEntity ,
159+ ) ;
136160 }
137161
138- const repoContributors = await this . githubService . listRepositoryContributors ( {
139- owner : repository . owner ,
140- repository : repository . name ,
141- } ) ;
162+ const repoContributors =
163+ await this . githubService . listRepositoryContributors ( {
164+ owner : repository . owner ,
165+ repository : repository . name ,
166+ } ) ;
142167
143168 const repoContributorsFiltered = repoContributors . filter (
144169 ( contributor ) => contributor . type === "User" ,
@@ -148,14 +173,17 @@ export class DigestCron {
148173 const contributor = await this . githubService . getUser ( {
149174 username : repoContributor . login ,
150175 } ) ;
151- const [ { id : contributorId } ] = await this . contributorsRepository . upsert ( {
176+ const contributorEntity : ContributorRow = {
152177 name : contributor . name || contributor . login ,
153178 username : contributor . login ,
154179 url : contributor . html_url ,
155180 avatarUrl : contributor . avatar_url ,
156181 runId,
157182 id : `${ provider } -${ contributor . login } ` ,
158- } ) ;
183+ } ;
184+ const [ { id : contributorId } ] =
185+ await this . contributorsRepository . upsert ( contributorEntity ) ;
186+ await this . searchService . upsert ( "contributor" , contributorEntity ) ;
159187
160188 await this . contributorsRepository . upsertRelationWithRepository ( {
161189 contributorId,
@@ -179,11 +207,18 @@ export class DigestCron {
179207 }
180208
181209 try {
182- await this . contributorsRepository . deleteAllRelationWithRepositoryButWithRunId ( runId ) ;
210+ await this . contributorsRepository . deleteAllRelationWithRepositoryButWithRunId (
211+ runId ,
212+ ) ;
183213 await this . contributionsRepository . deleteAllButWithRunId ( runId ) ;
184214 await this . contributorsRepository . deleteAllButWithRunId ( runId ) ;
185215 await this . repositoriesRepository . deleteAllButWithRunId ( runId ) ;
186216 await this . projectsRepository . deleteAllButWithRunId ( runId ) ;
217+ await Promise . all ( [
218+ this . searchService . deleteAllButWithRunId ( "project" , runId ) ,
219+ this . searchService . deleteAllButWithRunId ( "contribution" , runId ) ,
220+ this . searchService . deleteAllButWithRunId ( "contributor" , runId ) ,
221+ ] ) ;
187222 } catch ( error ) {
188223 captureException ( error , { tags : { type : "CRON" } } ) ;
189224 }
0 commit comments