2015-11-15 01:18:05 +01:00
|
|
|
;;; GNU Guix --- Functional package management for GNU
|
|
|
|
;;; Copyright © 2016 Ben Woodcroft <donttrustben@gmail.com>
|
2020-07-03 22:48:04 +02:00
|
|
|
;;; Copyright © 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
|
2018-12-20 00:02:38 +01:00
|
|
|
;;; Copyright © 2018 Eric Bavier <bavier@member.fsf.org>
|
2019-01-16 10:51:06 +01:00
|
|
|
;;; Copyright © 2019 Arun Isaac <arunisaac@systemreboot.net>
|
2019-09-17 20:50:36 +02:00
|
|
|
;;; Copyright © 2019 Efraim Flashner <efraim@flashner.co.il>
|
2015-11-15 01:18:05 +01:00
|
|
|
;;;
|
|
|
|
;;; This file is part of GNU Guix.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is free software; you can redistribute it and/or modify it
|
|
|
|
;;; under the terms of the GNU General Public License as published by
|
|
|
|
;;; the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
;;; your option) any later version.
|
|
|
|
;;;
|
|
|
|
;;; GNU Guix is distributed in the hope that it will be useful, but
|
|
|
|
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;;; GNU General Public License for more details.
|
|
|
|
;;;
|
|
|
|
;;; You should have received a copy of the GNU General Public License
|
|
|
|
;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
(define-module (guix import github)
|
|
|
|
#:use-module (ice-9 match)
|
|
|
|
#:use-module (srfi srfi-1)
|
2017-10-27 23:38:45 +02:00
|
|
|
#:use-module (srfi srfi-26)
|
2017-01-13 18:14:19 +01:00
|
|
|
#:use-module (srfi srfi-34)
|
2015-11-15 01:18:05 +01:00
|
|
|
#:use-module (guix utils)
|
2020-07-24 12:04:22 +02:00
|
|
|
#:use-module (guix i18n)
|
|
|
|
#:use-module (guix diagnostics)
|
2015-11-15 01:18:05 +01:00
|
|
|
#:use-module ((guix download) #:prefix download:)
|
2018-12-20 00:02:38 +01:00
|
|
|
#:use-module ((guix git-download) #:prefix download:)
|
2015-11-15 01:18:05 +01:00
|
|
|
#:use-module (guix import utils)
|
2018-06-10 20:35:39 +02:00
|
|
|
#:use-module (guix import json)
|
2020-07-24 12:04:22 +02:00
|
|
|
#:use-module (json)
|
2015-11-15 01:18:05 +01:00
|
|
|
#:use-module (guix packages)
|
|
|
|
#:use-module (guix upstream)
|
2017-01-13 18:14:19 +01:00
|
|
|
#:use-module (guix http-client)
|
2015-11-15 01:18:05 +01:00
|
|
|
#:use-module (web uri)
|
|
|
|
#:export (%github-updater))
|
|
|
|
|
|
|
|
(define (find-extension url)
|
|
|
|
"Return the extension of the archive e.g. '.tar.gz' given a URL, or
|
|
|
|
false if none is recognized"
|
2016-05-31 22:11:51 +02:00
|
|
|
(find (lambda (x) (string-suffix? x url))
|
2017-01-27 23:49:52 +01:00
|
|
|
(list ".tar.gz" ".tar.bz2" ".tar.xz" ".zip" ".tar"
|
|
|
|
".tgz" ".tbz" ".love")))
|
2015-11-15 01:18:05 +01:00
|
|
|
|
|
|
|
(define (updated-github-url old-package new-version)
|
|
|
|
;; Return a url for the OLD-PACKAGE with NEW-VERSION. If no source url in
|
|
|
|
;; the OLD-PACKAGE is a GitHub url, then return false.
|
|
|
|
|
|
|
|
(define (updated-url url)
|
|
|
|
(if (string-prefix? "https://github.com/" url)
|
2017-01-27 23:49:52 +01:00
|
|
|
(let ((ext (or (find-extension url) ""))
|
2019-08-26 21:34:40 +02:00
|
|
|
(name (package-upstream-name old-package))
|
2015-11-15 01:18:05 +01:00
|
|
|
(version (package-version old-package))
|
|
|
|
(prefix (string-append "https://github.com/"
|
|
|
|
(github-user-slash-repository url)))
|
|
|
|
(repo (github-repository url)))
|
|
|
|
(cond
|
|
|
|
((string-suffix? (string-append "/tarball/v" version) url)
|
|
|
|
(string-append prefix "/tarball/v" new-version))
|
|
|
|
((string-suffix? (string-append "/tarball/" version) url)
|
|
|
|
(string-append prefix "/tarball/" new-version))
|
|
|
|
((string-suffix? (string-append "/archive/v" version ext) url)
|
|
|
|
(string-append prefix "/archive/v" new-version ext))
|
|
|
|
((string-suffix? (string-append "/archive/" version ext) url)
|
|
|
|
(string-append prefix "/archive/" new-version ext))
|
|
|
|
((string-suffix? (string-append "/archive/" name "-" version ext)
|
|
|
|
url)
|
|
|
|
(string-append prefix "/archive/" name "-" new-version ext))
|
|
|
|
((string-suffix? (string-append "/releases/download/v" version "/"
|
|
|
|
name "-" version ext)
|
|
|
|
url)
|
|
|
|
(string-append prefix "/releases/download/v" new-version "/" name
|
|
|
|
"-" new-version ext))
|
|
|
|
((string-suffix? (string-append "/releases/download/" version "/"
|
|
|
|
name "-" version ext)
|
|
|
|
url)
|
|
|
|
(string-append prefix "/releases/download/" new-version "/" name
|
|
|
|
"-" new-version ext))
|
|
|
|
((string-suffix? (string-append "/releases/download/" version "/"
|
|
|
|
repo "-" version ext)
|
|
|
|
url)
|
|
|
|
(string-append prefix "/releases/download/" new-version "/" repo
|
|
|
|
"-" new-version ext))
|
|
|
|
((string-suffix? (string-append "/releases/download/" repo "-"
|
|
|
|
version "/" repo "-" version ext)
|
|
|
|
url)
|
|
|
|
(string-append "/releases/download/" repo "-" version "/" repo "-"
|
|
|
|
version ext))
|
|
|
|
(#t #f))) ; Some URLs are not recognised.
|
|
|
|
#f))
|
|
|
|
|
2020-07-03 22:48:04 +02:00
|
|
|
(match (package-source old-package)
|
|
|
|
((? origin? origin)
|
|
|
|
(let ((source-uri (origin-uri origin))
|
|
|
|
(fetch-method (origin-method origin)))
|
|
|
|
(cond
|
|
|
|
((eq? fetch-method download:url-fetch)
|
|
|
|
(match source-uri
|
|
|
|
((? string?)
|
|
|
|
(updated-url source-uri))
|
|
|
|
((source-uri ...)
|
|
|
|
(find updated-url source-uri))))
|
|
|
|
((and (eq? fetch-method download:git-fetch)
|
|
|
|
(string-prefix? "https://github.com/"
|
|
|
|
(download:git-reference-url source-uri)))
|
|
|
|
(download:git-reference-url source-uri))
|
|
|
|
(else #f))))
|
|
|
|
(_ #f)))
|
2015-11-15 01:18:05 +01:00
|
|
|
|
|
|
|
(define (github-package? package)
|
|
|
|
"Return true if PACKAGE is a package from GitHub, else false."
|
2018-12-20 00:02:38 +01:00
|
|
|
(->bool (updated-github-url package "dummy")))
|
2015-11-15 01:18:05 +01:00
|
|
|
|
|
|
|
(define (github-repository url)
|
|
|
|
"Return a string e.g. bedtools2 of the name of the repository, from a string
|
|
|
|
URL of the form 'https://github.com/arq5x/bedtools2/archive/v2.24.0.tar.gz'"
|
|
|
|
(match (string-split (uri-path (string->uri url)) #\/)
|
|
|
|
((_ owner project . rest)
|
2018-12-20 00:02:38 +01:00
|
|
|
(string-append (basename project ".git")))))
|
2015-11-15 01:18:05 +01:00
|
|
|
|
|
|
|
(define (github-user-slash-repository url)
|
|
|
|
"Return a string e.g. arq5x/bedtools2 of the owner and the name of the
|
|
|
|
repository separated by a forward slash, from a string URL of the form
|
|
|
|
'https://github.com/arq5x/bedtools2/archive/v2.24.0.tar.gz'"
|
|
|
|
(match (string-split (uri-path (string->uri url)) #\/)
|
|
|
|
((_ owner project . rest)
|
2018-12-20 00:02:38 +01:00
|
|
|
(string-append owner "/" (basename project ".git")))))
|
2015-11-15 01:18:05 +01:00
|
|
|
|
|
|
|
(define %github-token
|
|
|
|
;; Token to be passed to Github.com to avoid the 60-request per hour
|
|
|
|
;; limit, or #f.
|
|
|
|
(make-parameter (getenv "GUIX_GITHUB_TOKEN")))
|
|
|
|
|
2018-08-20 15:29:43 +02:00
|
|
|
(define (fetch-releases-or-tags url)
|
|
|
|
"Fetch the list of \"releases\" or, if it's empty, the list of tags for the
|
2019-07-21 23:05:54 +02:00
|
|
|
repository at URL. Return the corresponding JSON dictionaries (alists),
|
2018-08-20 15:29:43 +02:00
|
|
|
or #f if the information could not be retrieved.
|
|
|
|
|
|
|
|
We look at both /releases and /tags because the \"release\" feature of GitHub
|
|
|
|
is little used; often, people simply provide a tag. What's confusing is that
|
|
|
|
tags show up in the \"Releases\" tab of the web UI. For instance,
|
|
|
|
'https://github.com/aconchillo/guile-json/releases' shows a number of
|
|
|
|
\"releases\" (really: tags), whereas
|
|
|
|
'https://api.github.com/repos/aconchillo/guile-json/releases' returns the
|
|
|
|
empty list."
|
|
|
|
(define release-url
|
|
|
|
(string-append "https://api.github.com/repos/"
|
|
|
|
(github-user-slash-repository url)
|
|
|
|
"/releases"))
|
|
|
|
(define tag-url
|
|
|
|
(string-append "https://api.github.com/repos/"
|
|
|
|
(github-user-slash-repository url)
|
|
|
|
"/tags"))
|
|
|
|
|
|
|
|
(define headers
|
|
|
|
;; Ask for version 3 of the API as suggested at
|
|
|
|
;; <https://developer.github.com/v3/>.
|
|
|
|
`((Accept . "application/vnd.github.v3+json")
|
2020-02-22 11:48:29 +01:00
|
|
|
(user-agent . "GNU Guile")
|
|
|
|
,@(if (%github-token)
|
|
|
|
`((Authorization . ,(string-append "token " (%github-token))))
|
|
|
|
'())))
|
2018-08-20 15:29:43 +02:00
|
|
|
|
2020-07-24 12:04:22 +02:00
|
|
|
(guard (c ((and (http-get-error? c)
|
|
|
|
(= 404 (http-get-error-code c)))
|
|
|
|
(warning (G_ "~a is unreachable (~a)~%")
|
|
|
|
release-url (http-get-error-code c))
|
|
|
|
'#())) ;return an empty release set
|
|
|
|
(let* ((port (http-fetch release-url #:headers headers))
|
|
|
|
(result (json->scm port)))
|
|
|
|
(close-port port)
|
|
|
|
(match result
|
|
|
|
(#()
|
|
|
|
;; We got the empty list, presumably because the user didn't use GitHub's
|
|
|
|
;; "release" mechanism, but hopefully they did use Git tags.
|
|
|
|
(json-fetch tag-url #:headers headers))
|
|
|
|
(x x)))))
|
2018-08-20 15:29:43 +02:00
|
|
|
|
2015-11-15 01:18:05 +01:00
|
|
|
(define (latest-released-version url package-name)
|
|
|
|
"Return a string of the newest released version name given a string URL like
|
|
|
|
'https://github.com/arq5x/bedtools2/archive/v2.24.0.tar.gz' and the name of
|
|
|
|
the package e.g. 'bedtools2'. Return #f if there is no releases"
|
2019-01-16 21:04:07 +01:00
|
|
|
(define (pre-release? x)
|
2019-07-21 23:05:54 +02:00
|
|
|
(assoc-ref x "prerelease"))
|
2019-01-16 21:04:07 +01:00
|
|
|
|
2019-05-14 12:14:46 +02:00
|
|
|
(define (release->version release)
|
2019-07-21 23:05:54 +02:00
|
|
|
(let ((tag (or (assoc-ref release "tag_name") ;a "release"
|
|
|
|
(assoc-ref release "name"))) ;a tag
|
2019-05-14 12:14:46 +02:00
|
|
|
(name-length (string-length package-name)))
|
|
|
|
(cond
|
|
|
|
;; some tags include the name of the package e.g. "fdupes-1.51"
|
|
|
|
;; so remove these
|
|
|
|
((and (< name-length (string-length tag))
|
|
|
|
(string=? (string-append package-name "-")
|
|
|
|
(substring tag 0 (+ name-length 1))))
|
|
|
|
(substring tag (+ name-length 1)))
|
|
|
|
;; some tags start with a "v" e.g. "v0.25.0"
|
2019-09-17 20:50:36 +02:00
|
|
|
;; or with the word "version" e.g. "version.2.1"
|
2019-05-14 12:14:46 +02:00
|
|
|
;; where some are just the version number
|
2019-09-17 20:50:36 +02:00
|
|
|
((string-prefix? "version" tag)
|
|
|
|
(if (char-set-contains? char-set:digit (string-ref tag 7))
|
|
|
|
(substring tag 7)
|
|
|
|
(substring tag 8)))
|
2019-05-14 12:14:46 +02:00
|
|
|
((string-prefix? "v" tag)
|
|
|
|
(substring tag 1))
|
|
|
|
;; Finally, reject tags that don't start with a digit:
|
|
|
|
;; they may not represent a release.
|
|
|
|
((and (not (string-null? tag))
|
|
|
|
(char-set-contains? char-set:digit
|
|
|
|
(string-ref tag 0)))
|
|
|
|
tag)
|
|
|
|
(else #f))))
|
|
|
|
|
2019-07-21 23:05:54 +02:00
|
|
|
(let* ((json (and=> (fetch-releases-or-tags url)
|
|
|
|
vector->list)))
|
2015-11-15 01:18:05 +01:00
|
|
|
(if (eq? json #f)
|
2018-08-20 15:29:43 +02:00
|
|
|
(if (%github-token)
|
2015-11-15 01:18:05 +01:00
|
|
|
(error "Error downloading release information through the GitHub
|
|
|
|
API when using a GitHub token")
|
|
|
|
(error "Error downloading release information through the GitHub
|
|
|
|
API. This may be fixed by using an access token and setting the environment
|
|
|
|
variable GUIX_GITHUB_TOKEN, for instance one procured from
|
|
|
|
https://github.com/settings/tokens"))
|
2019-05-14 12:16:19 +02:00
|
|
|
(match (sort (filter-map release->version
|
|
|
|
(match (remove pre-release? json)
|
|
|
|
(() json) ; keep everything
|
|
|
|
(releases releases)))
|
|
|
|
version>?)
|
|
|
|
((latest-release . _) latest-release)
|
|
|
|
(() #f)))))
|
2015-11-15 01:18:05 +01:00
|
|
|
|
2016-04-14 21:40:20 +02:00
|
|
|
(define (latest-release pkg)
|
|
|
|
"Return an <upstream-source> for the latest release of PKG."
|
2017-10-27 23:38:45 +02:00
|
|
|
(define (origin-github-uri origin)
|
|
|
|
(match (origin-uri origin)
|
|
|
|
((? string? url)
|
|
|
|
url) ;surely a github.com URL
|
2018-12-20 00:02:38 +01:00
|
|
|
((? download:git-reference? ref)
|
|
|
|
(download:git-reference-url ref))
|
2017-10-27 23:38:45 +02:00
|
|
|
((urls ...)
|
|
|
|
(find (cut string-contains <> "github.com") urls))))
|
|
|
|
|
|
|
|
(let* ((source-uri (origin-github-uri (package-source pkg)))
|
2015-11-15 01:18:05 +01:00
|
|
|
(name (package-name pkg))
|
|
|
|
(newest-version (latest-released-version source-uri name)))
|
|
|
|
(if newest-version
|
|
|
|
(upstream-source
|
2016-04-14 21:40:20 +02:00
|
|
|
(package name)
|
2015-11-15 01:18:05 +01:00
|
|
|
(version newest-version)
|
|
|
|
(urls (list (updated-github-url pkg newest-version))))
|
|
|
|
#f))) ; On GitHub but no proper releases
|
|
|
|
|
|
|
|
(define %github-updater
|
|
|
|
(upstream-updater
|
|
|
|
(name 'github)
|
|
|
|
(description "Updater for GitHub packages")
|
|
|
|
(pred github-package?)
|
|
|
|
(latest latest-release)))
|
|
|
|
|
|
|
|
|