Skip to content

Commit

Permalink
RELTEC-12350: verify url of repository in changed yaml files
Browse files Browse the repository at this point in the history
  • Loading branch information
mplushnikov committed Dec 19, 2024
1 parent 664fd0e commit 807a5c3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
34 changes: 33 additions & 1 deletion internal/service/vcswebhookshandler/vcswebhookshandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Impl struct {
CustomConfiguration config.CustomConfiguration
Logging librepo.Logging
Timestamp librepo.Timestamp
Repositories service.Repositories

Updater service.Updater

Expand All @@ -43,6 +44,7 @@ func New(
configuration librepo.Configuration,
logging librepo.Logging,
timestamp librepo.Timestamp,
repositories service.Repositories,
updater service.Updater,
vcsPlatforms map[string]VCSPlatform,
) service.VCSWebhooksHandler {
Expand All @@ -51,6 +53,7 @@ func New(
Logging: logging,
Timestamp: timestamp,
Updater: updater,
Repositories: repositories,
vcsPlatforms: vcsPlatforms,
}
}
Expand Down Expand Up @@ -209,7 +212,7 @@ func (h *Impl) validateYamlFile(ctx context.Context, path string, contents strin
} else if strings.Contains(path, "/services/") {
return parseStrict(ctx, path, contents, &openapi.ServiceDto{})
} else if strings.Contains(path, "/repositories/") {
return parseStrict(ctx, path, contents, &openapi.RepositoryDto{})
return h.verifyRepository(ctx, path, contents)
} else {
aulogging.Logger.Ctx(ctx).Info().Printf("ignoring changed file %s in pull request (neither owner info, nor service nor repository)", path)
return nil
Expand All @@ -220,6 +223,35 @@ func (h *Impl) validateYamlFile(ctx context.Context, path string, contents strin
}
}

func (h *Impl) verifyRepository(ctx context.Context, path string, contents string) error {
repositoryDto := &openapi.RepositoryDto{}
err := parseStrict(ctx, path, contents, repositoryDto)
if err == nil {
_, after, found := strings.Cut(path, "/repositories/")
repoKey, isYaml := strings.CutSuffix(after, ".yaml")
if found && isYaml {
err = h.verifyRepositoryData(ctx, repoKey, repositoryDto)
}
}
return err
}

func (h *Impl) verifyRepositoryData(ctx context.Context, dtoKey string, dtoRepo *openapi.RepositoryDto) error {
repositories, err := h.Repositories.GetRepositories(ctx, "", "", "", "")
if err == nil {
for repoKey, repo := range repositories.Repositories {
if repoKey == dtoKey {
continue
}
if repo.Url == dtoRepo.Url {
err = fmt.Errorf("url of the repository '%s' clashes with existing repository '%s'", dtoKey, repoKey)
break
}
}
}
return err
}

func parseStrict[T openapi.OwnerDto | openapi.ServiceDto | openapi.RepositoryDto](_ context.Context, path string, contents string, resultPtr *T) error {
decoder := yaml.NewDecoder(strings.NewReader(contents))
decoder.KnownFields(true)
Expand Down
2 changes: 1 addition & 1 deletion internal/web/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (a *ApplicationImpl) ConstructServices() error {
}

if a.VCSWebhooksHandler == nil {
a.VCSWebhooksHandler = vcswebhookshandler.New(a.Config, a.Logging, a.Timestamp, a.Updater, *a.VCSPlatforms)
a.VCSWebhooksHandler = vcswebhookshandler.New(a.Config, a.Logging, a.Timestamp, a.Repositories, a.Updater, *a.VCSPlatforms)
}

return nil
Expand Down

0 comments on commit 807a5c3

Please sign in to comment.