Skip to content

Commit

Permalink
Merge pull request #583 from dzcode-io/fix/filterout-contributions-cr…
Browse files Browse the repository at this point in the history
…eated-by-bots

Fix: filtering out contributions created by bots
  • Loading branch information
ZibanPirate authored Jan 1, 2024
2 parents 4c4cea2 + 559cc0e commit dc8530d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/src/contribution/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class ContributionController {
): Promise<GetContributionsResponseDto> {
const { contributions, filters } = await this.contributionRepository.find(
(contribution) =>
!contribution.createdBy.username.includes("[bot]") &&
(labels.length === 0 || labels.some((label) => contribution.labels.includes(label))) &&
(languages.length === 0 ||
languages.some((language) => contribution.languages.includes(language))) &&
Expand Down
7 changes: 4 additions & 3 deletions api/src/contribution/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class ContributionRepository {

let contributions = (
await Promise.all(
projects.reduce<Promise<Model<ContributionEntity, "project">[]>[]>(
projects.reduce<Promise<Model<ContributionEntity, "project" | "createdBy">[]>[]>(
(pV, { repositories, name, slug }) => [
...pV,
...repositories
Expand All @@ -38,8 +38,7 @@ export class ContributionRepository {
owner,
repository,
});
// @TODO-ZM: filter out the ones created by bots
return issuesIncludingPRs.map<Model<ContributionEntity, "project">>(
return issuesIncludingPRs.map<Model<ContributionEntity, "project" | "createdBy">>(
({
number,
labels: gLabels,
Expand All @@ -49,6 +48,7 @@ export class ContributionRepository {
created_at, // eslint-disable-line camelcase
updated_at, // eslint-disable-line camelcase
comments,
user,
}) => ({
id: `${number}`,
labels: gLabels.map(({ name }) => name),
Expand All @@ -64,6 +64,7 @@ export class ContributionRepository {
updatedAt: updated_at, // eslint-disable-line camelcase
commentsCount: comments,
/* eslint-enable camelcase */
createdBy: this.githubService.githubUserToAccountEntity(user),
}),
);
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion api/src/contribution/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class FilterDto {
export class GetContributionsResponseDto extends GeneralResponseDto {
@ValidateNested({ each: true })
@Type(() => ContributionEntity)
contributions!: Model<ContributionEntity, "project">[];
contributions!: Model<ContributionEntity, "project" | "createdBy">[];

@ValidateNested({ each: true })
@Type(() => FilterDto)
Expand Down
14 changes: 14 additions & 0 deletions packages/models/src/contribution/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ exports[`should match snapshot when providing all fields: output 1`] = `
ContributionEntity {
"commentsCount": 0,
"createdAt": "2020-02-02T20:22:02.000Z",
"createdBy": AccountEntity {
"avatarUrl": "https://avatars.githubusercontent.com/u/20110076?v=4",
"id": "github/20110076",
"name": "Zakaria Mansouri",
"profileUrl": "/Account/github/20110076",
"username": "ZibanPirate",
},
"id": "71",
"labels": [
"discussion",
Expand All @@ -32,6 +39,13 @@ exports[`should match snapshot when providing required fields only: output 1`] =
ContributionEntity {
"commentsCount": 0,
"createdAt": "2020-02-02T20:22:02.000Z",
"createdBy": AccountEntity {
"avatarUrl": "https://avatars.githubusercontent.com/u/20110076?v=4",
"id": "github/20110076",
"name": "Zakaria Mansouri",
"profileUrl": "/Account/github/20110076",
"username": "ZibanPirate",
},
"id": "71",
"labels": [
"discussion",
Expand Down
7 changes: 7 additions & 0 deletions packages/models/src/contribution/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ runDTOTestCases(
createdAt: "2020-02-02T20:22:02.000Z",
updatedAt: "2020-02-02T20:22:02.000Z",
url: "https://github.com/dzcode-io/leblad/issues/71",
createdBy: {
id: "github/20110076",
username: "ZibanPirate",
name: "Zakaria Mansouri",
profileUrl: "/Account/github/20110076",
avatarUrl: "https://avatars.githubusercontent.com/u/20110076?v=4",
},
},
{},
);
5 changes: 5 additions & 0 deletions packages/models/src/contribution/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Type } from "class-transformer";
import { IsDateString, IsNumber, IsString, IsUrl, ValidateNested } from "class-validator";
import { BaseEntity, Model } from "src/_base";
import { AccountEntity } from "src/account";
import { ProjectReferenceEntity } from "src/project-reference";

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

@ValidateNested()
@Type(() => AccountEntity)
createdBy!: Model<AccountEntity>;

@IsString()
type!: "issue" | "pullRequest";

Expand Down

0 comments on commit dc8530d

Please sign in to comment.