Skip to content

A peer-to-peer stack for code collaboration

5.7 KB
Raw Blame
#!/usr/bin/env radicle

(load! (find-module-file! "prelude.rad"))
(load! (find-module-file! "monadic/issue.rad"))
(load! (find-module-file! "monadic/project.rad"))
(load! (find-module-file! "prelude/io-utils.rad"))

(file-module! "prelude/error-messages.rad")
(file-module! "monadic/items.rad")

(import prelude/error-messages :as 'error)
(import monadic/items :as 'items)

(import prelude/validation :as 'validation)
(import prelude/machine :as 'machine)

(def machine-name (fn [] (get-rsm-of-type! :rad-issue)))

(def valid-states ["open" "closed"])

(def status-opts ["--filter-by-state" "--state" "-s"])

(def list-opts-help
  (string-append
    "[("
    (string/intercalate " | " status-opts)
    ") <state>] [--fancy]"))

(def help
  (string-append
  "rad issue - Radicle Issue CLI

   Usage:
        rad issue list " list-opts-help "
        rad issue new
        rad issue [show | close | mark-read | mark-unread] <issue-number>
        rad issue comment <issue-number> <comment>
        rad issue all-read
        rad issue help

     list         - Lists all issues
                    The list can be filtered by state:
                      " (string/intercalate ", " valid-states) "
                    The flags for filtering by state:
                      " (string/intercalate ", " status-opts) "
                    Filtering by multiple states is possible via '-s <state1> -s
                    <state2> ...'.
                    If the option '--fancy' is appended, the list is opened via
                    fzf (note that fzf has to be installed).
                    If the option '--unread' is appended, only unread issues
                    are listed.
                    Unread issues are marked with a * after the timestamp.
     new          - Create a new issue in $EDITOR
     show         - Show an issue
     comment      - Add a comment to an issue
     close        - Close an issue
                    This command is restricted to the maintainer of the project.
     mark-read    - Mark issue as read
     mark-unread  - Mark issue as unread
     all-read     - Mark all issues as read
     help         - Print this help and exit
       "))

(def cmd-parse-failure
  (fn [error]
    (parse-failure error help)))

(def show-issue!
  "Shows a single ISSUE `n`"
  (fn [n machine]
    (def issue (items/verify-item-number n (list-issues machine) :issue))
    (put-str! (items/pretty-item-view (items/enrich-item machine issue)))
    (newness/mark-read! machine n)))

(def close-issue
  (fn [n machine]
    (catch 'daemon-error
      (do
        (items/verify-item-number n (list-issues machine) :issue)
        (simple-edit-issue! machine n {:state :closed})
        (put-str! (string-append "Issue #" (show n) " has been closed."))
        (newness/mark-read! machine n))
      (fn [_]
        (put-str! (error/state-change-failure :issue "closed"))
        (exit! 1)))))

(def create-comment!
  "Add a new `comment` to ISSUE."
  (fn [machine n comment]
    (items/verify-item-number n (list-issues machine) :issue)
    (simple-add-comment! machine n comment (get-git-username!))
    (put-str! (string-append "Added comment to issue #" (show n)))
    (newness/mark-read! machine n)))

(def add-comment-from-editor!
  "Add a new `comment` to ISSUE opened via the list command with option `--fancy`."
  (fn [machine n added-comment]
    (simple-add-comment! machine n added-comment (get-git-username!))
    (string-append "Sent comment: " added-comment)))

(def list
  (fn [machine options]
    (def issues (values (list-issues machine)))
    (items/list-items machine options issues :issue add-comment-from-editor!)))

(def new-issue
  (fn [machine]
    (def template
";; Issue template. An empty or invalid value will abort.
{:title      \"Pick a title\"
 :body       \"Pick a body\"
 :labels     []
}
")
    (def i (read (edit-in-editor! template)))
    (def author {:git-username (get-git-username!)})
    (match (create-issue!  machine (simple-issue (<> i author)))
           ['n] (do (put-str! (string-append "Created issue #" (show n) " in " machine))
                    (newness/mark-read! machine n))
           _    (put-str! (error/no-number-returned :issue)))))

(def cmd-options
  [
    { :key :state :type :multi-opt :options ["-s" "--state" "--filter-by-state"] :possible-values valid-states :default ["open"]}
    { :key :fancy :type :flag :options ["--fancy"] :default #f }
    { :key :unread :type :flag :options ["--unread"] :default #f }
  ])

(def /list-cmd
  (fn [opts]
    (/cmd-opts "list" opts cmd-options help)))

(def args (get-args!))

(def whole-issue-num
  (fn [action num-str f]
    (whole-num help :issue action num-str f)))

(def mark-all-read!
  (fn [machine]
    (map (fn [i] (newness/mark-read! machine (lookup :number i)))
         (values (list-issues machine)))))

(machine/catch-daemon!
 (fn []
   (match args
          (/list-cmd 'options) (list (machine-name) options)
          (/cmd-0 "new" help) (new-issue (machine-name))
          (/cmd-1 "close" 'n help)            (whole-issue-num "close" n (fn [n] (close-issue n (machine-name))))
          (/cmd-1 "show" 'n help)             (whole-issue-num "show" n (fn [n] (show-issue! n (machine-name))))
          (/cmd-2 "comment" 'n 'comment help) (whole-issue-num "comment on" n (fn [n] (create-comment! (machine-name) n comment)))
          (/cmd-1 "mark-read" 'n help)        (whole-issue-num "mark as read" n (fn [n] (newness/mark-read! (machine-name) n)))
          (/cmd-1 "mark-unread" 'n help) (whole-issue-num "mark as unread" n (fn [n] (newness/mark-unread! (machine-name) n)))
          (/cmd-0 "all-read" help) (mark-all-read! (machine-name))
          (/cmd-help) (put-str! help)
          (/cons 'cmd _) (cmd-parse-failure (error/unknown-command cmd))
          [] (put-str! help))))