Skip to content

A peer-to-peer stack for code collaboration

Extract verify item number from rad issue and patch (#595)

Merle Breitkreuz8b3ed6366735
diff --git a/bin/rad-issue b/bin/rad-issue
index c696382..3321b99 100755
--- a/bin/rad-issue
+++ b/bin/rad-issue
@@ -65,18 +65,10 @@
(fn [error]
(parse-failure error help)))
-(def verify-issue-number
- (fn [issue-no machine]
- (match (lookup-default issue-no #f (list-issues machine))
- #f (do
- (put-str! (error/item-not-found :issue (show issue-no)))
- (exit! 1))
- 'the-issue the-issue)))
-
(def show-issue!
"Shows a single ISSUE `n`"
(fn [n machine]
- (def issue (verify-issue-number 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)))
@@ -84,7 +76,7 @@
(fn [n machine]
(catch 'daemon-error
(do
- (verify-issue-number n machine)
+ (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))
@@ -95,7 +87,7 @@
(def create-comment!
"Add a new `comment` to ISSUE."
(fn [machine n comment]
- (verify-issue-number n machine)
+ (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)))
diff --git a/bin/rad-patch b/bin/rad-patch
index e1d7f30..1162647 100755
--- a/bin/rad-patch
+++ b/bin/rad-patch
@@ -74,14 +74,6 @@
(fn [error]
(parse-failure error help)))
-(def verify-patch-number
- (fn [patch-no machine]
- (match (lookup-default patch-no #f (list-patches machine))
- #f (do
- (put-str! (error/item-not-found :patch (show patch-no)))
- (exit! 1))
- 'the-patch the-patch)))
-
;; git interactions
(def verify-commit
@@ -131,7 +123,7 @@
(def checkout-patch!
"Creates a new branch patch/<PATCH-ID> based on the PATCH, and switches to it."
(fn [machine patch-no]
- (def patch (verify-patch-number patch-no machine))
+ (def patch (items/verify-item-number patch-no (list-patches machine) :patch))
(def patch-branch (string-append "patch/" (show patch-no)))
(process-git-with-exit!
["checkout" "-b" patch-branch]
@@ -143,7 +135,7 @@
(def push-patch!
"Merges the PATCH to master, and pushes the changes."
(fn [machine patch-no]
- (def patch (verify-patch-number patch-no machine))
+ (def patch (items/verify-item-number patch-no (list-patches machine) :patch))
(process-git-with-exit!
["checkout" "master"]
(error/checkout-master-failure))
@@ -190,7 +182,7 @@
(def show-patch!
"Shows a single PATCH `n`"
(fn [machine n]
- (def patch (verify-patch-number n machine))
+ (def patch (items/verify-item-number n (list-patches machine) :patch))
(put-str! (items/pretty-item-view (items/enrich-item machine patch)))
(newness/mark-read! machine n)))
@@ -208,7 +200,7 @@
(error/state-change-failure :patch (items/pretty-state new-state)))
(exit! 1)))
(put-str! message)))
- (verify-patch-number n machine)
+ (items/verify-item-number n (list-patches machine) :patch)
(if (eq? new-state :accepted)
(match (push-patch! machine n)
:ok (edit-state-with-feedback!
@@ -220,7 +212,7 @@
(def create-comment!
"Add a new `comment` to PATCH."
(fn [machine n comment]
- (verify-patch-number n machine)
+ (items/verify-item-number n (list-patches machine) :patch)
(simple-add-comment! machine n comment (get-git-username!))
(put-str! (string-append "Added comment to patch #" (show n)))
(newness/mark-read! machine n)))
diff --git a/rad/monadic/items.rad b/rad/monadic/items.rad
index 408e07e..e0aa36b 100644
--- a/rad/monadic/items.rad
+++ b/rad/monadic/items.rad
@@ -1,6 +1,11 @@
{:module 'monadic/items
:doc "Functions for interacting with dicts of items in machines"
- :exports '[summary pretty-item-view list-items pretty-state enrich-item]}
+ :exports
+ '[summary pretty-item-view list-items pretty-state enrich-item
+ verify-item-number]}
+
+(file-module! "prelude/error-messages.rad")
+(import prelude/error-messages :as 'error)
(def summary
"Given a list of `items`, each a dict with a `:state` key, displays a summary of
@@ -218,3 +223,13 @@
(put-str! (pretty-table-header))
(map (fn [d] (put-str! (first d))) filtered-items)
(put-str! (summary (items-name item-flag) items)))))))
+
+(def verify-item-number
+ "Verifies that the item corresponding to `item-number` is part of the given
+ list of `items`. Returns an error and exits if the item is not found."
+ (fn [item-number items item-flag]
+ (match (lookup-maybe item-number items)
+ [:just 'issue] issue
+ :nothing (do
+ (put-str! (error/item-not-found item-flag (show item-number)))
+ (exit! 1)))))