Skip to content

Commit

Permalink
Actually fixed github tags this time
Browse files Browse the repository at this point in the history
  • Loading branch information
DataDrake committed Aug 5, 2018
1 parent 07b382c commit f13b990
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 65 deletions.
21 changes: 10 additions & 11 deletions providers/cpan/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func (c Provider) Match(query string) string {
filename := sms[len(sms)-1]
pieces := strings.Split(filename, "-")
pieces = pieces[0 : len(sms)-2]
name := pieces[0]
if len(pieces) > 2 {
name = strings.Join(pieces, "-")
}
name := pieces[0]
if len(pieces) > 2 {
name = strings.Join(pieces, "-")
}
return name
}

Expand All @@ -60,9 +60,8 @@ func (c Provider) Name() string {
return "CPAN"
}


type CPANRelease struct {
Module string `json:"main_module"`
Module string `json:"main_module"`
}

func nameToModule(name string) (module string, s results.Status) {
Expand Down Expand Up @@ -93,17 +92,17 @@ func nameToModule(name string) (module string, s results.Status) {
if err != nil {
panic(err.Error())
}
module = r.Module
return
module = r.Module
return
}

// Latest finds the newest release for a CPAN package
func (c Provider) Latest(name string) (r *results.Result, s results.Status) {
// Query the APIDownloadURL
module, s := nameToModule(name)
if s != results.OK {
return
}
if s != results.OK {
return
}
resp, err := http.Get(fmt.Sprintf(APIDownloadURL, module))
if err != nil {
panic(err.Error())
Expand Down
44 changes: 2 additions & 42 deletions providers/github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
// APIReleases is a format string for the Releases API
APIReleases = "https://api.github.com/repos/%s/releases"
// APITags is a format string for the Tags API
APITags = "https://api.github.com/repos/%s/tags"
APITags = "https://api.github.com/repos/%s/git/refs/tags"
// SourceFormat is the format string for Github release tarballs
SourceFormat = "https://github.com/%s/archive/%s.tar.gz"
)
Expand Down Expand Up @@ -67,7 +67,7 @@ func (c Provider) Latest(name string) (r *results.Result, s results.Status) {
if s != results.OK || rs.Empty() {
return
}
r = rs.First()
r = rs.Last()
return
default:
s = results.Unavailable
Expand Down Expand Up @@ -102,46 +102,6 @@ func (c Provider) Name() string {
return "GitHub"
}

func getTags(name string) (rs *results.ResultSet, s results.Status) {
// Query the API
req, _ := http.NewRequest("GET", fmt.Sprintf(APITags, name), nil)
if key := config.Global.Github.Key; len(key) > 0 {
req.Header["Authorization"] = []string{"token " + key}
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err.Error())
}
defer resp.Body.Close()
// Translate Status Code
switch resp.StatusCode {
case 200:
s = results.OK
case 404:
s = results.NotFound
default:
s = results.Unavailable
}

// Fail if not OK
if s != results.OK {
return
}

dec := json.NewDecoder(resp.Body)
tags := make(Tags, 0)
err = dec.Decode(&tags)
if err != nil {
panic(err.Error())
}
if len(tags) == 0 {
s = results.NotFound
return
}
rs = tags.Convert(name)
return
}

// Releases finds all matching releases for a github package
func (c Provider) Releases(name string) (rs *results.ResultSet, s results.Status) {
// Query the API
Expand Down
127 changes: 123 additions & 4 deletions providers/github/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,72 @@
package github

import (
"encoding/json"
"fmt"
"github.com/DataDrake/cuppa/config"
"github.com/DataDrake/cuppa/results"
"net/http"
"sort"
"strings"
"time"
)

// Ref is a representation of a Github Git Reference
type Ref struct {
Object struct {
URL string `json:"url"`
} `json:"object"`
}

// Refs is a list of Refs
type Refs []Ref

// ToTags converts Refs to Tags
func (rs Refs) ToTags() Tags {
tags := make(Tags, 0)
for _, ref := range rs {
if len(ref.Object.URL) == 0 {
continue
}
req, _ := http.NewRequest("GET", ref.Object.URL, nil)
if key := config.Global.Github.Key; len(key) > 0 {
req.Header["Authorization"] = []string{"token " + key}
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err.Error())
}
if resp.StatusCode != 200 {
resp.Body.Close()
continue
}
dec := json.NewDecoder(resp.Body)
tag := Tag{}
err = dec.Decode(&tag)
if err != nil {
panic(err.Error())
continue
}
resp.Body.Close()
tags = append(tags, tag)
}
return tags
}

// Tag is a JSON representation of a Github tag
type Tag struct {
Name string `json:"name"`
Tag string `json:"tag"`
Tagger struct {
Date string `json:"date"`
} `json:"tagger"`
}

// Convert turns a Github tag into a Cuppa result
func (t *Tag) Convert(name string) *results.Result {
r := &results.Result{}
pieces := strings.Split(name, "/")
r.Name = pieces[len(pieces)-1]
pieces = strings.Split(t.Name, "/")
pieces = strings.Split(t.Tag, "/")
r.Version = pieces[len(pieces)-1]
r.Location = fmt.Sprintf(SourceFormat, name, r.Version)
return r
Expand All @@ -41,10 +91,79 @@ func (t *Tag) Convert(name string) *results.Result {
// Tags are a JSON representation of one or more tags
type Tags []Tag

// Len gets the length of Tags
func (ts Tags) Len() int {
return len(ts)
}

// Swap is used by Sort
func (ts Tags) Swap(i, j int) {
ts[i], ts[j] = ts[j], ts[i]
}

// Less is used by Sort
func (ts Tags) Less(i, j int) bool {
d1, err := time.Parse(time.RFC3339, ts[i].Tagger.Date)
if err != nil {
return true
}
d2, err := time.Parse(time.RFC3339, ts[j].Tagger.Date)
if err != nil {
return false
}
return d1.Before(d2)
}

func getTags(name string) (rs *results.ResultSet, s results.Status) {
// Query the API
req, _ := http.NewRequest("GET", fmt.Sprintf(APITags, name), nil)
if key := config.Global.Github.Key; len(key) > 0 {
req.Header["Authorization"] = []string{"token " + key}
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err.Error())
}
defer resp.Body.Close()
// Translate Status Code
switch resp.StatusCode {
case 200:
s = results.OK
case 404:
s = results.NotFound
default:
s = results.Unavailable
}

// Fail if not OK
if s != results.OK {
return
}

dec := json.NewDecoder(resp.Body)
refs := make(Refs, 0)
err = dec.Decode(&refs)
if err != nil {
panic(err.Error())
}
if len(refs) == 0 {
s = results.NotFound
return
}
tags := refs.ToTags()
if len(tags) == 0 {
s = results.NotFound
return
}
rs = tags.Convert(name)
return
}

// Convert a Github tagset to a Cuppa result set
func (ts *Tags) Convert(name string) *results.ResultSet {
func (ts Tags) Convert(name string) *results.ResultSet {
sort.Sort(ts)
rs := results.NewResultSet(name)
for _, t := range *ts {
for _, t := range ts {
r := t.Convert(name)
if r != nil {
rs.AddResult(r)
Expand Down
16 changes: 8 additions & 8 deletions results/resultset.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ func (rs *ResultSet) First() *Result {

// Last retrieves the first result from a query
func (rs *ResultSet) Last() *Result {
switch len(rs.results) {
case 0:
return nil
case 1:
return rs.results[0]
default:
return rs.results[len(rs.results)-1]
}
switch len(rs.results) {
case 0:
return nil
case 1:
return rs.results[0]
default:
return rs.results[len(rs.results)-1]
}
}

// PrintAll pretty-prints an entire ResultSet
Expand Down

0 comments on commit f13b990

Please sign in to comment.