Skip to content

A peer-to-peer stack for code collaboration

Diff app (#370)

Merle Breitkreuz50db59de1118
diff --git a/bin/rad-diff b/bin/rad-diff
new file mode 100755
index 0000000..9fa852f
--- /dev/null
+++ b/bin/rad-diff
@@ -0,0 +1,341 @@
+#!/usr/bin/env radicle
+
+;; TODO:
+;; - don't use make-chain-ref when we don't need loading
+;; - handle parsing numbers in cmd-parsing/validation logic
+
+
+(load! (find-module-file! "prelude.rad"))
+(load! (find-module-file! "monadic/diff.rad"))
+(load! (find-module-file! "prelude/io-utils.rad"))
+(load! (find-module-file! "prelude/cmd-parsing.rad"))
+(import prelude/validation :as 'validation)
+
+(def base-name "http://localhost:8000/chains/")
+
+(def make-name
+ (fn [chain]
+ (if (eq? (take 7 chain) "http://")
+ chain
+ (string-append base-name chain))))
+
+(def make-chain-ref
+ (fn [chain]
+ (ref (chain/load-chain! (make-name chain)))))
+
+(def valid-list-options
+ ["--new" "--pending" "--retracted" "--accepted" "--rejected" "--fancy"])
+
+(def list-opts-help
+ (foldl (fn [a opt] (string-append a " [" opt "]")) "" valid-list-options))
+
+(def help
+ (string-append
+ "rad-diff - Radicle Diff CLI
+
+ Usage:
+ rad-diff list <chain-name>" list-opts-help "
+ rad-diff create-chain <chain-name>
+ rad-diff propose <commit> <chain-name>
+ rad-diff [retract|accept|reject|show] <diff-no> <chain-name>
+ rad-diff comment <diff-no> <comment> <chain-name>
+ rad-diff done
+ rad-diff help
+
+ list - Select from diffs to view or comment on
+ done - Mark all diffs as 'seen'
+ propose - Create a new diff in $EDITOR
+ show - Show a diff
+ comment - Shortcut for adding a comment
+ retract - Retract your own diff
+ accept - Accept a diff
+ reject - Reject a diff
+ create-chain - Create a new diffs chain
+ help - Print this help and exit
+
+ If <chain-name> begins with 'http://', that URL will be used. Otherwise,
+ " base-name " will be prepended to <chain-name>." ))
+
+(def cmd-parse-failure
+ (fn [error]
+ (parse-failure error help)))
+
+(def mark-last-seen
+ (fn []
+ (write-file-key! ".radicle-diffs" :last-mark-done (now!))))
+
+(init-file-dict! ".radicle-diffs")
+
+(def verify-diff-number
+ (fn [diff-no chain-ref]
+ (match (lookup-default diff-no #f (list-diffs chain-ref))
+ #f (do
+ (put-str! (string-append "DIFF " (show diff-no) " does not exist."))
+ (exit! 1))
+ 'the-diff the-diff)))
+
+;; git interactions
+
+(def verify-commit
+ (fn [commit]
+ (match (process-with-stdout-stderr-exitcode! "git" ["show" commit] "")
+ [_ _ :ok] commit
+ _ (do
+ (put-str! "Commit can't be found")
+ (exit! 1)))))
+
+(def get-patch!
+ (fn [commit]
+ (string/unlines (process-with-stdout! "git" ["format-patch" "-1" commit "--stdout"] ""))))
+
+(def get-title!
+ (fn [commit]
+ (string/unlines (process-with-stdout! "git" ["show" "-s" "--format=%s" commit] ""))))
+
+(def get-description!
+ (fn [commit]
+ (string/unlines (process-with-stdout! "git" ["show" "-s" "--format=%b" commit] ""))))
+
+(def get-author-name!
+ (fn [commit]
+ (string/unlines (process-with-stdout! "git" ["show" "-s" "--format=%aN" commit] ""))))
+
+(def get-author-email!
+ (fn [commit]
+ (string/unlines (process-with-stdout! "git" ["show" "-s" "--format=%aE" commit] ""))))
+
+(def get-short-hash!
+ (fn [commit]
+ (string/unlines (process-with-stdout! "git" ["rev-parse" "--short" commit] ""))))
+
+(def get-git-username!
+ (string/unlines (process-with-stdout! "git" ["config" "user.name"] "")))
+
+(def apply-patch!
+ "Given a patch, apply it to the current branch"
+ (fn [patch]
+ (def file (unlines (shell-with-stdout! "mktemp" "")))
+ (put-str! file)
+ (write-file! file patch)
+ (process! "git" ["am" file] "")))
+
+(def checkout-diff!
+ "Creates a new branch diff/<DIFF-ID> based on the DIFF, and switches to it."
+ (fn [chain diff-no]
+ (def chain-ref (make-chain-ref chain))
+ (def diff (verify-diff-number diff-no chain-ref))
+ (match (process! "git" ["checkout" "-b" (string-append "diff/" (show diff-no))] "")
+ :ok (apply-patch! (lookup :patch diff))
+ _ (put-str! (string-append "Command 'git checkout -b diff/" (show diff-no) "' failed")))))
+
+(def push-diff!
+ "Merges the DIFF to master, and pushes the changes."
+ (fn [chain-ref diff-no]
+ (def diff (verify-diff-number diff-no chain-ref))
+ (process! "git" ["checkout" "master"] "")
+ (match (apply-patch! (lookup :patch diff))
+ :ok (do
+ (put-str! "ok!")
+ (process! "git" ["push"] ""))
+ _ (do
+ (put-str! "Resolve any conflicts and then run 'git push'")
+ (exit! 1)))))
+
+(def new-diff!
+ "Creates a new diff on `chain` with a patch from the given `commit`."
+ (fn [chain commit]
+ (verify-commit commit)
+ (def patch (get-patch! commit))
+ (def author-name (get-author-name! commit))
+ (def author-email (get-author-name! commit))
+ (def short-hash (get-short-hash! commit))
+ (def title (get-title! commit))
+ (def description (get-description! commit))
+ (def diff {:short-hash short-hash
+ :title title
+ :description description
+ :author-name author-name
+ :author-email author-email
+ :patch patch})
+ (simple-create-diff! (make-chain-ref chain) diff)
+ (put-str! (string-append "Proposing " short-hash " to " chain))))
+
+;; showing diffs
+
+(def pretty-state
+ (fn [state]
+ (drop 1 (show state))))
+
+(def pretty-headline
+ (fn [i]
+ (match i
+ '{:number n
+ :title tit
+ :state s
+ :author-name an
+ :short-hash sh}
+ (string-append
+ "(" (pretty-state s) ") [" an "] " sh " - " tit " | DIFF " (show n)))))
+
+(def pretty-comment
+ (fn [c]
+ (match c
+ '{:created-at c-a
+ :body b
+ :author-name a-n}
+ (string-append "### " a-n " [" c-a "]\n\n" b "\n"))))
+
+(def pretty-diff
+ (fn [i]
+ (match i
+ '{:patch p
+ :created-at c-a
+ :comments cs}
+ (string-append
+ (pretty-headline i) "\n\n"
+ "created at " c-a "\n\n"
+ p "\n\n"
+ "Comments\n"
+ "--------\n\n"
+ (string/unlines (map pretty-comment cs))))))
+
+(def comment-divider "----------------------------------------")
+
+(def add-comment-md
+ (fn [x]
+ (string-append
+ "\n" comment-divider "\n"
+ "---Add comment above the line.\n\n"
+ x)))
+
+(def list-items
+ "Lists all diffs and filters them by `options`. If `--new` is passed as an
+ option only recent items are shown. If `--fancy is passed, the list is opened
+ via fzf."
+ (fn [chain options]
+ (def chain-ref (make-chain-ref chain))
+
+ (def mk-key
+ (fn [v]
+ (match v
+ (/as 'a {:activity :new-diff
+ :diff 'diff})
+ [(string-append "[new/changed] " (pretty-headline diff)) a]
+ (/as 'a {:activity :new-comments
+ :diff 'diff})
+ [(string-append "[new comments] " (pretty-headline diff)) a]
+ (/as 'i {:diff 'diff})
+ [(pretty-headline diff) i])))
+
+ (def filt
+ (match (filter (fn [opt] (not (elem? opt [:new :fancy]))) options)
+ [] (fn [x] #t)
+ _ (fn [x] (elem? (lookup :state (lookup :diff x)) options))))
+
+ (def items
+ (map mk-key
+ (filter filt
+ (match [(elem? :new options) (read-file-key! ".radicle-diffs" :last-mark-done)]
+ [#t [:just 't]] (recent-activity chain-ref t)
+ _ (map (fn [i] {:diff i})
+ (reverse (values (list-diffs chain-ref))))))))
+
+ (def items-dict (dict-from-seq items))
+ (if (empty-seq? items)
+ (put-str! "No recent items!")
+ (if (elem? :fancy options)
+ (match (fzf-select-with-preview!
+ (map first items)
+ (fn [s] (pretty-diff (lookup :diff (lookup s items-dict)))))
+ (/just 's)
+ (do (def selected (lookup s items-dict))
+ (def diff-number (match selected {:diff {:number 'n}} n))
+ (def pretty-item
+ (add-comment-md
+ (pretty-diff (lookup :diff selected))))
+ (def edited-item (edit-in-editor! pretty-item))
+ (def added-comment
+ (string/unlines
+ (take-while (fn [x] (not (eq? x comment-divider)))
+ (string/lines edited-item))))
+ (put-str!
+ (if (eq? added-comment "")
+ "Item was not updated."
+ (do (simple-add-comment! chain-ref diff-number added-comment get-git-username!)
+ (string-append "Sent comment: " added-comment)))))
+ _ (put-str! "No selection made."))
+ (map (fn [d] (put-str! (first d))) items)))))
+
+(def show-diff!
+ "Shows a single DIFF `n`"
+ (fn [chain-name n]
+ (if (number? n)
+ (if (integral? n)
+ (do (def chain-ref (make-chain-ref chain-name))
+ (def diff (verify-diff-number n chain-ref))
+ (put-str! (pretty-diff diff)))
+ (cmd-parse-failure "Diff numbers must be whole numbers!"))
+ (cmd-parse-failure "To show a diff, please provide the diff number."))))
+
+(def new-diff-chain!
+ "Create a new DIFF chain with the given URL"
+ (fn [chain-name]
+ (create-diffs-chain! (make-name chain-name))))
+
+(def edit-diff-state!
+ "Edit the state of a DIFF. If the new state is `:accepted` the DIFF is then
+ also merged to master and the changes pushed."
+ (fn [chain-name n new-state]
+ (if (number? n)
+ (if (integral? n)
+ (do (def chain-ref (make-chain-ref chain-name))
+ (verify-diff-number n chain-ref)
+ (simple-edit-diff! chain-ref n {:state new-state})
+ (if (eq? new-state :accepted)
+ (do
+ (push-diff! chain-ref n)
+ (put-str! (string-append "Diff " (show n) " has been " (pretty-state new-state) " and merged.")))
+ (put-str! (string-append "Diff " (show n) " has been " (pretty-state new-state)))))
+ (cmd-parse-failure "Diff numbers must be whole numbers!"))
+ (cmd-parse-failure "To change the state, please provide the diff number."))))
+
+(def create-comment!
+ "Add a new `comment` to DIFF."
+ (fn [chain-name n comment]
+ (if (number? n)
+ (if (integral? n)
+ (do (def chain-ref (make-chain-ref chain-name))
+ (verify-diff-number n chain-ref)
+ (simple-add-comment! chain-ref n comment get-git-username!)
+ (put-str! (string-append "Added comment to Diff " (show n))))
+ (cmd-parse-failure "Diff numbers must be whole numbers!"))
+ (cmd-parse-failure "To create a comment, please provide the diff number."))))
+
+(def list-options
+ (fn [os]
+ (map (fn [o]
+ (if (member? o valid-list-options)
+ (read (string-append ":" (drop 2 o)))
+ (cmd-parse-failure (string-append "Invalid list option: " o)))) os)))
+
+(def /list-cmd
+ (fn [chain-name opts]
+ (/cmd-2-opts "list" chain-name opts valid-list-options list-options help)))
+
+(def args (get-args!))
+(match args
+ (/list-cmd 'chain-name 'options) (list-items chain-name options)
+ (/cmd-2 "propose" 'commit 'chain-name help) (new-diff! (make-name chain-name) commit)
+ (/cmd-2 "checkout" 'diff-no 'chain-name help) (checkout-diff! chain-name (read diff-no))
+ (/cmd-2 "show" 'diff-no 'chain-name help) (show-diff! chain-name (read diff-no))
+ (/cmd-2 "retract" 'diff-no 'chain-name help) (edit-diff-state! chain-name (read diff-no) :retracted)
+ (/cmd-2 "accept" 'diff-no 'chain-name help) (edit-diff-state! chain-name (read diff-no) :accepted)
+ (/cmd-2 "reject" 'diff-no 'chain-name help) (edit-diff-state! chain-name (read diff-no) :rejected)
+ ["comment" 'diff-no 'comment 'chain-name] (create-comment! chain-name (read diff-no) comment)
+ (/cmd-1 "create-chain" 'chain-name help) (new-diff-chain! chain-name)
+ (/cmd-0 "done" help) (mark-last-seen)
+ ["help"] (put-str! help)
+ ["-h"] (put-str! help)
+ ["--help"] (put-str! help)
+ (/cons 'cmd _) (cmd-parse-failure (string-append "Unknown command: " cmd))
+ [] (put-str! help))
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index 71d02d2..d6d6eb0 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -887,6 +887,12 @@ is used.
This requires the ``prelude/test/primitive-stub`` script to be loaded.
+``(process-with-stdout-stderr-exitcode! command args to-write)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Like ``process-with-stdout!``, but returns a vec
+``[stdout stderr exitcode]``.
+
``prelude/bool``
----------------
@@ -1184,6 +1190,11 @@ Install test doubles for the ``send!`` and ``receive!`` primitives that
use a mutable dictionary to store RSMs. Requires
``rad/test/stub-primitives`` to be loaded
+``(send-signed-command! chain chain-id cmd payload)``
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Send a command signed by the keys in ``my-keys.rad``.
+
``prelude/state-machine``
-------------------------
diff --git a/rad/monadic/diff-remote.rad b/rad/monadic/diff-remote.rad
new file mode 100644
index 0000000..b234f96
--- /dev/null
+++ b/rad/monadic/diff-remote.rad
@@ -0,0 +1,226 @@
+(import prelude/basic :unqualified)
+(import prelude/bool :unqualified)
+(import prelude/dict :unqualified)
+(import prelude/set :as 'set)
+(import prelude/ref :unqualified)
+(import prelude/validation :as 'validator)
+(import prelude/patterns :unqualified)
+(import prelude/util :unqualified)
+(import prelude/lens :unqualified)
+
+(def chain-id
+ "The name of this chain."
+ "monadic/radicle/diffs")
+
+(def diffs
+ "DIFFs are stored in a dict in a ref, keyed by diff-number."
+ (ref {}))
+
+(def diff-counter
+ "We use a stateful counter for giving diff numbers."
+ (make-counter))
+
+(def used-nonces
+ "We keep track of the UUIDs which have been used as nonces in inputs."
+ (ref set/empty))
+
+(def unused-nonce?
+ "Check that a nonce has not been used already."
+ (fn [nonce]
+ (not (set/member? nonce (read-ref used-nonces)))))
+
+(def mark-used-nonce
+ "Mark a UUID as used."
+ (fn [nonce]
+ (modify-ref used-nonces (fn [s] (set/insert nonce s)))))
+
+(def set-diffs
+ "A helper to set stuff in the diffs ref."
+ (fn [l v]
+ (set-ref diffs l v)))
+
+(def validator/nonce
+ "Validator for unused nonces."
+ (validator/and
+ [validator/uuid
+ (validator/pred "Nonce not already used" unused-nonce?)]))
+
+(def validator/text
+ "A validator for text."
+ (fn [max-len]
+ (validator/and
+ [(validator/type :string)
+ (validator/pred (string-append "< " (show max-len) " chars in length")
+ (fn [s] (< (string-length s) max-len)))])))
+
+(def input-keys
+ {:nonce validator/nonce
+ :chain-id (validator/= chain-id)})
+
+(def validator/time-created
+ (validator/and
+ [(validator/keys
+ {;; NOTE that timestamps are claimed by authors, are not verified, and may
+ ;; be incorrect.
+ :created-at validator/timestamp
+ :modified-at validator/timestamp})
+ (validator/pred "`:modified-at` and `:create-at` are equal"
+ (fn [t]
+ (eq? (lookup :created-at t)
+ (lookup :modified-at t))))]))
+
+(def validator/time-modified
+ (validator/keys
+ {:modified-at validator/timestamp}))
+
+(def validator/input
+ "Checks that the input as a valid new *chain input*, that is, it has an unused
+ nonce, is marked with this chain name, and is signed."
+ (validator/and
+ [(validator/keys input-keys)
+ validator/signed]))
+
+(def validator/diff-number
+ "A validator for numbers which refer to existing diffs."
+ (validator/pred "Existing diff number" (fn [n] (member? n (read-ref diffs)))))
+
+(def comment-keys
+ "A comment's body should be text that isn't too long."
+ {:body (validator/text 4000)})
+
+(def validator/full-comment
+ (validator/keys comment-keys))
+
+(def validator/diff-reference
+ "Refers to an existing issue."
+ (validator/optional-keys {:issue-number validator/diff-number}))
+
+(def validator/comment-input
+ "A comment input should refer to an existing issue."
+ (validator/and
+ [validator/input
+ validator/time-created
+ validator/full-comment
+ validator/diff-reference]))
+
+(def diff-keys
+ {:title (validator/text 100)
+ :description (validator/text 4000)
+ :patch (validator/type :string)
+ :state (validator/member [:pending :retracted :accepted :rejected])
+ :comments (validator/every validator/full-comment)})
+
+(def validator/full-diff (validator/keys diff-keys))
+
+(def validator/diff-input
+ "An diff should have all the required fields, be a signed input, etc."
+ (validator/and
+ [validator/input
+ validator/time-created
+ validator/full-diff]))
+
+(def edit-keys
+ (fn [ks]
+ (<> ks
+ (<> (keys input-keys)
+ [:diff-number :modified-at :author :signature]))))
+
+(def validator/diff-edit-input
+ "An input to edit an diff."
+ (validator/and
+ [validator/input
+ validator/time-modified
+ validator/diff-reference
+ (validator/contains-only (edit-keys (keys diff-keys)))]))
+
+(def strip-input
+ "After an entity input has been processed/verified, we no longer need some of
+ the fields."
+ (fn [e]
+ (delete-many [:chain-id :signature :nonce :diff-number] e)))
+
+(def create-diff
+ "Create an diff from a dict, checking that it is valid and storing it in `diffs`."
+ (fn [i]
+ (validator/diff-input i)
+ (def n (diff-counter :next))
+ (def i_
+ (strip-input
+ (<> i
+ {:number n})))
+ (set-diffs (@ n) i_)
+ (mark-used-nonce (lookup :nonce i))))
+
+(def over-diffs
+ "A helper to modify stuff in the diff ref."
+ (fn [l f]
+ (over-ref diffs l f)))
+
+(def add-comment
+ "Add a comment to a diff."
+ (fn [c]
+ (validator/comment-input c)
+ (def c_ (strip-input c))
+ (over-diffs
+ (.. (@ (lookup :diff-number c)) (@ :comments))
+ (fn [cs] (add-right c_ cs)))
+ (mark-used-nonce (lookup :nonce c))))
+
+(def merge-new
+ (fn [new]
+ (fn [old]
+ (<> old new))))
+
+(def edit-diff
+ "Modify some of the attributes of an diff."
+ (fn [i]
+ (validator/diff-edit-input i)
+ (def i_ (delete :author (strip-input i)))
+ (over-diffs (@ (lookup :diff-number i)) (merge-new i_))))
+
+(def list-diffs
+ "List existing diffs."
+ (fn [] (read-ref diffs)))
+
+(def commands
+ "The set of allowed chain commands."
+ (ref
+ {'create-diff create-diff
+ 'list-diffs list-diffs
+ 'edit-diff edit-diff
+ 'add-comment add-comment}))
+
+(def process-command
+ "Function that maps post-eval commands to existing functions"
+ (fn [expr]
+ (def command (first expr))
+ (def args (rest expr))
+ (def do-this (lookup command (read-ref commands)))
+ (apply do-this args)))
+
+;; Move to eval-fn-app & updatable-eval from chain.rad to a pure file so it may
+;; be imported
+
+(def eval-fn-app
+ "Given a state, a function, an argument and a callback, returns
+ the result of evaluating the function call on the arg in the given
+ state, while also calling the callback on the result."
+ (fn [state f arg cb]
+ (def exec (base-eval (list f (list 'quote arg)) state))
+ (cb (first exec))
+ exec))
+
+(def updatable-eval
+ "Given an evaluation function `f`, returns a new one which augments `f` with a new
+ command `(update expr)` which evaluates arbitrary expression using `base-eval`."
+ (fn [sub-eval]
+ (fn [expr state]
+ (match expr
+ (/cons 'cmd _) (if (eq? cmd 'update)
+ (base-eval (nth 1 expr) state)
+ (sub-eval expr state))
+ _ (sub-eval expr state)))))
+
+(def eval (updatable-eval
+ (fn [expr state]
+ (eval-fn-app state 'process-command expr (fn [x] x)))))
diff --git a/rad/monadic/diff.rad b/rad/monadic/diff.rad
new file mode 100644
index 0000000..234960c
--- /dev/null
+++ b/rad/monadic/diff.rad
@@ -0,0 +1,140 @@
+(load! (find-module-file! "prelude.rad"))
+
+(import prelude/lens :unqualified)
+(import prelude/chain '[send-code! ]:unqualified)
+(import prelude/time '[install-fake-clock] :unqualified)
+(import prelude/io :as 'io)
+
+(def radicle-diff-chain-id
+ "The name of this chain."
+ "monadic/radicle/diffs")
+
+(def create-diffs-chain!
+ "Create a remote diff chain with the given url."
+ (fn [url]
+ (chain/send-prelude! url)
+ (send-code! url (find-module-file! "monadic/diff-remote.rad"))))
+
+(def simple-diff
+ "Create an open diff with no comments or labels."
+ (fn [p]
+ (def t (now!))
+ (<> {:state :pending
+ :comments []
+ :created-at t
+ :modified-at t}
+ p)))
+
+(def simple-comment
+ "Create a comment for an issue."
+ (fn [diff-number bod author-name]
+ (def t (now!))
+ {:body bod
+ :author-name author-name
+ :diff-number diff-number
+ :created-at t
+ :modified-at t}))
+
+(def edit-diff!
+ "Edit a remote diff with the keys in `my-keys.rad`."
+ (fn [chain i]
+ (chain/send-signed-command! chain radicle-diff-chain-id 'edit-diff i)))
+
+(def simple-edit-diff!
+ (fn [chain n i]
+ (edit-diff! chain
+ (<> {:diff-number n
+ :modified-at (now!)}
+ i))))
+
+(def create-diff!
+ "Create a new remote diff with the keys in `my-keys.rad`."
+ (fn [chain i]
+ (chain/send-signed-command! chain radicle-diff-chain-id 'create-diff i)))
+
+(def add-comment!
+ "Create a remote comment with the keys in `my-keys.rad`."
+ (fn [chain c]
+ (chain/send-signed-command! chain radicle-diff-chain-id 'add-comment c)))
+
+(def simple-create-diff!
+ "Create a remote diff with sensible defaults."
+ (fn [chain diff]
+ (create-diff! chain (simple-diff diff))))
+
+(def simple-add-comment!
+ "Create a remote comment."
+ (fn [chain n b name]
+ (add-comment! chain (simple-comment n b name))))
+
+(def list-diffs
+ "Return the full map of diffs."
+ (fn [chain]
+ (lookup :result (chain/eval-in-chain '(list-diffs) (read-ref chain)))))
+
+(def recent-activity
+ "Returns the activity on the chain since a timestamp."
+ (fn [chain from]
+ (def mod-at
+ (fn [x]
+ (unix-epoch
+ (lookup :modified-at x))))
+ (def u (unix-epoch from))
+ (def since
+ (fn [xs]
+ (filter (fn [x] (< u (mod-at x))) xs)))
+ (def most-recent-first
+ (fn [xs]
+ (sort-by (fn [x] (- 0 (mod-at x)))
+ xs)))
+ (def all-diffs (values (list-diffs chain)))
+ (def is (map (fn [i]
+ {:activity :new-diff
+ :modified-at (lookup :modified-at i)
+ :diff i})
+ (since all-diffs)))
+ (def cs
+ (cat-maybes
+ (map (fn [i]
+ (def xs (most-recent-first (since (lookup :comments i))))
+ (if (eq? xs [])
+ :nothing
+ [:just
+ {:activity :new-comments
+ :modified-at (lookup :modified-at (first xs))
+ :diff i}]))
+ all-diffs)))
+ (most-recent-first (<> is cs))))
+
+(:test
+ "The monadic diffs chain works."
+ [:setup
+ (do (chain/install-remote-chain-fake)
+ (install-fake-clock)
+ (io/install-fake-filesystem!
+ { "../my-keys.rad" (show (gen-key-pair! (default-ecc-curve)))
+ })
+ (def chain-name "http://foo")
+ (def chain (ref (chain/new-chain chain-name)))
+ (create-diffs-chain! chain-name)
+ (simple-create-diff! chain {:title "title0" :description "desc0" :patch "patch0"})
+ (simple-create-diff! chain {:title "title1" :description "desc1" :patch "patch1"})
+ (simple-add-comment! chain 0 "comment0" "name0")
+ (simple-add-comment! chain 1 "comment1" "name1")
+ (simple-edit-diff! chain 0 {:state :accepted})
+ (def t (now!))
+ (simple-create-diff! chain {:title "new" :description "new" :patch "new"})
+ (simple-create-diff! chain {:title "newer" :description "newer" :patch "newer"})
+ (simple-add-comment! chain 0 "new" "name0")
+ (chain/update-chain-ref! chain)
+ (def diffs (list-diffs chain))
+ (def new (recent-activity chain t)))]
+ [ (length (seq diffs)) ==> 4 ]
+ [ (view (.. (@ 0) (@ :title)) diffs) ==> "title0" ]
+ [ (view (.. (@ 1) (@ :title)) diffs) ==> "title1" ]
+ [ (view (... [(@ 1) (@ :comments) (@nth 0) (@ :body)]) diffs) ==> "comment1" ]
+ [ (view (.. (@ 0) (@ :state)) diffs) ==> :accepted ]
+ [ (length new) ==> 3 ]
+ [ (view (.. (@nth 0) (@ :activity)) new) ==> :new-comments ]
+ [ (view (... [(@nth 1) (@ :diff) (@ :title)]) new) ==> "newer" ]
+ )
diff --git a/rad/monadic/issues.rad b/rad/monadic/issues.rad
index 2887263..3536d28 100644
--- a/rad/monadic/issues.rad
+++ b/rad/monadic/issues.rad
@@ -22,22 +22,6 @@
(chain/send-prelude! url)
(send-code! url (find-module-file! "monadic/issue-remote.rad"))))
-(def sign-entity!
- "Assumes a key pair is stored at `my-keys.rad`. Using that key pair, will sign a
- dict by adding `:author` and `:signature` fields, so that it is valid
- according to `validator/signed`, while also adding a `:nonce`."
- (fn [e]
- (def nonce (uuid!))
- (def e_ (<> e {:chain-id radicle-issues-chain-id
- :nonce nonce}))
- (def ks (read-file-value! "my-keys.rad"))
- (def sig
- (gen-signature! (lookup :private-key ks)
- (show e_)))
- (<> e_
- {:author (lookup :public-key ks)
- :signature sig})))
-
(def simple-issue
"Create an open issue with no comments or labels."
(fn [i]
@@ -58,32 +42,25 @@
:created-at t
:modified-at t}))
-(def send-signed-command!
- "Send a command signed by the keys in `my-keys.rad`."
- (fn [chain cmd payload]
- (chain/send!
- (lookup :url (read-ref chain))
- [(list cmd (sign-entity! payload))])))
-
(def create-issue!
"Create a new remote issue with the keys in `my-keys.rad`."
(fn [chain i]
- (send-signed-command! chain 'create-issue i)))
+ (chain/send-signed-command! chain radicle-issues-chain-id 'create-issue i)))
(def add-comment!
"Create a remote comment with the keys in `my-keys.rad`."
(fn [chain c]
- (send-signed-command! chain 'add-comment c)))
+ (chain/send-signed-command! chain radicle-issues-chain-id 'add-comment c)))
(def edit-issue!
"Edit a remote issue with the keys in `my-keys.rad`."
(fn [chain i]
- (send-signed-command! chain 'edit-issue i)))
+ (chain/send-signed-command! chain radicle-issues-chain-id 'edit-issue i)))
(def edit-comment!
"Edit a remote comment with the keys in `my-keys.rad`."
(fn [chain c]
- (send-signed-command! chain 'edit-comment c)))
+ (chain/send-signed-command! chain radicle-issues-chain-id 'edit-comment c)))
(def simple-create-issue!
"Create a remote issue with sensible defaults."
@@ -198,7 +175,7 @@
(do (chain/install-remote-chain-fake)
(install-fake-clock)
(io/install-fake-filesystem!
- { "my-keys.rad" (show (gen-key-pair! (default-ecc-curve)))
+ { "../my-keys.rad" (show (gen-key-pair! (default-ecc-curve)))
})
(def chain-name (string-append "http://foo"))
(def chain (ref (chain/new-chain chain-name)))
diff --git a/rad/prelude/chain.rad b/rad/prelude/chain.rad
index 49abdb1..e29dd17 100644
--- a/rad/prelude/chain.rad
+++ b/rad/prelude/chain.rad
@@ -2,9 +2,9 @@
:doc "Functions for simulating remote chains."
:exports '[new-chain load-chain! eval-in-chain update-chain-ref! eval
updatable-eval update-chain! eval-fn-app send-prelude!
- send-code! send! receive! install-remote-chain-fake]}
+ send-code! send! receive! install-remote-chain-fake send-signed-command!]}
-(import prelude/io '[read-file-values!] :as 'io)
+(import prelude/io '[read-file-value! read-file-values!] :as 'io)
(import prelude/ref '[modify-ref] :unqualified)
(import prelude/lens '[make-lens .. ... view set over @ @def] :unqualified)
(import prelude/basic :unqualified)
@@ -280,3 +280,25 @@
"Send code from a file to a remote chain."
(fn [chain-id filename]
(send! chain-id (io/read-file-values! filename))))
+
+(def sign-entity!
+ "Assumes a key pair is stored at `my-keys.rad`. Using that key pair, will sign a
+ dict by adding `:author` and `:signature` fields, so that it is valid
+ according to `validator/signed`, while also adding a `:nonce`."
+ (fn [e chain-id]
+ (def nonce (uuid!))
+ (def e_ (<> e {:chain-id chain-id
+ :nonce nonce}))
+ (def ks (io/read-file-value! (find-module-file! "../my-keys.rad")))
+ (def sig
+ (gen-signature! (lookup :private-key ks)
+ (show e_)))
+ (<> e_
+ {:author (lookup :public-key ks)
+ :signature sig})))
+
+(def send-signed-command!
+ "Send a command signed by the keys in `my-keys.rad`."
+ (fn [chain chain-id cmd payload]
+ (send! (lookup :url (read-ref chain))
+ [(list cmd (sign-entity! payload chain-id))])))
diff --git a/rad/prelude/cmd-parsing.rad b/rad/prelude/cmd-parsing.rad
index 0fb0099..d81a01b 100644
--- a/rad/prelude/cmd-parsing.rad
+++ b/rad/prelude/cmd-parsing.rad
@@ -3,7 +3,6 @@
:exports
'[parse-failure /cmd-0 /cmd-1 /cmd-2 /cmd-2-opts]}
-(load! "rad/prelude.rad")
(import prelude/validation :as 'validation)
(def parse-failure
diff --git a/rad/prelude/io.rad b/rad/prelude/io.rad
index e56c209..63072ca 100644
--- a/rad/prelude/io.rad
+++ b/rad/prelude/io.rad
@@ -3,7 +3,7 @@
:exports '[print! shell! process! read-line! read-file-value! read-file-values!
shell-with-stdout! shell-no-stdin! process-with-stdout! write-file!
init-file-dict! read-file-key! write-file-key! ls! modify-file!
- install-fake-filesystem!
+ install-fake-filesystem! process-with-stdout-stderr-exitcode!
]}
(import prelude/patterns :unqualified)
@@ -163,6 +163,30 @@ Example: `(process! \"ls\" [\"-Glah\"] \"\")`. "
[ (process-with-stdout! "cat" [] "there") ==> ["there"] ]
)
+(def process-with-stdout-stderr-exitcode!
+ "Like `process-with-stdout!`, but returns a vec `[stdout stderr exitcode]`."
+ (fn [command args to-write]
+ (def cp
+ { :cmdspec [:raw command args]
+ :stdin :create-pipe
+ :stdout :create-pipe
+ :stderr :create-pipe
+ })
+ (match (system! cp)
+ {:stdin [:just 'stdin]
+ :stdout [:just 'stdout]
+ :stderr [:just 'stderr]
+ :proc 'ph
+ } (do (write-handle! stdin to-write)
+ (close-handle! stdin)
+ [(read-contents-handle! stdout) (read-contents-handle! stderr) (wait-for-process! ph)])
+ _ (throw 'io-error "expected only stdin, stdout and stderr to be open"))))
+
+(:test "process-with-stdout-stderr-exitcode!"
+ [ (process-with-stdout-stderr-exitcode! "echo" ["hi"] "") ==> [["hi"] [] :ok] ]
+ [ (process-with-stdout-stderr-exitcode! "false" [] "") ==> [[] [] [:error 1]] ]
+)
+
;; General utilities
(def ls!
"List the contents of the current working directory"
@@ -212,4 +236,12 @@ Example: `(process! \"ls\" [\"-Glah\"] \"\")`. "
(original-read-file! path))
))
(write-ref primitive-stub-ref/read-file! read-file)
+ (def original-find-module-file! (read-ref primitive-stub-ref/find-module-file!))
+ (def find-module-file
+ (fn [path]
+ (if (member? path files)
+ path
+ (original-find-module-file! path))
+ ))
+ (write-ref primitive-stub-ref/find-module-file! find-module-file)
))
diff --git a/rad/tests/stub-primitives.rad b/rad/tests/stub-primitives.rad
index c802c30..6e144e2 100644
--- a/rad/tests/stub-primitives.rad
+++ b/rad/tests/stub-primitives.rad
@@ -18,3 +18,8 @@
(def primitive-stub-ref/read-file! (ref read-file!))
(def read-file!
(fn [path] ((read-ref primitive-stub-ref/read-file!) path)))
+
+;; find-module-file!
+(def primitive-stub-ref/find-module-file! (ref find-module-file!))
+(def find-module-file!
+ (fn [path] ((read-ref primitive-stub-ref/find-module-file!) path)))
diff --git a/test/all.rad b/test/all.rad
index 297de59..7c70611 100755
--- a/test/all.rad
+++ b/test/all.rad
@@ -5,6 +5,7 @@
(load! (find-module-file! "tests/stub-primitives.rad"))
(load! (find-module-file! "prelude.rad"))
+(load! (find-module-file! "monadic/diff.rad"))
(load! (find-module-file! "monadic/issues.rad"))
(load! (find-module-file! "monadic/names.rad"))