diff --git a/upstream/debian.go b/upstream/debian.go new file mode 100644 index 0000000..1be5a23 --- /dev/null +++ b/upstream/debian.go @@ -0,0 +1,36 @@ +package upstream + +import ( + "fmt" + "net/url" + "regexp" + + "github.com/go-errors/errors" +) + +type debianVersion struct { + Version string `json:"version"` +} + +type debianResponse struct { + Versions []debianVersion `json:"versions"` +} + +type debian string + +func (d debian) releasesURL() string { + // API documentation: https://sources.debian.org/doc/api/ + return fmt.Sprintf("https://sources.debian.org/api/src/%s/", url.PathEscape(string(d))) +} + +func (d debian) latestVersion() (Version, error) { + var res debianResponse + if err := fetchJSON(d, &res); err != nil { + return "", errors.WrapPrefix(err, "No debian release found for "+string(d), 0) + } + match := regexp.MustCompile("([^-]+)[-|~]").FindStringSubmatch(res.Versions[0].Version) + if len(match) > 0 { + return Version(match[1]), nil + } + return Version(res.Versions[0].Version), nil +} diff --git a/upstream/debian_test.go b/upstream/debian_test.go new file mode 100644 index 0000000..76da71c --- /dev/null +++ b/upstream/debian_test.go @@ -0,0 +1,68 @@ +package upstream + +import ( + "net/http" + "testing" + + "github.com/h2non/gock" + "github.com/simon04/aur-out-of-date/pkg" +) + +func mockDebian() *gock.Response { + return gock.New("https://sources.debian.org"). + Get("/api/src/babeltrace/"). + Reply(http.StatusOK). + SetHeader("Content-Type", "application/json"). + BodyString(` + { + "package": "babeltrace", + "path": "babeltrace", + "pathl": [ + [ + "babeltrace", + "/src/babeltrace/" + ] + ], + "suite": "", + "type": "package", + "versions": [ + { + "area": "main", + "suites": [ + "buster", + "sid" + ], + "version": "1.5.6-1" + }, + { + "area": "main", + "suites": [ + "stretch" + ], + "version": "1.5.1-1" + }, + { + "area": "main", + "suites": [ + "jessie", + "jessie-kfreebsd" + ], + "version": "1.2.3-2" + } + ] + }`) +} + +func TestDebianSource1(t *testing.T) { + defer gock.Off() + mockDebian() + + p := pkg.New("babeltrace", "0", "", "http://debian.backend.mirrors.debian.org/debian/pool/main/b/babeltrace/python3-babeltrace_1.5.6-1_hurd-i386.deb") + version, err := VersionForPkg(p) + if err != nil { + t.Error(err) + } + if version != "1.5.6" { + t.Errorf("Expecting version 1.5.6, but got %v", version) + } +} diff --git a/upstream/version.go b/upstream/version.go index 831bf6e..40892fc 100644 --- a/upstream/version.go +++ b/upstream/version.go @@ -81,6 +81,12 @@ func forURL(url string) (Version, error) { if len(match) > 0 { return gitLab{match[1], match[2], match[3]}.latestVersion() } + case strings.Contains(url, "debian.org"): + // Example: http://ftp.debian.org/debian/pool/main/p/python3-defaults/python3-defaults_3.6.6-1.tar.gz + match := regexp.MustCompile("/debian/pool/(?:contrib|main|non-free)/[a-z]{1,4}/([^/#.]+)/[^/#]+(?:.tar|.deb)").FindStringSubmatch(url) + if len(match) > 0 { + return debian(match[1]).latestVersion() + } } return "", errors.Errorf("No release found for %s", url) }