Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): avoid having entities with same name and type #7291

Merged
merged 3 commits into from
Feb 10, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions engine/api/v2_repository_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,12 +555,6 @@ func (api *API) analyzeRepository(ctx context.Context, projectRepoID string, ana
skippedEntities := make([]sdk.EntityWithObject, 0)
skippedFiles := make(sdk.StringSlice, 0)

// Load existing entity on the current branch
existingEntities, err := entity.LoadByRepositoryAndRefAndCommit(ctx, api.mustDB(), analysis.ProjectRepositoryID, analysis.Ref, "HEAD")
if err != nil {
return api.stopAnalysis(ctx, analysis, err)
}

// Build user role map
for _, t := range sdk.EntityTypes {
if _, has := userRoles[t]; !has {
Expand Down Expand Up @@ -798,6 +792,13 @@ skipEntity:
if currentAnalysisBranch != nil && currentAnalysisBranch.Default && currentAnalysisBranch.LatestCommit == analysis.Commit {
delOpts.WithHooks = true
}

// Load existing entity on the current branch
existingEntities, err := entity.LoadByRepositoryAndRefAndCommit(ctx, api.mustDB(), analysis.ProjectRepositoryID, analysis.Ref, "HEAD")
if err != nil {
return api.stopAnalysis(ctx, analysis, err)
}

for _, e := range existingEntities {
// If an existing entities has not been found in the current head commit (deleted or renamed)
// => remove the entity
Expand All @@ -807,6 +808,7 @@ skipEntity:
log.Warn(ctx, "user %s [%s] removed the entity %s [%s] but it has not the right to do it", u.Username, u.ID, e.Name, e.Type)
continue
}
log.Info(ctx, "deleting entity %s of type %s: file doesn't exist anymore", e.Name, e.Type)
if err := DeleteEntity(ctx, tx, &e, srvs, delOpts); err != nil {
return api.stopAnalysis(ctx, analysis, sdk.NewErrorFrom(err, fmt.Sprintf("unable to delete entity %s [%s] ", e.Name, e.Type)))
}
Expand Down Expand Up @@ -1518,6 +1520,14 @@ func (api *API) handleEntitiesFiles(ctx context.Context, ef *EntityFinder, files
if err != nil {
return nil, err
}
for _, newEntity := range es {
for _, alreadyExistEntity := range entities {
if newEntity.Name == alreadyExistEntity.Name && newEntity.Type == alreadyExistEntity.Type {
return nil, []error{sdk.NewErrorFrom(sdk.ErrInvalidData, "there is at least 2 %s with the name %s", newEntity.Type, newEntity.Name)}
}
}
}

entities = append(entities, es...)
analysis.Data.Entities = append(analysis.Data.Entities, sdk.ProjectRepositoryDataEntity{
FileName: fileName,
Expand Down