Move a rule several places in one change
The Rules page has a Move… button that puts a rule before any other of its chain, or at the end, in one move. Staged in a changeset, a move right after a move of the same object takes its place, so pressing ↑ three times is one operation; moving it back where it was takes the operation out. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
57ab9881db
commit
5348765d90
6 files changed
+129
-3
No files matched your search
@@ -89,6 +89,17 @@ func (s *Server) stage(w http.ResponseWriter, r *http.Request, p *auth.Principal
|
||||
if err := replay(before, cs.Ops); err != nil {
|
||||
return conflict("changeset %d can't be applied as it is (%v); review it first", id, err)
|
||||
}
|
||||
// A move right after a move of the same object takes its place:
|
||||
// moving a rule up three steps is one operation.
|
||||
kept := cs.Ops
|
||||
if n := len(kept); n > 0 && foldMove(before, &kept[n-1], &op) {
|
||||
kept = kept[:n-1]
|
||||
before = s.eng.State()
|
||||
if err := replay(before, kept); err != nil {
|
||||
return conflict("changeset %d can't be applied as it is (%v); review it first", id, err)
|
||||
}
|
||||
}
|
||||
folded := len(kept) < len(cs.Ops)
|
||||
after := before.Clone()
|
||||
objID, err := applyOp(after, &op)
|
||||
if err != nil {
|
||||
@@ -98,6 +109,13 @@ func (s *Server) stage(w http.ResponseWriter, r *http.Request, p *auth.Principal
|
||||
after.Sort()
|
||||
changes := engine.Diff(before, after)
|
||||
if len(changes) == 0 {
|
||||
// Moved back where it was: the earlier move goes too.
|
||||
if folded && !queryBool(r, "dry_run") {
|
||||
cs.Ops = kept
|
||||
if err := s.store.UpdateChangeset(r.Context(), cs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, api.MutationResponse{Changes: []model.Change{}, Changeset: cs})
|
||||
return nil
|
||||
}
|
||||
@@ -108,7 +126,7 @@ func (s *Server) stage(w http.ResponseWriter, r *http.Request, p *auth.Principal
|
||||
op.Summary = engine.UndoDescription(&h)
|
||||
}
|
||||
op.AddedAt = s.nowFn()
|
||||
for _, o := range cs.Ops {
|
||||
for _, o := range kept {
|
||||
if o.Seq >= op.Seq {
|
||||
op.Seq = o.Seq + 1
|
||||
}
|
||||
@@ -116,7 +134,7 @@ func (s *Server) stage(w http.ResponseWriter, r *http.Request, p *auth.Principal
|
||||
if op.Seq == 0 {
|
||||
op.Seq = 1
|
||||
}
|
||||
ops := append(append([]model.Op{}, cs.Ops...), op)
|
||||
ops := append(append([]model.Op{}, kept...), op)
|
||||
dry := queryBool(r, "dry_run")
|
||||
// The whole changeset, checked as it would be applied.
|
||||
res, err := s.eng.Apply(r.Context(), p, "", engine.Options{DryRun: true, Via: via(r, p)}, func(st *model.State, _ func() int64) error {
|
||||
@@ -145,6 +163,31 @@ func (s *Server) stage(w http.ResponseWriter, r *http.Request, p *auth.Principal
|
||||
return nil
|
||||
}
|
||||
|
||||
// foldMove reports whether op, a move, can take the place of prev, the
|
||||
// changeset's last operation: a move of the same object. A move puts
|
||||
// the object relative to the others, which an earlier move of it
|
||||
// doesn't change, so only the chain it moved to has to be carried over.
|
||||
// st is the changeset so far.
|
||||
func foldMove(st *model.State, prev, op *model.Op) bool {
|
||||
if prev.Op != model.OpMove || op.Op != model.OpMove || prev.Kind != op.Kind {
|
||||
return false
|
||||
}
|
||||
a, err1 := findObject(st, prev.Kind, prev.Ref)
|
||||
b, err2 := findObject(st, op.Kind, op.Ref)
|
||||
if err1 != nil || err2 != nil || a.GetID() != b.GetID() {
|
||||
return false
|
||||
}
|
||||
var was, now api.MoveInput
|
||||
if json.Unmarshal(prev.Data, &was) != nil || json.Unmarshal(op.Data, &now) != nil {
|
||||
return false
|
||||
}
|
||||
if now.Chain == "" && was.Chain != "" {
|
||||
now.Chain = was.Chain
|
||||
op.Data, _ = json.Marshal(now)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Server) createChangeset(w http.ResponseWriter, r *http.Request, p *auth.Principal) error {
|
||||
if err := require(p, auth.ResConfig, true); err != nil {
|
||||
return err
|
||||
|
||||
Reference in new issue
Block a user