Skip to content

Commit

Permalink
feat: support challenge instances or flavors as parameter for 'pathwa…
Browse files Browse the repository at this point in the history
…r admin redump'
  • Loading branch information
moul committed Jul 29, 2020
1 parent c1bc314 commit 6add5df
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
47 changes: 26 additions & 21 deletions go/pkg/pwapi/api_admin-redump.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package pwapi

import (
"context"
"strconv"
"fmt"

"go.uber.org/zap"
"go.uber.org/multierr"
"pathwar.land/pathwar/v2/go/pkg/errcode"
"pathwar.land/pathwar/v2/go/pkg/pwdb"
)
Expand All @@ -14,32 +14,37 @@ func (svc *service) AdminRedump(ctx context.Context, in *AdminRedump_Input) (*Ad
return nil, errcode.ErrRestrictedArea
}

var errs error
for _, identifier := range in.Identifiers {
nb, err := strconv.Atoi(identifier)
if err != nil {
// for now, we only accept numerical identifiers, but the plan is to also search for names
return nil, errcode.TODO.Wrap(err)
instances := []int64{}
if id, err := pwdb.GetIDBySlugAndKind(svc.db, identifier, "challenge-instance"); err == nil {
instances = append(instances, id)
} else if id, err := pwdb.GetIDBySlugAndKind(svc.db, identifier, "challenge-flavor"); err == nil {
var flavor pwdb.ChallengeFlavor
if err = svc.db.Preload("Instances").First(&flavor, id).Error; err != nil {
errs = multierr.Append(errs, err)
continue
}
for _, instance := range flavor.Instances {
instances = append(instances, instance.ID)
}
}

// FIXME: support passing IDs for other entities too
var instance pwdb.ChallengeInstance
err = svc.db.First(&instance, nb).Error
if err != nil {
return nil, errcode.TODO.Wrap(err)
if len(instances) == 0 {
errs = multierr.Append(errs, fmt.Errorf("no such entry for %q", identifier))
continue
}

switch instance.Status {
case pwdb.ChallengeInstance_NeedRedump:
svc.logger.Debug("level already marked as needing a redump", zap.Int64("instance-id", instance.ID))
default:
err = svc.db.Model(&instance).Updates(&pwdb.ChallengeInstance{Status: pwdb.ChallengeInstance_NeedRedump}).Error
if err != nil {
return nil, errcode.TODO.Wrap(err)
}
svc.logger.Debug("level marked as needing a redump", zap.Int64("instance-id", instance.ID))
err := svc.db.
Model(pwdb.ChallengeInstance{}).
Where("id IN (?)", instances).
Updates(&pwdb.ChallengeInstance{Status: pwdb.ChallengeInstance_NeedRedump}).
Error
if err != nil {
errs = multierr.Append(errs, err)
}
}

out := AdminRedump_Output{}
return &out, nil
return &out, errs
}
5 changes: 5 additions & 0 deletions go/pkg/pwdb/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ func GetIDBySlugAndKind(db *gorm.DB, slug string, kind string) (int64, error) {
Model(ChallengeFlavor{}).
Where("id = ? OR slug = ? OR slug = ?", slug, slug, slug+"@default").
Pluck("id", &ids).Error
case "challenge-instance":
err = db.
Model(ChallengeInstance{}).
Where("id = ?", slug).
Pluck("id", &ids).Error
default:
return 0, errcode.ErrUnknownDBKind
}
Expand Down

0 comments on commit 6add5df

Please sign in to comment.