From patchwork Fri Jan 26 17:25:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Ludovic_Court=C3=A8s?= X-Patchwork-Id: 59441 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id E556A27BBE9; Fri, 26 Jan 2024 17:26:05 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-3.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL, SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 8349227BBE2 for ; Fri, 26 Jan 2024 17:26:04 +0000 (GMT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rTPxo-0005g9-Eu; Fri, 26 Jan 2024 12:26:00 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rTPxm-0005fa-8l for guix-patches@gnu.org; Fri, 26 Jan 2024 12:25:58 -0500 Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rTPxm-00006t-03; Fri, 26 Jan 2024 12:25:58 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rTPxq-0004gs-DK; Fri, 26 Jan 2024 12:26:02 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#68741] [PATCH 2/6] swh: Add bindings for the =?utf-8?b?4oCc?= =?utf-8?b?RXh0SUTigJ0=?= API. Resent-From: Ludovic =?utf-8?q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix@cbaines.net, dev@jpoiret.xyz, ludo@gnu.org, othacehe@gnu.org, rekado@elephly.net, zimon.toutoune@gmail.com, me@tobias.gr, guix-patches@gnu.org Resent-Date: Fri, 26 Jan 2024 17:26:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 68741 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 68741@debbugs.gnu.org Cc: Ludovic =?utf-8?q?Court=C3=A8s?= , Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice X-Debbugs-Original-Xcc: Christopher Baines , Josselin Poiret , Ludovic =?utf-8?q?Court=C3=A8s?= , Mathieu Othacehe , Ricardo Wurmus , Simon Tournier , Tobias Geerinckx-Rice Received: via spool by 68741-submit@debbugs.gnu.org id=B68741.170628994417944 (code B ref 68741); Fri, 26 Jan 2024 17:26:02 +0000 Received: (at 68741) by debbugs.gnu.org; 26 Jan 2024 17:25:44 +0000 Received: from localhost ([127.0.0.1]:52648 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rTPxX-0004fF-DP for submit@debbugs.gnu.org; Fri, 26 Jan 2024 12:25:44 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:47870) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rTPxU-0004ef-PJ for 68741@debbugs.gnu.org; Fri, 26 Jan 2024 12:25:41 -0500 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rTPxI-0008SK-Sh; Fri, 26 Jan 2024 12:25:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=uZJgLjo/h8ho0gAmTFFlAg5ftkZjinmccurE++XcOSg=; b=JiPV43iiFgewA/0HUG0j mJkfRGCTRJG9iyqwtdis3E1uYf79QiID3ItBdPP+es31JVmkveMvllg1IrrLg9RWLwe9b4PpPvMrc yY77oCXlUAX6JRhhR6flpQkLTi7XKJFx6nbpkUWTK8MDc2A7zGngeIFMNOIZCLMPzJQRPOPYMCVZx bv8z6Mynf3W6ckw5AQFAs0q5hsoi2Id0HUJnJJZi6lgaXSe6tMWuiJC51UkwpT3GidRF4KanXOj0g h1fgvE5AMMqPVmCJOx61Sq+ZNVDpG2JEKVlv810ewZeIY/8nqNe5n6eGjfnQo+Ya/Zm86/YFfRcRT iG85q0QRiFQXCg==; From: Ludovic =?utf-8?q?Court=C3=A8s?= Date: Fri, 26 Jan 2024 18:25:02 +0100 Message-ID: <848b0eb1d2ee9d7a31940c9e1867b8decde6ae3f.1706287537.git.ludo@gnu.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: References: MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org X-getmail-retrieved-from-mailbox: Patches This interface was deployed at archive.softwareheritage.org a few days ago. Our main use case will be looking up directories by “nar-sha256” hashes. * guix/swh.scm (): New JSON-mapped record type. (lookup-external-id, lookup-directory-by-nar-hash): New procedures. * tests/swh.scm (%external-id): New variable. ("lookup-directory-by-nar-hash"): New test. Change-Id: Ib671c7798aeb6f8132ac78f2b06b9285da8e7bd5 --- guix/swh.scm | 35 +++++++++++++++++++++++++++++++++++ tests/swh.scm | 21 ++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/guix/swh.scm b/guix/swh.scm index 4e71bdb045..60e97c6d38 100644 --- a/guix/swh.scm +++ b/guix/swh.scm @@ -78,6 +78,14 @@ (define-module (guix swh) lookup-revision lookup-origin-revision + external-id? + external-id-value + external-id-type + external-id-version + external-id-target + lookup-external-id + lookup-directory-by-nar-hash + content? content-checksums content-data-url @@ -382,6 +390,15 @@ (define-json-mapping make-directory-entry directory-entry? (permissions directory-entry-permissions "perms") (target-url directory-entry-target-url "target_url")) +;; +(define-json-mapping make-external-id external-id? + json->external-id + (value external-id-value "extid") + (type external-id-type "extid_type") + (version external-id-version "extid_version") + (target external-id-target) + (target-url external-id-target-url "target_url")) + ;; (define-json-mapping make-save-reply save-reply? json->save-reply @@ -436,6 +453,24 @@ (define (json->directory-entries port) (map json->directory-entry (vector->list (json->scm port)))) +(define (lookup-external-id type id) + "Return the external ID record for ID, a bytevector, of the given TYPE +(currently one of: \"bzr-nodeid\", \"hg-nodeid\", \"nar-sha256\", +\"checksum-sha512\")." + (call (swh-url "/api/1/extid" type + (string-append "hex:" (bytevector->base16-string id))) + json->external-id)) + +(define* (lookup-directory-by-nar-hash hash #:optional (algorithm 'sha256)) + "Return the SWHID of a directory---i.e., prefixed by \"swh:1:dir\"---for the +directory that with the given HASH (a bytevector), assuming nar serialization +and use of ALGORITHM." + ;; example: + ;; https://archive.softwareheritage.org/api/1/extid/nar-sha256/base64url:0jD6Z4TLMm5g1CviuNNuVNP31KWyoT_oevfr8TQwc3Y/ + (and=> (lookup-external-id (string-append "nar-" (symbol->string algorithm)) + hash) + external-id-target)) + (define (origin-visits origin) "Return the list of visits of ORIGIN, a record as returned by 'lookup-origin'." diff --git a/tests/swh.scm b/tests/swh.scm index a36f951241..e7ced6b50c 100644 --- a/tests/swh.scm +++ b/tests/swh.scm @@ -1,5 +1,5 @@ ;;; GNU Guix --- Functional package management for GNU -;;; Copyright © 2019, 2020, 2021 Ludovic Courtès +;;; Copyright © 2019-2021, 2024 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -18,6 +18,7 @@ (define-module (test-swh) #:use-module (guix swh) + #:use-module (guix base32) #:use-module (guix tests http) #:use-module (web response) #:use-module (srfi srfi-19) @@ -56,6 +57,16 @@ (define %directory-entries \"length\": 456, \"dir_id\": 2 } ]") +(define %external-id + "{ \"extid_type\": \"nar-sha256\", + \"extid\": +\"0b56ba94c2b83b8f74e3772887c1109135802eb3e8962b628377987fe97e1e63\", + \"version\": 0, + \"target\": \"swh:1:dir:84a8b34591712c0a90bab0af604188bcd1fe3153\", + \"target_url\": +\"https://archive.softwareheritage.org/swh:1:dir:84a8b34591712c0a90bab0af604188bcd1fe3153\" + }") + (define-syntax-rule (with-json-result str exp ...) (with-http-server `((200 ,str)) (parameterize ((%swh-base-url (%local-url))) @@ -98,6 +109,14 @@ (define-syntax-rule (with-json-result str exp ...) (directory-entry-length entry))) (lookup-directory "123")))) +(test-equal "lookup-directory-by-nar-hash" + "swh:1:dir:84a8b34591712c0a90bab0af604188bcd1fe3153" + (with-json-result %external-id + (lookup-directory-by-nar-hash + (nix-base32-string->bytevector + "0qqygvlpz63phdi2p5p8ncp80dci230qfa3pwds8yfxqqaablmhb") + 'sha256))) + (test-equal "rate limit reached" 3000000000 (let ((too-many (build-response