Skip to content

A peer-to-peer stack for code collaboration

7.4 KB
Raw Blame
;; Helpers for setting up and interacting with the issue machine (as defined in
;; rad/monadic/issue-remote.rad)
{:module  'monadic/issue
 :doc     "Functions for interacting with issue machine"
 :exports '[create-issue-machine!]}

(import prelude/lens :unqualified)
(import prelude/machine '[send-code! send-signed-command!] :unqualified)
(import prelude/time '[install-fake-clock] :unqualified)
(import prelude/io :as 'io)
(import prelude/key-management '[use-fake-keys! get-keys!] :unqualified)

(def radicle-issue-machine-id
  "The name of this machine."
  "monadic/radicle/issue")

(def create-issue-machine!
  "Create a remote issue machine with the given url."
  (fn []
    (def owner-keys (lookup :public-key (get-keys!)))
    (def id (machine/new-machine!))
    (machine/send-prelude! id)
    (send-code! id (find-module-file! "monadic/issue-remote.rad"))
    (machine/send-signed-command! id radicle-issue-machine-id 'add-admin {:key owner-keys})
    id))

(def simple-issue
  "Create an open issue with no comments or labels."
  (fn [i]
    (def t (now!))
    (<> {:labels      []
         :state       :open
         :comments    []
         :created-at  t
         :modified-at t}
        i)))

(def simple-comment
  "Create a comment for an issue."
  (fn [issue-number bod author-name]
    (def t (now!))
    {:body         bod
     :git-username  author-name
     :issue-number issue-number
     :created-at   t
     :modified-at  t}))

(def create-issue!
  "Create a new remote issue with the keys in `my-keys.rad`."
  (fn [machine i]
    (machine/send-signed-command! machine radicle-issue-machine-id 'create-issue i)))

(def add-comment!
  "Create a remote comment with the keys in `my-keys.rad`."
  (fn [machine c]
    (machine/send-signed-command! machine radicle-issue-machine-id 'add-comment c)))

(def edit-issue!
  "Edit a remote issue with the keys in `my-keys.rad`."
  (fn [machine i]
    (machine/send-signed-command! machine radicle-issue-machine-id 'edit-issue i)))

(def edit-comment!
  "Edit a remote comment with the keys in `my-keys.rad`."
  (fn [machine c]
    (machine/send-signed-command! machine radicle-issue-machine-id 'edit-comment c)))

(def simple-create-issue!
  "Create a remote issue with sensible defaults."
  (fn [machine tit bod]
    (create-issue! machine (simple-issue {:title tit :body bod}))))

(def simple-add-comment!
  "Create a remote comment."
  (fn [machine in b name]
    (add-comment! machine (simple-comment in b name))))

(def simple-edit-issue!
  (fn [machine n i]
    (edit-issue! machine
                 (<> {:issue-number n
                      :modified-at (now!)}
                     i))))

(def simple-edit-comment!
  (fn [machine n i c]
    (edit-comment! machine
                   (<> {:issue-number n
                        :comment-index i
                        :modified-at (now!)}
                       c))))

(def list-issues
  "Return the full map of issues."
  (fn [machine]
    (machine/query! machine '(list-issues))))

(def import-github
  "Imports the github issues stored in file `githubissues.rad`. To create this
  file use the `radicle-github-issues` executable. Will add a note to all
  `:body`s that this is an imported entity."
  (fn [machine]
    (def note-imported
      (fn [x]
        (insert
         :body
         (string-append
          (lookup :body x)
          "\n\n*Imported from GitHub.* Original author: `"
          (lookup :github-username x)
          "`.")
         x)))
    (def note-comments
      (fn [i] (over (@ :comments) (fn [cs] (map note-imported cs)) i)))
    (def issues (read-file-value! "githubissues.rad"))
    (def imported
      (map
       (fn [i] (insert :modified-at (lookup :created-at i) (note-imported (note-comments i))))
       issues))
    (map (fn [i] (create-issue! machine i)) imported)))

(def short
  "Short render of an issue."
  (fn [i]
    (string-append
     (show (lookup :number i))
     ": "
     (lookup :title i))))

(def list-last
  "Get the last `n` issues."
  (fn [machine n]
    (def is (sort-by (fn [i] (- 0 (lookup :number i))) (values (list-issues machine))))
    (take n is)))

(:test
 "The monadic issue machine works."
 [:setup
  (do (machine/install-remote-machine-fake)
      (def owner-keys (gen-key-pair! (default-ecc-curve)))
      (def non-owner-keys (gen-key-pair! (default-ecc-curve)))
      (set-fake-keys! owner-keys)
      (def machine (create-issue-machine!))
      (simple-create-issue! machine "title0" "body0")
      (simple-create-issue! machine "title1" "body1")
      (simple-add-comment! machine 0 "comment0" "name0")
      (simple-edit-issue! machine 0 {:state :closed})
      (simple-edit-comment! machine 0 0 {:body "comment0-edit"})
      (set-fake-keys! non-owner-keys)
      (simple-add-comment! machine 1 "comment1" "name1")
      (simple-create-issue! machine "new" "new")
      (simple-create-issue! machine "newer" "newer")
      (simple-add-comment! machine 0 "new" "name0")
      (def is (list-issues machine))
      (set-fake-keys! owner-keys))
 ]
 [ (length (seq is)) ==> 4 ]
 [ (view (.. (@ 0) (@ :title)) is) ==> "title0" ]
 [ (view (.. (@ 1) (@ :title)) is) ==> "title1" ]
 [ (view (... [(@ 1) (@ :comments) (@nth 0) (@ :body)]) is) ==> "comment1" ]
 [ (view (... [(@ 0) (@ :comments) (@nth 0) (@ :body)]) is) ==> "comment0-edit" ]
 [ (short (first (values is))) ==> "0: title0" ]
 [ (short (first (list-last machine 1))) ==> "3: newer" ]
 [ (view (.. (@ 0) (@ :state)) is) ==> :closed ]
 ;; authorization
 ;; owner can edit comments
 [ (do
      (set-fake-keys! owner-keys)
      (catch 'validation-failure
             (do (simple-edit-comment! machine 0 0 {:body "edit"})
                 :valid)
             (fn [_] :not-valid))) ==> :valid
 ]
 ;; author can edit comments
 [ (do
      (set-fake-keys! non-owner-keys)
      (catch 'validation-failure
             (do (simple-edit-comment! machine 0 1 {:body "edit"})
                 :valid)
             (fn [_] :not-valid))) ==> :valid
 ]
 ;; non-owner/non-author cannot edit comments
 [ (do
      (set-fake-keys! non-owner-keys)
      (catch 'validation-failure
             (do (simple-edit-comment! machine 0 0 {:body "edit"})
                 :valid)
             (fn [_] :not-valid))) ==> :not-valid
 ]
 ;; owner can edit issues
 [ (do
      (set-fake-keys! owner-keys)
      (catch 'validation-failure
             (do (simple-edit-issue! machine 2 {:state :closed})
                 :valid)
             (fn [_] :not-valid))) ==> :valid
 ]
 ;; author can edit comments
 [ (do
      (set-fake-keys! non-owner-keys)
      (catch 'validation-failure
             (do (simple-edit-comment! machine 0 1 {:body "edit"})
                 :valid)
             (fn [_] :not-valid))) ==> :valid
 ]
 ;; author can edit issues
 [ (do
      (set-fake-keys! non-owner-keys)
      (catch 'validation-failure
             (do (simple-edit-issue! machine 2 {:state :closed})
                 :valid)
             (fn [_] :not-valid))) ==> :valid
 ]
 ;; non-owner/non-author cannot edit comments
 [ (do
      (set-fake-keys! non-owner-keys)
      (catch 'validation-failure
             (do (simple-edit-comment! machine 0 0 {:body "edit"})
                 :valid)
             (fn [_] :not-valid))) ==> :not-valid
 ]
 ;; non-owner/non-author cannot edit issues
 [ (do
      (set-fake-keys! non-owner-keys)
      (catch 'validation-failure
             (do (simple-edit-issue! machine 0 {:state :closed})
                 :valid)
             (fn [_] :not-valid))) ==> :not-valid
 ]
)