Skip to content

Commit 5df5b34

Browse files
authored
Merge pull request #581 from dzcode-io/fixed-bug-where-projects-endpoint-stuck
Fixed bug where projects endpoint stuck
2 parents 0b2ce54 + b5c2f60 commit 5df5b34

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

api/src/fetch/service.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { lock } from "@dzcode.io/utils/dist/concurrency";
1+
import { lockFactory } from "@dzcode.io/utils/dist/concurrency";
22
import { defaults } from "make-fetch-happen";
33
import { ConfigService } from "src/config/service";
44
import { Service } from "typedi";
@@ -28,9 +28,11 @@ export class FetchService {
2828
};
2929

3030
private makeFetchHappenInstance;
31-
private fetch = lock(async <T>(url: string, { headers }: Omit<FetchConfig, "params"> = {}) => {
32-
const response = await this.makeFetchHappenInstance(url, { headers });
33-
const jsonResponse = (await response.json()) as T;
34-
return jsonResponse;
35-
});
31+
private fetch = lockFactory(
32+
async <T>(url: string, { headers }: Omit<FetchConfig, "params"> = {}) => {
33+
const response = await this.makeFetchHappenInstance(url, { headers });
34+
const jsonResponse = (await response.json()) as T;
35+
return jsonResponse;
36+
},
37+
);
3638
}

api/src/project/controller.ts

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { AccountEntity } from "@dzcode.io/models/dist/account";
12
import { Controller, Get } from "routing-controllers";
23
import { OpenAPI, ResponseSchema } from "routing-controllers-openapi";
34
import { DataService } from "src/data/service";
@@ -34,35 +35,34 @@ export class ProjectController {
3435
repositories: await Promise.all(
3536
repositories.map(async ({ provider, owner, repository }) => {
3637
let contributionCount = 0;
38+
let contributors: AccountEntity[] = [];
39+
let languages: string[] = [];
3740
try {
38-
contributionCount = (
39-
await this.githubService.listRepositoryIssues({ owner, repository })
40-
).length;
41+
[contributionCount, contributors, languages] = await Promise.all([
42+
(await this.githubService.listRepositoryIssues({ owner, repository })).length,
43+
await Promise.all(
44+
(
45+
await this.githubService.listRepositoryContributors({ owner, repository })
46+
).map(async ({ login }) => {
47+
const githubUser = await this.githubService.getUser({ username: login });
48+
return this.githubService.githubUserToAccountEntity(githubUser);
49+
}),
50+
),
51+
await this.githubService.listRepositoryLanguages({ owner, repository }),
52+
]);
4153
} catch (error) {
42-
this.loggerService.warn({
43-
message: `Failed to fetch contributionCount for ${owner}/${repository}: ${error}`,
44-
meta: { owner, repository },
54+
this.loggerService.error({
55+
message: `Failed to fetch rich info for project: ${owner}/${repository}`,
56+
meta: { owner, repository, error },
4557
});
4658
}
59+
4760
return {
4861
provider,
4962
owner,
5063
repository,
51-
stats: {
52-
contributionCount,
53-
languages: await this.githubService.listRepositoryLanguages({
54-
owner,
55-
repository,
56-
}),
57-
},
58-
contributors: await Promise.all(
59-
(
60-
await this.githubService.listRepositoryContributors({ owner, repository })
61-
).map(async ({ login }) => {
62-
const githubUser = await this.githubService.getUser({ username: login });
63-
return this.githubService.githubUserToAccountEntity(githubUser);
64-
}),
65-
),
64+
stats: { contributionCount, languages },
65+
contributors,
6666
};
6767
}),
6868
),

packages/utils/src/concurrency/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface QueueObject {
66
}
77

88
// eslint-disable-next-line @typescript-eslint/no-explicit-any
9-
export const lock = <T extends (...args: any) => Promise<any>>(func: T): T => {
9+
export const lockFactory = <T extends (...args: any) => Promise<any>>(func: T): T => {
1010
const queue: Record<string, QueueObject> = {};
1111
let timer: NodeJS.Timer | null = null;
1212

@@ -63,7 +63,6 @@ export const lock = <T extends (...args: any) => Promise<any>>(func: T): T => {
6363
callbacks[index](undefined, error);
6464
}
6565
}
66-
delete queue[queueKeys[0]];
6766
};
6867

6968
return lockedFunction as T;

0 commit comments

Comments
 (0)