* REST API

This section contains requests to the [[https://zuul-ci.org/docs/zuul/latest/rest-api.html][Zuul REST API]] using the function =zuul--rest-request=.

** Queries

*** Tenants

List all tenants.

#+begin_src elisp :results none
  (zuul--rest-request
   "/api/tenants"
   :parser #'zuul--request-debug-parser)
#+end_src

*** Tenant status

List status of a tenant.

#+begin_src elisp :results none
  (zuul--rest-request
   (format "/api/tenant/%s/status" zuul-tenant)
   :parser #'zuul--request-debug-parser)
#+end_src

*** Buildsets

List all buildsets for a tenant.

#+begin_src elisp :results none
  (zuul--rest-request
   (format "/api/tenant/%s/buildsets" zuul-tenant)
   :params `(("limit" "200"))
   :parser #'zuul--request-debug-parser)
#+end_src

*** Buildset

List a specific buildset.

#+begin_src elisp :results none
  (zuul--rest-request
   (format "/api/tenant/%s/buildsets" zuul-tenant)
   :params `(("change" "300203"))
   :parser #'zuul--request-debug-parser)
#+end_src

*** Builds

List all builds for a tenant.

#+begin_src elisp :results none
  (zuul--rest-request
   (format "/api/tenant/%s/builds" zuul-tenant)
   :params `(("limit" "100"))
   :parser #'zuul--request-debug-parser)
#+end_src

*** Build

List a specific build.

#+begin_src elisp :results none
  (zuul--rest-request
   (format "/api/tenant/%s/builds" zuul-tenant)
   :params '(("uuid" "0646db9be9a0450c87a37d7d7386b17a"))
   :parser #'zuul--request-debug-parser)
#+end_src

* Use zuul.el

** Retrieve builds

Retrieve builds using =zuul-get-builds=.

#+begin_src elisp
  (zuul-get-builds
   :change "300203")
#+end_src
** Retrieve buildset

Retrieve builds using =zuul-get-buildsets=.

#+begin_src elisp :results none
  (zuul-get-buildsets
   :change "300203")
#+end_src
** Open a build log

Select and open a build retrieved with =zuul-get-builds=.

#+begin_src elisp :results none
  (let ((zuul-build-display-buffer-action 'display-buffer-pop-up-window))
    (zuul--open-build-log
     (zuul-get-builds
      :change "300203")))
#+end_src

Select and open a build retrieved with =zuul-get-buildsets=.

#+begin_src elisp :results none
  (let ((zuul-build-display-buffer-action 'display-buffer-pop-up-window))
    (zuul--open-build-log
     (zuul-get-buildsets
      :change "300203")))
#+end_src

* Screenshots

** Anonymize

Add functions used to =anonymize= data.

#+begin_src elisp :results none
  (defun zuul-anonymize-build-name (build)
    (with-temp-buffer
      (insert (let-alist (zuul-data build) .job_name))
      (unpackaged/lorem-ipsum-overlay t)
      (buffer-string)))

  (defun zuul-anonymize-buffer-with-overlay (&rest args)
    (unpackaged/lorem-ipsum-overlay)
    (setq mode-line-format nil))

  (defun zuul-anonymize-buffer (&rest args)
    (unpackaged/lorem-ipsum-overlay)
    (setq mode-line-format nil))

  (defun zuul-anonymize-silence-messages (orig-fun &rest args)
    (let ((inhibit-message t))
      (apply orig-fun args)))

  (defun zuul-anonymize-task-command (data)
    (let ((command
           (pcase-let* ((`(,_hostname . ,data) (plist-get data ':host))
                        (cmd-str (let-alist data .cmd)))
             (if (stringp cmd-str)
                 cmd-str
               (mapconcat #'identity cmd-str " ")))))
      (with-temp-buffer
        (insert command)
        (unpackaged/lorem-ipsum-overlay t)
        (buffer-string))))

  (defun zuul-anonymize-playbook-name (data)
    (with-temp-buffer
      (insert (let-alist (plist-get data ':playbook)
                .playbook))
      (unpackaged/lorem-ipsum-overlay t)
      (buffer-string)))
#+end_src

Add =advices= around functions that could be sensitive.

#+begin_src elisp :results none
  (advice-add 'zuul--data-host-cmd-str :override #'zuul-anonymize-task-command)
  (advice-add 'zuul--data-playbook-name-str :override #'zuul-anonymize-playbook-name)
  (advice-add 'url-insert-file-contents :around #'zuul-anonymize-silence-messages)
  (advice-add 'compile-goto-error :around #'zuul-anonymize-silence-messages)
  (advice-add 'compile-goto-error :after #'zuul-anonymize-buffer)
  (advice-add 'zuul-log-mode :after #'zuul-anonymize-buffer-with-overlay)
  (advice-add 'zuul--build-name-str :override #'zuul-anonymize-build-name)
  (toggle-frame-tab-bar)
#+end_src

Remove advices and settings to restore original behavior.

#+begin_src elisp :results none
  (advice-remove 'zuul--data-host-cmd-str #'zuul-anonymize-task-command)
  (advice-remove 'zuul--data-playbook-name-str #'zuul-anonymize-playbook-name)
  (advice-remove 'url-insert-file-contents #'zuul-anonymize-silence-messages)
  (advice-remove 'compile-goto-error #'zuul-anonymize-silence-messages)
  (advice-remove 'compile-goto-error #'zuul-anonymize-buffer)
  (advice-remove 'zuul-log-mode #'zuul-anonymize-buffer-with-overlay)
  (advice-remove 'zuul--build-name-str #'zuul-anonymize-build-name)
  (toggle-frame-tab-bar)
#+end_src
