Skip to content

A peer-to-peer stack for code collaboration

3.4 KB
Raw Blame
;; Helpers for setting up and interacting with project RSMs
{:module  'monadic/project
 :doc     "Interact with projects and project machines"
 :exports '[get-project-url! get-meta! create-project! add-rsm!
            first-rsm-of-type!
           ]}

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

(def get-project-url!
  (fn []
    (def proj (get-git-config! "radicle.project-id"))
    (if (eq? proj "")
        (do
          (put-str! "Not in a radicle project")
          (exit! 1))
        proj)))

(def rsm-id
  "The name of this RSM."
  "monadic/radicle/project")

(def set-meta!
  "Set the metadata for a project"
  (fn [machine i]
    (send-signed-command! machine rsm-id 'set-meta i)))

(def get-meta!
  "Get the metadata for a project"
  (fn [machine]
    (machine/query! machine '(get-meta))))

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

(def add-rsm!
  "Add a new RSM to the project."
  (fn [machine i]
    (send-signed-command! machine rsm-id 'add-rsm i)))

(def list-rsms!
  "List RMSs of the project."
  (fn [machine]
    (machine/query! machine '(list-rsms))))

(def add-issue-rsm!
  (fn [machine id]
    (add-rsm! machine { :id id :type :rad-issue })))

(def add-patch-rsm!
  (fn [machine id]
    (add-rsm! machine { :id id :type :rad-patch })))

(def add-repo-rsm!
  (fn [machine id]
    (add-rsm! machine { :id id :type :rad-repo })))

(def first-rsm-of-type!
  (fn [machine typ]
    (first (filter (fn [x] (eq? (lookup :type x) typ)) (list-rsms! machine)))))

(def get-rsm-of-type!
  "Gets the project via get-project-url, and looks in the project for an RSM
   of the specified type. The `:id` of the first matching RSM is returned."
  (fn [rsm-type]
    (lookup :id (first-rsm-of-type! (get-project-url!) rsm-type))))

(:test "The monadic project 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)
      (install-fake-clock)
      (def machine (create-project! {:name "test" :description "test project"}))
      (add-issue-rsm! machine "a")
      (add-patch-rsm! machine "b")
      (add-repo-rsm! machine "c"))
  ]

  [ (get-meta! machine) ==> { :name "test" :description "test project" }]
  [ (lookup :id (first-rsm-of-type! machine :rad-issue)) ==> "a" ]
  [ (lookup :id (first-rsm-of-type! machine :rad-patch)) ==> "b" ]
  [ (lookup :id (first-rsm-of-type! machine :rad-repo)) ==> "c" ]
  ;; authorization
  [ (do
       (set-fake-keys! non-owner-keys)
       (def originally (length (list-rsms! machine)))
       (def res (catch 'validation-failure
              (do (add-patch-rsm! machine "d")
                  :valid)
              ;; check that the machine has in fact not been added
              (fn [_] :not-valid)))
       [res (eq? originally (length (list-rsms! machine)))]) ==> [:not-valid #t]
  ]
)