|
| 1 | +package repo |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/ipfs/go-datastore" |
| 7 | + "github.com/qri-io/dataset" |
| 8 | +) |
| 9 | + |
| 10 | +// ChangeRequestStore is the interface for storying & manipulating Change Requests |
| 11 | +// Qri repos should embed a ChangeRequestStore |
| 12 | +type ChangeRequestStore interface { |
| 13 | + // Put a change request into the store |
| 14 | + PutChangeRequest(path datastore.Key, cr *ChangeRequest) error |
| 15 | + // Get a change request by it's path |
| 16 | + GetChangeRequest(path datastore.Key) (*ChangeRequest, error) |
| 17 | + // accept an open change request |
| 18 | + // AcceptChangeRequest(path datastore.Key) error |
| 19 | + // decline an open change request |
| 20 | + // DeclineChangeRequest(path datastore.Key) error |
| 21 | + // get change requests for a given target |
| 22 | + ChangeRequestsForTarget(target datastore.Key, limit, offset int) ([]*ChangeRequest, error) |
| 23 | + // list change requests in this store |
| 24 | + ListChangeRequests(limit, offset int) ([]*ChangeRequest, error) |
| 25 | +} |
| 26 | + |
| 27 | +const ( |
| 28 | + // open change requests haven't been addressed yet |
| 29 | + ChangeRequestStatusOpen = "open" |
| 30 | + // accepted change requests have been merged into |
| 31 | + // the target history |
| 32 | + ChangeRequestStatusAccepted = "accepted" |
| 33 | + // declined change requests will not be merged into |
| 34 | + // the target history |
| 35 | + ChangeRequestStatusDeclined = "declined" |
| 36 | +) |
| 37 | + |
| 38 | +// ChangeRequests are proposed additions to the history of a given dataset |
| 39 | +type ChangeRequest struct { |
| 40 | + // status of this request. one of: open,accepted,declined |
| 41 | + Status string `json:"status"` |
| 42 | + // created marks the time this change request was created |
| 43 | + Created time.Time `json:"created"` |
| 44 | + // the dataset this change is aimed at. The |
| 45 | + // history of target must match the history |
| 46 | + // of change up until new history entries |
| 47 | + // TODO - should changes be targeting a mutable history? |
| 48 | + Target datastore.Key `json:"target"` |
| 49 | + // path to HEAD of the change history |
| 50 | + Path datastore.Key `json:"path"` |
| 51 | + // The actual change history. All relevant details must be stored |
| 52 | + // in the dataset itself. Title & description of the change goes |
| 53 | + // into this dataset's commit. |
| 54 | + Change *dataset.Dataset `json:"change"` |
| 55 | +} |
| 56 | + |
| 57 | +// accept an open change request, advancing the name of the dataset |
| 58 | +// that refer to the target path to the newly-added history |
| 59 | +func AcceptChangeRequest(r Repo, path datastore.Key) (err error) { |
| 60 | + cr, err := r.GetChangeRequest(path) |
| 61 | + if err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + |
| 65 | + cr.Status = ChangeRequestStatusAccepted |
| 66 | + if err := r.PutChangeRequest(path, cr); err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + // TODO - place all datasets related to this history chain in the store |
| 71 | + ds := &dataset.Dataset{Previous: path} |
| 72 | + for { |
| 73 | + if ds.Previous.Equal(cr.Target) { |
| 74 | + break |
| 75 | + } |
| 76 | + // datasets can sometimes resolve over the netowork, so this get / put |
| 77 | + // combination is required |
| 78 | + ds, err = r.GetDataset(ds.Previous) |
| 79 | + if err != nil { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + if err = r.PutDataset(ds.Previous, ds); err != nil { |
| 84 | + return |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + name, err := r.GetName(cr.Target) |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + if err := r.PutName(name, cr.Path); err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// decline an open change request |
| 101 | +func DeclineChangeRequest(r Repo, path datastore.Key) error { |
| 102 | + cr, err := r.GetChangeRequest(path) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + cr.Status = ChangeRequestStatusDeclined |
| 107 | + return r.PutChangeRequest(path, cr) |
| 108 | +} |
0 commit comments