Fix several things

- Variable names were wrong.

- Improve error checking for invalid inputs.

- Improve user questions.
This commit is contained in:
Sergio Durigan Junior 2024-03-31 21:19:58 -04:00
parent ca697024fd
commit 8d662ee833
Signed by: sergiodj
GPG Key ID: D0EB762865FC5E36
1 changed files with 41 additions and 29 deletions

View File

@ -42,11 +42,11 @@ ever commented on those issues.
Return an alist of (user . username)."
(let* ((debian-bsb-users (plz 'get
(concat sdj/sponsor-debian-pkg-debian-pkg-salsa-api-url
(concat sdj/sponsor-debian-pkg-salsa-api-url
"projects/"
(url-hexify-string "debian-brasilia-team/docs")
"/issues?per_page=100&order_by=updated_at")
:headers `(("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-debian-pkg-salsa-api-token))
:headers `(("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-salsa-api-token))
:timeout 60
:connect-timeout 60
:as #'json-read)))
@ -76,14 +76,14 @@ Return an alist of (team . id)."
"Return the full URL for an API endpoint ending with 'NAME'.
The endpoint will always end with a slash."
(concat sdj/sponsor-debian-pkg-debian-pkg-salsa-api-url name "/"))
(concat sdj/sponsor-debian-pkg-salsa-api-url name "/"))
(defun sdj/sponsor-debian-pkg--generate-team-candidates ()
"Generate a list of candidates for team names.
The generated list is actually a list of namespaces the user is a member of."
(plz 'get (sdj/sponsor-debian-pkg--api-endpoint "namespaces")
:headers `(("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-debian-pkg-salsa-api-token))
:headers `(("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-salsa-api-token))
:as #'sdj/sponsor-debian-pkg--parse-teams))
(defun sdj/sponsor-debian-pkg--team-repo-exists-p (team repo)
@ -122,7 +122,7 @@ needed."
If 'IMPORT-REPOURL' is provided, use it as the initial import."
(plz 'post (sdj/sponsor-debian-pkg--api-endpoint "projects")
:headers `(("Content-Type" . "application/json")
("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-debian-pkg-salsa-api-token))
("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-salsa-api-token))
:body (json-encode
(delete nil
`(("name" . ,projname)
@ -160,8 +160,8 @@ create the repository."
(defun sdj/sponsor-debian-pkg--confirm-username (user)
"Confirm if 'USER' exists."
(let ((u (plz 'get
(concat sdj/sponsor-debian-pkg-debian-pkg-salsa-api-url "users?username=" (url-hexify-string user))
:headers `(("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-debian-pkg-salsa-api-token))
(concat sdj/sponsor-debian-pkg-salsa-api-url "users?username=" (url-hexify-string user))
:headers `(("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-salsa-api-token))
:as #'json-read)))
(and (not (seq-empty-p u)) user)))
@ -171,27 +171,35 @@ create the repository."
Note that internally teams are actually treated as namespaces."
(condition-case err
(let ((u (plz 'get
(concat sdj/sponsor-debian-pkg-debian-pkg-salsa-api-url "namespaces/" (url-hexify-string team))
:headers `(("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-debian-pkg-salsa-api-token))
(concat sdj/sponsor-debian-pkg-salsa-api-url "namespaces/" (url-hexify-string team))
:headers `(("PRIVATE-TOKEN" . ,sdj/sponsor-debian-pkg-salsa-api-token))
:as #'json-read)))
(and (not (seq-empty-p u)) (cons team (alist-get 'id u))))
(plz-error nil)))
(defun sdj/sponsor-debian-pkg--read-sponsoree-choice ()
(defun sdj/sponsor-debian-pkg--read-sponsoree-choice (&optional optional)
"Read the sponsoree."
(let* ((sponsoreealist (sdj/sponsor-debian-pkg--generate-sponsoree-candidates))
(sponsoreechoice (completing-read "Sponsoree: " sponsoreealist)))
(or (alist-get sponsoreechoice sponsoreealist nil nil #'string=)
(sdj/sponsor-debian-pkg--confirm-username sponsoreechoice))))
(sponsoreechoice (completing-read (concat (when optional "(optional) ")
"Sponsoree: ") sponsoreealist)))
(if sponsoreechoice
(or (alist-get sponsoreechoice sponsoreealist nil nil #'string=)
(sdj/sponsor-debian-pkg--confirm-username sponsoreechoice))
(unless optional
(error "You must specify a sponsoree")))))
(defun sdj/sponsor-debian-pkg--read-team-choice (&optional dontask-p)
(defun sdj/sponsor-debian-pkg--read-team-choice (&optional dontask-p optional)
"Read the team."
(when (or dontask-p
(y-or-n-p "Would you like to specify a team? "))
(y-or-n-p (concat (when optional "(optional) ")
"Would you like to specify a team? " )))
(let* ((teamalist (sdj/sponsor-debian-pkg--generate-team-candidates))
(teamchoice (completing-read "Team: " teamalist nil t)))
(cons teamchoice
(alist-get teamchoice teamalist nil nil #'string=)))))
(if teamchoice
(cons teamchoice
(alist-get teamchoice teamalist nil nil #'string=))
(unless optional
(error "You must specify a team"))))))
(defun sdj/sponsor-debian-pkg--read-string-or-nil (prompt)
"Read a string from the minibuffer or nil."
@ -199,19 +207,23 @@ Note that internally teams are actually treated as namespaces."
(unless (string-empty-p r)
r)))
(defun sdj/sponsor-debian-pkg--read-repourl (&optional prompt)
(defun sdj/sponsor-debian-pkg--read-repourl (&optional prompt optional)
"Read a repository URL."
(let ((r (sdj/sponsor-debian-pkg--read-string-or-nil (or prompt
"Repository URL: "))))
(when r
(string-remove-suffix "/" r))))
(let ((r (sdj/sponsor-debian-pkg--read-string-or-nil
(concat (when optional "(optional) ")
(or prompt
"Repository URL: ")))))
(if r
(string-remove-suffix "/" r)
(unless optional
(error "You must specify a repository URL")))))
(defun sdj/sponsor-debian-pkg--get-namespace-from-repourl (repourl)
"Given a repository URL 'REPOURL', return its namespace."
(let ((str-no-url (or (and (string-prefix-p "git@salsa.debian.org:" repourl)
(string-remove-prefix "git@salsa.debian.org:" repourl))
(and (string-prefix-p "https://salsa.debian.org/" repourl)
(string-remove-prefix "https://salsa.debian.org/" repourl)))))
(when-let ((str-no-url (or (and (string-prefix-p "git@salsa.debian.org:" repourl)
(string-remove-prefix "git@salsa.debian.org:" repourl))
(and (string-prefix-p "https://salsa.debian.org/" repourl)
(string-remove-prefix "https://salsa.debian.org/" repourl)))))
(string-remove-suffix
"/"
(string-remove-prefix
@ -246,7 +258,7 @@ Return an alist of (ID . DATA), where 'ID' is either 'username or
(result (sdj/sponsor-debian-pkg--what-is-this-namespace namespace))
(team (or (and (eq (car result) 'team)
(cdr result))
(sdj/sponsor-debian-pkg--read-team-choice)))
(sdj/sponsor-debian-pkg--read-team-choice nil t)))
(sponsoree (or (and (eq (car result) 'username)
(cdr result))
(sdj/sponsor-debian-pkg--read-sponsoree-choice))))
@ -270,7 +282,7 @@ initial repository."
(when sponsoree-repourl
(when-let* ((sponsoree (sdj/sponsor-debian-pkg--get-namespace-from-repourl sponsoree-repourl))
(sponsoreedir (file-name-as-directory
(concat sdj/sponsor-debian-pkg-debian-pkg-path sponsoree)))
(concat sdj/sponsor-debian-pkg-path sponsoree)))
(projdir (file-name-as-directory
(concat sponsoreedir projname)))
(team-gitdir (file-name-as-directory
@ -300,7 +312,7 @@ initial repository."
(error "You must provide a valid sponsoree name and a repository URL"))
(let* ((projname (sdj/sponsor-debian-pkg--get-project-from-repourl repo))
(sponsoreedir (file-name-as-directory
(concat sdj/sponsor-debian-pkg-debian-pkg-path sponsoree)))
(concat sdj/sponsor-debian-pkg-path sponsoree)))
(projdir (file-name-as-directory
(concat sponsoreedir projname)))
(repo-is-private-p (string-search (concat "/" sponsoree "/") repo))