Skip to content

Commit

Permalink
Adds an admin task for Updating User Metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
richmahn committed Jul 11, 2024
1 parent 39f5243 commit 6e0cbc2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2886,6 +2886,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
Expand Down
1 change: 1 addition & 0 deletions services/cron/tasks_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func initBasicTasks() {
registerDeletedBranchesCleanup()
/*** DCS Customizations ***/
registerUpdateDoor43MetadataTask()
registerUpdateUserMetadataTask()
registerLoadMetadataSchemasTask()
/*** END DCS Customizations ***/
if !setting.Repository.DisableMigrations {
Expand Down
10 changes: 10 additions & 0 deletions services/cron/tasks_dcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
55 changes: 39 additions & 16 deletions services/door43metadata/door43metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 6e0cbc2

Please sign in to comment.