Skip to content

Commit

Permalink
Merge pull request #29 from z3ntu/debian
Browse files Browse the repository at this point in the history
Add support for debian.org
  • Loading branch information
simon04 authored Oct 16, 2018
2 parents 499b04f + ccd50a9 commit 4f26c94
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 0 deletions.
36 changes: 36 additions & 0 deletions upstream/debian.go
Original file line number Diff line number Diff line change
@@ -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
}
68 changes: 68 additions & 0 deletions upstream/debian_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
6 changes: 6 additions & 0 deletions upstream/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 4f26c94

Please sign in to comment.