ui: do not attempt running maintenance if the current user is not the maintenance owner, to avoid producing error in the Tasks tab (#1010)

This commit is contained in:
Jarek Kowalski authored and GitHub committed 2021-04-24 12:37:06 -07:00
1 parent 70a83b381b
commit d290c0a967
3 files changed
+22 -4

No files matched your search

+6
View File
@@ -488,6 +488,12 @@ func (s *Server) periodicMaintenance(ctx context.Context, rep repo.Repository) {
return
case <-time.After(maintenanceAttemptFrequency):
if owned, err := maintenance.IsOwnedByThisUser(ctx, rep); err == nil && !owned {
// maintenance not owned by this user, don't run, but keep trying because
// maintenance ownership MAY change to this user in the future.
continue
}
if err := s.taskmgr.Run(ctx, "Maintenance", "Periodic maintenance", func(ctx context.Context, _ uitask.Controller) error {
return periodicMaintenanceOnce(ctx, rep)
}); err != nil {
+14
View File
@@ -22,6 +22,10 @@ type Params struct {
FullCycle CycleParams `json:"full"`
}
func (p *Params) isOwnedByByThisUser(rep repo.Repository) bool {
return p.Owner == rep.ClientOptions().UsernameAtHost()
}
// DefaultParams represents default values of maintenance parameters.
func DefaultParams() Params {
return Params{
@@ -52,6 +56,16 @@ func HasParams(ctx context.Context, rep repo.Repository) (bool, error) {
return len(md) > 0, nil
}
// IsOwnedByThisUser determines whether current user is the maintenance owner.
func IsOwnedByThisUser(ctx context.Context, rep repo.Repository) (bool, error) {
p, err := GetParams(ctx, rep)
if err != nil {
return false, errors.Wrap(err, "error getting maintenance params")
}
return p.isOwnedByByThisUser(rep), nil
}
// GetParams returns repository-wide maintenance parameters.
func GetParams(ctx context.Context, rep repo.Repository) (*Params, error) {
md, err := manifestIDs(ctx, rep)
+2 -4
View File
@@ -140,10 +140,8 @@ func RunExclusive(ctx context.Context, rep repo.DirectRepositoryWriter, mode Mod
return errors.Wrap(err, "unable to get maintenance params")
}
if !force {
if myUsername := rep.ClientOptions().UsernameAtHost(); p.Owner != myUsername {
return NotOwnedError{p.Owner}
}
if !force && !p.isOwnedByByThisUser(rep) {
return NotOwnedError{p.Owner}
}
if mode == ModeAuto {