-
Notifications
You must be signed in to change notification settings - Fork 0
/
github.go
121 lines (94 loc) · 2.08 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"log"
"net/http"
"time"
"github.com/pkg/errors"
)
type GithubProvider struct {
SourceProvider
User string
Token string
}
func NewGithubProvider() *GithubProvider {
return &GithubProvider{
SourceProvider: SourceProvider{
Name: "Github",
Client: &http.Client{
Timeout: 30 * time.Second,
},
},
}
}
func (g *GithubProvider) WithCredentials(user, pass string) {
g.User = user
g.Token = pass
}
func (g *GithubProvider) Fetch(repoName string) (repo Repository) {
const githubURL = "https://api.github.com/"
var (
reply struct {
Name string `json:"full_name"`
Description string `json:"description"`
HTMLURL string `json:"html_url"`
TagURL string `json:"tags_url"`
}
c = make(chan string)
)
client := g.SourceProvider.Client
go func() {
var reply []TagReply
tagsURL := <-c
req, err := http.NewRequest("GET", tagsURL, nil)
if err != nil {
log.Fatal(err)
}
if g.User != "" && g.Token != "" {
req.SetBasicAuth(g.User, g.Token)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&reply)
if err != nil {
log.Fatal(errors.Wrap(err, "failed to parse tags"))
}
if len(reply) >= 1 {
tags := sortMakeVersionTags(reply)
c <- tags[0].Original()
} else {
c <- "None"
}
}()
req, err := http.NewRequest("GET", githubURL+"repos/"+repoName, nil)
if err != nil {
log.Fatal(req)
}
if g.User != "" && g.Token != "" {
req.SetBasicAuth(g.User, g.Token)
}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&reply)
if err != nil {
log.Fatal(errors.Wrap(err, "failed to parse response"))
}
log.Printf("LOG: checking %s \n", reply.Name)
c <- reply.TagURL
// set repo details
repo.Description = reply.Description
repo.URL = reply.HTMLURL
repo.TagURL = reply.TagURL
// set repo version
if tag := <-c; tag != "None" {
repo.Version.Name = reply.Name
repo.Version.Version = tag
}
return
}