Skip to content

Commit

Permalink
check repo existing (#137)
Browse files Browse the repository at this point in the history
* check repo existing

* fix add user
  • Loading branch information
thozh authored Jul 31, 2020
1 parent e5f2cad commit 5fcd9de
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
2 changes: 1 addition & 1 deletion api/src/orm.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ConfigService } from './config/config.service';
database: configService.get('TYPEORM_DATABASE'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
synchronize: Boolean(configService.get('TYPEORM_SYNCHRONIZE')),
logging: process.env.NODE_ENV === 'dev',
// logging: process.env.NODE_ENV === 'dev',
}),
}),
],
Expand Down
41 changes: 26 additions & 15 deletions api/src/repository/repository.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,32 @@ export class RepositoryController implements CrudController<Repository> {
throw new NotFoundException();
}

const newRepo = {
name: data.name,
author: data.owner.login,
repoImg: data.owner.avatar_url,
repoUrl: data.html_url,
fullName: data.full_name,
githubId: data.id,
branch: repo.branch,
path: repo.path,
createdAt: new Date(),
users: [user],
hasYarnLock,
};

const repository = await this.service.addRepo(newRepo);
let repository = null;
const existingRepo = await this.service.findRepo({
fullName: repo.fullName,
});
if (existingRepo) {
existingRepo.users = [...existingRepo.users, user];
repository = await this.service.updateRepo(
existingRepo.id.toString(),
existingRepo,
);
} else {
const newRepo = {
name: data.name,
author: data.owner.login,
repoImg: data.owner.avatar_url,
repoUrl: data.html_url,
fullName: data.full_name,
githubId: data.id,
branch: repo.branch,
path: repo.path,
createdAt: new Date(),
users: [user],
hasYarnLock,
};
repository = await this.service.addRepo(newRepo);
}

await this.queue.add('compute_yarn_dependencies', {
repositoryFullName: repository.fullName,
Expand Down
9 changes: 4 additions & 5 deletions api/src/repository/repository.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ export class RepositoryService extends TypeOrmCrudService<Repository> {
async updateRepo(repoId: string, repo: Repository) {
const repository = await this.findRepo({ id: parseInt(repoId, 10) });
let users = repository.users;

/*
* Search if the users passed in repo exists in the repository found in db
* If it doesnt exist, adds the user to the repository
*/
if (
repo.users &&
users &&
!users.some((repoUser) =>
repo.users.some((user) => user.id === repoUser.id),
)
users.some(repoUser => repo.users.some(user => user.id === repoUser.id))
) {
users = [...repository.users, ...(repo.users || [])];
}
Expand All @@ -79,13 +78,13 @@ export class RepositoryService extends TypeOrmCrudService<Repository> {
(repo): Promise<DeleteResult | Repository | void> => {
const users = repo.users;

if (users.some((user) => user.id === userId)) {
if (users.some(user => user.id === userId)) {
if (users.length === 1) {
return this.repositoryContent.delete(repo.id);
} else {
return this.repositoryContent.save({
...repo,
users: users.filter((user) => user.id !== userId),
users: users.filter(user => user.id !== userId),
});
}
}
Expand Down
2 changes: 0 additions & 2 deletions api/src/webhooks/webhooks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { PullRequestService } from '../pull-request/pull-request.service';
import { RepositoryService } from '../repository/repository.service';
import { UsersService } from '../users/users.service';
import { WebhookInterceptor } from './webhooks.interceptor';
import { ConfigService } from '../config/config.service';

@ApiTags('webhooks')
@Controller('webhooks')
Expand All @@ -23,7 +22,6 @@ export class WebhooksController {
private readonly repositoryService: RepositoryService,
private readonly userService: UsersService,
private readonly pullRequestService: PullRequestService,
private readonly configService: ConfigService,
@InjectQueue('dependencies')
private readonly dependenciesQueue: Queue,
) {}
Expand Down

0 comments on commit 5fcd9de

Please sign in to comment.