Skip to content

Commit dc8530d

Browse files
authored
Merge pull request #583 from dzcode-io/fix/filterout-contributions-created-by-bots
Fix: filtering out contributions created by bots
2 parents 4c4cea2 + 559cc0e commit dc8530d

File tree

6 files changed

+32
-4
lines changed

6 files changed

+32
-4
lines changed

api/src/contribution/controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class ContributionController {
2020
): Promise<GetContributionsResponseDto> {
2121
const { contributions, filters } = await this.contributionRepository.find(
2222
(contribution) =>
23+
!contribution.createdBy.username.includes("[bot]") &&
2324
(labels.length === 0 || labels.some((label) => contribution.labels.includes(label))) &&
2425
(languages.length === 0 ||
2526
languages.some((language) => contribution.languages.includes(language))) &&

api/src/contribution/repository.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class ContributionRepository {
2222

2323
let contributions = (
2424
await Promise.all(
25-
projects.reduce<Promise<Model<ContributionEntity, "project">[]>[]>(
25+
projects.reduce<Promise<Model<ContributionEntity, "project" | "createdBy">[]>[]>(
2626
(pV, { repositories, name, slug }) => [
2727
...pV,
2828
...repositories
@@ -38,8 +38,7 @@ export class ContributionRepository {
3838
owner,
3939
repository,
4040
});
41-
// @TODO-ZM: filter out the ones created by bots
42-
return issuesIncludingPRs.map<Model<ContributionEntity, "project">>(
41+
return issuesIncludingPRs.map<Model<ContributionEntity, "project" | "createdBy">>(
4342
({
4443
number,
4544
labels: gLabels,
@@ -49,6 +48,7 @@ export class ContributionRepository {
4948
created_at, // eslint-disable-line camelcase
5049
updated_at, // eslint-disable-line camelcase
5150
comments,
51+
user,
5252
}) => ({
5353
id: `${number}`,
5454
labels: gLabels.map(({ name }) => name),
@@ -64,6 +64,7 @@ export class ContributionRepository {
6464
updatedAt: updated_at, // eslint-disable-line camelcase
6565
commentsCount: comments,
6666
/* eslint-enable camelcase */
67+
createdBy: this.githubService.githubUserToAccountEntity(user),
6768
}),
6869
);
6970
} catch (error) {

api/src/contribution/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export class FilterDto {
3131
export class GetContributionsResponseDto extends GeneralResponseDto {
3232
@ValidateNested({ each: true })
3333
@Type(() => ContributionEntity)
34-
contributions!: Model<ContributionEntity, "project">[];
34+
contributions!: Model<ContributionEntity, "project" | "createdBy">[];
3535

3636
@ValidateNested({ each: true })
3737
@Type(() => FilterDto)

packages/models/src/contribution/__snapshots__/index.spec.ts.snap

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ exports[`should match snapshot when providing all fields: output 1`] = `
66
ContributionEntity {
77
"commentsCount": 0,
88
"createdAt": "2020-02-02T20:22:02.000Z",
9+
"createdBy": AccountEntity {
10+
"avatarUrl": "https://avatars.githubusercontent.com/u/20110076?v=4",
11+
"id": "github/20110076",
12+
"name": "Zakaria Mansouri",
13+
"profileUrl": "/Account/github/20110076",
14+
"username": "ZibanPirate",
15+
},
916
"id": "71",
1017
"labels": [
1118
"discussion",
@@ -32,6 +39,13 @@ exports[`should match snapshot when providing required fields only: output 1`] =
3239
ContributionEntity {
3340
"commentsCount": 0,
3441
"createdAt": "2020-02-02T20:22:02.000Z",
42+
"createdBy": AccountEntity {
43+
"avatarUrl": "https://avatars.githubusercontent.com/u/20110076?v=4",
44+
"id": "github/20110076",
45+
"name": "Zakaria Mansouri",
46+
"profileUrl": "/Account/github/20110076",
47+
"username": "ZibanPirate",
48+
},
3549
"id": "71",
3650
"labels": [
3751
"discussion",

packages/models/src/contribution/index.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ runDTOTestCases(
1818
createdAt: "2020-02-02T20:22:02.000Z",
1919
updatedAt: "2020-02-02T20:22:02.000Z",
2020
url: "https://github.com/dzcode-io/leblad/issues/71",
21+
createdBy: {
22+
id: "github/20110076",
23+
username: "ZibanPirate",
24+
name: "Zakaria Mansouri",
25+
profileUrl: "/Account/github/20110076",
26+
avatarUrl: "https://avatars.githubusercontent.com/u/20110076?v=4",
27+
},
2128
},
2229
{},
2330
);

packages/models/src/contribution/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Type } from "class-transformer";
22
import { IsDateString, IsNumber, IsString, IsUrl, ValidateNested } from "class-validator";
33
import { BaseEntity, Model } from "src/_base";
4+
import { AccountEntity } from "src/account";
45
import { ProjectReferenceEntity } from "src/project-reference";
56

67
export class ContributionEntity extends BaseEntity {
@@ -14,6 +15,10 @@ export class ContributionEntity extends BaseEntity {
1415
@Type(() => ProjectReferenceEntity)
1516
project!: Model<ProjectReferenceEntity>;
1617

18+
@ValidateNested()
19+
@Type(() => AccountEntity)
20+
createdBy!: Model<AccountEntity>;
21+
1722
@IsString()
1823
type!: "issue" | "pullRequest";
1924

0 commit comments

Comments
 (0)