Re: [PATCH qa-frontpage WIP] Add a library to parse patchwork json data

2023-09-25 Thread Christopher Baines

Vivien Kraus  writes:

> Hi!
>
> Here is a small library that exports 3 types:
> −  is the collection of metadata that is present
>   in the square brackets in the patch names;
> −  is an individual item of the patch series;
> −  is a whole series of patches;
>
> And a set of functions to parse and serialize these.
>
> A fun experiment is to run the following script:
>
> (use-modules (guix-qa-frontpage patchwork patch-series))
> (use-modules (rnrs bytevectors))
> (use-modules (web client))
> (use-modules (ice-9 receive))
> (use-modules (json))
>
> (define patchwork-data
>   (receive (r body)
>   (http-get 
> "https://patches.guix-patches.cbaines.net/api/patches/?order=-id";)
> (json-string->scm (utf8->string body
>
> (define patchwork-series
>   (map scm->patch-series (vector->list patchwork-data)))
>
> (for-each
>  (lambda (correct-series)
>(display correct-series)
>(newline))
>  (map patch-series->scm patchwork-series))
>
> You will see that patchwork has quite a lot of creativity when it
> comes to breaking my expectations. I made sure to add as much
> information in exceptions so that we can understand what is happening.

This looks good, but would it be possible to adapt this to work with the
series endpoint [1], rather than the patches one?

1: https://patches.guix-patches.cbaines.net/api/series/?order=-id

I think the use of the patches endpoint currently is just because
previously the Patchwork checks information was used. Now that Patchwork
checks aren't used, I think the series endpoint can be used instead, and
this should map much more closely to the data structures you're trying
to construct.

Thanks,

Chris


signature.asc
Description: PGP signature


[PATCH qa-frontpage WIP] Add a library to parse patchwork json data

2023-09-19 Thread Vivien Kraus
---
Hi!

Here is a small library that exports 3 types:
−  is the collection of metadata that is present
  in the square brackets in the patch names;
−  is an individual item of the patch series;
−  is a whole series of patches;

And a set of functions to parse and serialize these.

A fun experiment is to run the following script:

(use-modules (guix-qa-frontpage patchwork patch-series))
(use-modules (rnrs bytevectors))
(use-modules (web client))
(use-modules (ice-9 receive))
(use-modules (json))

(define patchwork-data
  (receive (r body)
  (http-get 
"https://patches.guix-patches.cbaines.net/api/patches/?order=-id";)
(json-string->scm (utf8->string body

(define patchwork-series
  (map scm->patch-series (vector->list patchwork-data)))

(for-each
 (lambda (correct-series)
   (display correct-series)
   (newline))
 (map patch-series->scm patchwork-series))

You will see that patchwork has quite a lot of creativity when it
comes to breaking my expectations. I made sure to add as much
information in exceptions so that we can understand what is happening.

Best regards,

Vivien

 Makefile.am  |   3 +
 guix-qa-frontpage/patchwork/patch-name.scm   | 117 +
 guix-qa-frontpage/patchwork/patch-series.scm | 165 +++
 guix-qa-frontpage/patchwork/patch.scm|  93 +++
 4 files changed, 378 insertions(+)
 create mode 100644 guix-qa-frontpage/patchwork/patch-name.scm
 create mode 100644 guix-qa-frontpage/patchwork/patch-series.scm
 create mode 100644 guix-qa-frontpage/patchwork/patch.scm

diff --git a/Makefile.am b/Makefile.am
index 79b7032..7b00ea9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -32,6 +32,9 @@ SOURCES = 
\
   guix-qa-frontpage/server.scm 
\
   guix-qa-frontpage/database.scm   
\
   guix-qa-frontpage/patchwork.scm  
\
+  guix-qa-frontpage/patchwork/patch-name.scm   
\
+  guix-qa-frontpage/patchwork/patch.scm
\
+  guix-qa-frontpage/patchwork/patch-series.scm 
\
   guix-qa-frontpage/guix-data-service.scm  
\
   guix-qa-frontpage/branch.scm 
\
   guix-qa-frontpage/issue.scm  
\
diff --git a/guix-qa-frontpage/patchwork/patch-name.scm 
b/guix-qa-frontpage/patchwork/patch-name.scm
new file mode 100644
index 000..1b4cd97
--- /dev/null
+++ b/guix-qa-frontpage/patchwork/patch-name.scm
@@ -0,0 +1,117 @@
+(define-module (guix-qa-frontpage patchwork patch-name)
+  #:use-module (srfi srfi-9)
+  #:use-module (srfi srfi-9 gnu)
+  #:use-module (ice-9 match)
+  #:use-module (ice-9 exceptions)
+  #:export (
+make-patch-name-metadata
+patch-name-metadata?
+patch-name-metadata-bug-number
+patch-name-metadata-feature-branch
+patch-name-metadata-revision
+patch-name-metadata-index
+patch-name-metadata-total
+patch-name-metadata-set-index
+
+&patch-name-parser-error
+patch-name-parser-error?
+make-patch-name-parser-error
+
+parse-patch-name
+synthesize-patch-name
+))
+
+(define-record-type 
+  (make-patch-name-metadata bug-number feature-branch revision index total)
+  patch-name-metadata?
+  (bug-number patch-name-metadata-bug-number)
+  (feature-branch patch-name-metadata-feature-branch)
+  (revision patch-name-metadata-revision)
+  (index patch-name-metadata-index)
+  (total patch-name-metadata-total))
+
+(define (patch-name-metadata-set-index meta index)
+  (match meta
+(($  bug branch rev _ total)
+ (make-patch-name-metadata bug branch rev index total
+
+(set-record-type-printer!
+ 
+ (lambda (record port)
+   (match record
+ (($  bug feature revision index total)
+  (format port
+  "#< \
+bug-number=~s feature-branch=~s revision=~s \
+index=~s total=~s>"
+  bug feature revision index total)
+
+(define-exception-type &patch-name-parser-error
+  &error
+  make-patch-name-parser-error
+  patch-name-parser-error?)
+
+(define (parse-patch-name name)
+  "Given a patch @var{name} obtained from Patchwork, infer the metadata
+from its name."
+  (define (raise-error message)
+(raise-exception
+ (make-exception
+  (make-error)
+  (make-patch-name-parser-error)
+  (make-exception-with-message message)
+  (make-exception-with-irritants (list name))
+  (make-exception-with-origin 'parse-patch-name
+  (define (as-bug-number arg)
+(and (string-prefix? "bug#" arg)
+ (string->number (substring arg (string-length "bug#")
+  (define (as-revision arg)
+