diff --git a/h-tests.el b/h-tests.el index edc7197..19d8b1f 100644 --- a/h-tests.el +++ b/h-tests.el @@ -216,6 +216,22 @@ For reference: a empty test root looks like this: (insert-file-contents "./tests/fixtures/github-get-request-ko.txt") (should (equal (h--fetch-github-parse-response (current-buffer)) nil)))) +;; Gitea + +(ert-deftest h--tests-fetch-gitea-parse-response-ok () + "Test h--tests-fetch-gitea-parse-response with a fixture." + (with-temp-buffer + (insert-file-contents "./tests/fixtures/gitea-get-request-ok.txt") + (should (equal (h--fetch-gitea-parse-response (current-buffer)) + '((ssh . "gitea@git.alternativebit.fr:NinjaTrappeur/h.el.git") + (https . "https://git.alternativebit.fr/NinjaTrappeur/h.el.git")))))) + +(ert-deftest h--tests-fetch-gitea-parse-response-ko () + "Test h--tests-fetch-gitea-parse-response with a fixture." + (with-temp-buffer + (insert-file-contents "./tests/fixtures/gitea-get-request-ko.txt") + (should (equal (h--fetch-gitea-parse-response (current-buffer)) nil)))) + ;; Test repo URI parser ;;;;;;;;;;;;;;;;; diff --git a/h.el b/h.el index 5bdaba5..c2875d0 100644 --- a/h.el +++ b/h.el @@ -225,18 +225,44 @@ Returns nil if the repo does not exists." nil))) ;;; Gitea Fetcher -;; (defun h--query-gitea (instance-url user-name repo-name callback) -;; "Queries the INSTANCE-URL gitea instance to retrieve a repo informations. -;; This function will first try to dertermine whether the -;; USER-NAME/REPO-NAME exists. +(defun h--query-gitea-owner-repo (instance-url user-name repo-name callback) + "Queries the INSTANCE-URL gitea instance to retrieve a repo informations. +This function will first try to dertermine whether the +USER-NAME/REPO-NAME exists. -;; If so, calls the CALLBACK function with a alist containing the ssh and -;; https clone URLs. If the repo does not exists, calls the callback with -;; nil as parameter." -;; (url-retrieve -;; (format "%s/api/v1/repos/%s/%s" instance-url user-name repo-name) -;; (lambda (&rest _rest) (funcall callback (h--fetch-gitea-parse-response(current-buffer)))))) -;; ; Get /repos/owner/repo +If so, calls the CALLBACK function with a alist containing the ssh and +https clone URLs. If the repo does not exists, calls the callback with +nil as parameter." + (url-retrieve + (format "https://%s/api/v1/repos/%s/%s" instance-url user-name repo-name) + (lambda (&rest _rest) (funcall callback (h--fetch-gitea-parse-response(current-buffer)))))) + +(defun h--fetch-gitea-parse-response (response-buffer) + "Parse the RESPONSE-BUFFER containing a GET response from the Gitea API. + +Parsing a response from a GET https://instance/api/v1/repos/user/repo request. + +If the repo does exists, returns a alist in the form of: + +`( + (ssh . SSH-CHECKOUT-URL) + (https . HTTPS-CHECKOUT-URL) +) + +Returns nil if the repo does not exists." + (progn (set-buffer response-buffer) + (goto-char (point-min)) + (if (not(eq(re-search-forward "^HTTP/1.1 200 OK$" nil t) nil)) + (progn + (goto-char (point-min)) + (re-search-forward "^$") + (delete-region (point) (point-min)) + (let* ((parsed-buffer (json-read)) + (ssh-url (alist-get 'ssh_url parsed-buffer)) + (https-url (alist-get 'clone_url parsed-buffer))) + `((ssh . ,ssh-url) + (https . ,https-url)))) + nil))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Internal: repo URI parser diff --git a/tests/fixtures/gitea-get-request-ko.txt b/tests/fixtures/gitea-get-request-ko.txt new file mode 100644 index 0000000..664e7aa --- /dev/null +++ b/tests/fixtures/gitea-get-request-ko.txt @@ -0,0 +1,11 @@ +HTTP/1.1 404 Not Found +Server: nginx +Date: Thu, 16 Jun 2022 16:54:27 GMT +Content-Type: application/json;charset=utf-8 +Content-Length: 108 +Connection: keep-alive +Set-Cookie: _csrf=A_VRhpShoO8WBnU2gvYqJZ_z2_86MTY1NTM5ODQ2Nzc5NjM3NTEwNw; Path=/; Expires=Fri, 17 Jun 2022 16:54:27 GMT; HttpOnly; Secure; SameSite=Lax +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN + +{"errors":null,"message":"The target couldn't be found.","url":"https://git.alternativebit.fr/api/swagger"} diff --git a/tests/fixtures/gitea-get-request-ok.txt b/tests/fixtures/gitea-get-request-ok.txt new file mode 100644 index 0000000..df52ff2 --- /dev/null +++ b/tests/fixtures/gitea-get-request-ok.txt @@ -0,0 +1,11 @@ +HTTP/1.1 200 OK +Server: nginx +Date: Thu, 16 Jun 2022 16:53:31 GMT +Content-Type: application/json;charset=utf-8 +Content-Length: 1674 +Connection: keep-alive +Set-Cookie: _csrf=000000000000000000000000000000000000000000000000000000; Path=/; Expires=Fri, 17 Jun 2022 16:53:31 GMT; HttpOnly; Secure; SameSite=Lax +X-Content-Type-Options: nosniff +X-Frame-Options: SAMEORIGIN + +{"id":28,"owner":{"id":1,"login":"NinjaTrappeur","full_name":"","email":"felix@alternativebit.fr","avatar_url":"https://git.alternativebit.fr/avatars/326105984221f71c9e555addc514dae6","language":"","is_admin":false,"last_login":"0001-01-01T00:00:00Z","created":"2018-04-10T16:10:10+02:00","restricted":false,"active":false,"prohibit_login":false,"location":"","website":"","description":"","visibility":"public","followers_count":0,"following_count":0,"starred_repos_count":0,"username":"NinjaTrappeur"},"name":"h.el","full_name":"NinjaTrappeur/h.el","description":"","empty":false,"private":false,"fork":false,"template":false,"parent":null,"mirror":false,"size":107,"html_url":"https://git.alternativebit.fr/NinjaTrappeur/h.el","ssh_url":"gitea@git.alternativebit.fr:NinjaTrappeur/h.el.git","clone_url":"https://git.alternativebit.fr/NinjaTrappeur/h.el.git","original_url":"","website":"","stars_count":0,"forks_count":0,"watchers_count":1,"open_issues_count":0,"open_pr_counter":0,"release_counter":0,"default_branch":"master","archived":false,"created_at":"2020-10-25T15:53:38+01:00","updated_at":"2022-06-16T18:43:05+02:00","permissions":{"admin":false,"push":false,"pull":true},"has_issues":true,"internal_tracker":{"enable_time_tracker":true,"allow_only_contributors_to_track_time":true,"enable_issue_dependencies":true},"has_wiki":true,"has_pull_requests":true,"has_projects":false,"ignore_whitespace_conflicts":false,"allow_merge_commits":true,"allow_rebase":true,"allow_rebase_explicit":true,"allow_squash_merge":true,"default_merge_style":"merge","avatar_url":"","internal":false,"mirror_interval":"","mirror_updated":"0001-01-01T00:00:00Z","repo_transfer":null}