From 383472e1f8a9af7467fed6e41299a2145bf4f8a8 Mon Sep 17 00:00:00 2001 From: Richard Mahn Date: Wed, 10 Jul 2024 21:17:32 -0400 Subject: [PATCH] Adds an admin task for Updating User Metadata --- options/locale/locale_en-US.ini | 1 + services/cron/tasks_basic.go | 1 + services/cron/tasks_dcs.go | 10 +++++ services/door43metadata/door43metadata.go | 55 ++++++++++++++++------- 4 files changed, 51 insertions(+), 16 deletions(-) diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index de99221b55..3e01c4df72 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2872,6 +2872,7 @@ dashboard.rebuild_issue_indexer = Rebuild issue indexer ;;; DCS Customizations dashboard.update_metadata = Update Door43 Metadata +dashboard.update_user_metadata = Update User Metadata dashboard.load_schemas = Load Metadata Schemas ;;; END DCS Customizations diff --git a/services/cron/tasks_basic.go b/services/cron/tasks_basic.go index bb27639201..e0943689b4 100644 --- a/services/cron/tasks_basic.go +++ b/services/cron/tasks_basic.go @@ -182,6 +182,7 @@ func initBasicTasks() { registerDeletedBranchesCleanup() /*** DCS Customizations ***/ registerUpdateDoor43MetadataTask() + registerUpdateUserMetadataTask() registerLoadMetadataSchemasTask() /*** END DCS Customizations ***/ if !setting.Repository.DisableMigrations { diff --git a/services/cron/tasks_dcs.go b/services/cron/tasks_dcs.go index f26e5f2b42..29c6c660a4 100644 --- a/services/cron/tasks_dcs.go +++ b/services/cron/tasks_dcs.go @@ -20,6 +20,16 @@ func registerUpdateDoor43MetadataTask() { }) } +func registerUpdateUserMetadataTask() { + RegisterTaskFatal("update_user_metadata", &BaseConfig{ + Enabled: true, + RunAtStart: false, + Schedule: "", + }, func(ctx context.Context, _ *user_model.User, _ Config) error { + return metadata_service.UpdateUserMetadata(ctx) + }) +} + func registerLoadMetadataSchemasTask() { RegisterTaskFatal("load_schemas", &BaseConfig{ Enabled: true, diff --git a/services/door43metadata/door43metadata.go b/services/door43metadata/door43metadata.go index 9769ce8bc6..7407867236 100644 --- a/services/door43metadata/door43metadata.go +++ b/services/door43metadata/door43metadata.go @@ -173,24 +173,17 @@ func processDoor43MetadataForRepoLatestDMs(ctx context.Context, repo *repo_model return nil } -// processDoor43MetadataForRepoOwner determines a repo's owner's languages, subjects, and metadata_types and puts them in those user fields to save to DB -func processDoor43MetadataForRepoOwner(ctx context.Context, repo *repo_model.Repository) error { - if repo == nil { - return fmt.Errorf("no repository provided") - } - - if repo.Owner == nil { - err := repo.LoadOwner(ctx) - if err != nil { - return err - } +// processDoor43MetadataForUser determines the given user's languages, subjects, and metadata_types and puts them in those user fields to save to DB +func processDoor43MetadataForUser(ctx context.Context, user *user_model.User) error { + if user == nil { + return fmt.Errorf("no user provided") } - repo.Owner.RepoLanguages = models.GetRepoLanguages(ctx, repo.Owner) - repo.Owner.RepoSubjects = models.GetRepoSubjects(ctx, repo.Owner) - repo.Owner.RepoMetadataTypes = models.GetRepoMetadataTypes(ctx, repo.Owner) + user.RepoLanguages = models.GetRepoLanguages(ctx, user) + user.RepoSubjects = models.GetRepoSubjects(ctx, user) + user.RepoMetadataTypes = models.GetRepoMetadataTypes(ctx, user) - return user_model.UpdateUserCols(ctx, repo.Owner, "repo_languages", "repo_subjects", "repo_metadata_types") + return user_model.UpdateUserCols(ctx, user, "repo_languages", "repo_subjects", "repo_metadata_types") } // ProcessDoor43MetadataForRepo handles the metadata for a given repo for all its releases @@ -224,7 +217,11 @@ func ProcessDoor43MetadataForRepo(ctx context.Context, repo *repo_model.Reposito if err != nil { return err } - err = processDoor43MetadataForRepoOwner(ctx, repo) + err = repo.LoadOwner(ctx) + if err != nil { + return err + } + err = processDoor43MetadataForUser(ctx, repo.Owner) if err != nil { return err } @@ -777,6 +774,32 @@ func processDoor43MetadataForRepoRef(ctx context.Context, repo *repo_model.Repos return nil } +// UpdateUserMetadata updates the user table with their repo langauges, subjects and metadata types +func UpdateUserMetadata(ctx context.Context) error { + log.Trace("Doing: UpdateUserMetadata") + + var users []*user_model.User + err := db.GetEngine(ctx). + Join("INNER", "repository", "`repository`.owner_id = `user`.id"). + Join("INNER", "door43_metadata", "`door43_metadata`.repo_id = `repository`.id"). + GroupBy("`user`.id"). + Find(&users) + if err != nil { + log.Error("UpdateUserMetadata: %v", err) + } + + for _, user := range users { + if err := processDoor43MetadataForUser(ctx, user); err != nil { + log.Info("Failed to process metadata for user (%v): %v", user, err) + if err = system.CreateRepositoryNotice("Failed to process metadata for user (%s): %v", user.Name, err); err != nil { + log.Error("ProcessDoor43MetadataForUser: %v", err) + } + } + } + log.Trace("Finished: UpdateUserMetadata") + return nil +} + // UpdateDoor43Metadata generates door43_metadata table entries for valid repos/releases that don't have them func UpdateDoor43Metadata(ctx context.Context) error { log.Trace("Doing: UpdateDoor43Metadata")