forked from justwatchcom/github-releases-notifier
-
Notifications
You must be signed in to change notification settings - Fork 2
/
releasechecker.go
200 lines (166 loc) · 5.23 KB
/
releasechecker.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main
import (
"context"
"errors"
"fmt"
"github.com/magmel48/github-releases-notifier/pkg/models"
"golang.org/x/oauth2"
"net/url"
"strings"
"time"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/shurcooL/graphql"
)
var Github = "github.com"
var Gitlab = "gitlab.com"
var ApiUrl = map[string]string{Github: "https://api.github.com/graphql", Gitlab: "https://gitlab.com/api/graphql"}
// Checker knows about the current repositories releases to compare against.
type Checker struct {
logger log.Logger
tokens map[string]string
releases map[string]models.Repository
}
type QueryResult interface {
GetID() models.ID
GetName() models.String
GetDescription() models.String
GetURL() *url.URL
GetReleasesCount() int
GetLatestReleaseID() models.ID
GetLatestReleaseName() models.String
GetLatestReleaseDescription() models.String
GetLatestReleaseURL() *url.URL
GetLatestReleasePublishingDate() time.Time
}
// Run the queries and comparisons for the given repositories in a given interval.
func (c *Checker) Run(interval time.Duration, repositories []string, releases chan<- models.Repository) {
if c.releases == nil {
c.releases = make(map[string]models.Repository)
}
for {
for _, repoName := range repositories {
s := strings.Split(repoName, "/")
website, owner, name := s[0], s[1], s[2]
var nextRepo models.Repository
var err error
if name != "" {
if website == Github || website == Gitlab {
nextRepo, err = c.query(website, owner, name)
} else {
err = errors.New(website + " is not supported")
}
} else {
err = errors.New("no website specified")
}
if err != nil {
level.Warn(c.logger).Log(
"msg", "failed to query the repository's releases",
"owner", owner,
"name", name,
"err", err,
)
continue
}
currRepo, ok := c.releases[repoName]
// We've queried the repository for the first time.
// Saving the current state to compare with the next iteration.
if !ok {
c.releases[repoName] = nextRepo
continue
}
if nextRepo.Release.PublishedAt.After(currRepo.Release.PublishedAt) {
releases <- nextRepo
c.releases[repoName] = nextRepo
} else {
level.Debug(c.logger).Log(
"msg", "no new release for repository",
"owner", owner,
"name", name,
)
}
}
time.Sleep(interval)
}
}
func (c *Checker) getClient(website string) (*graphql.Client, error) {
if token, ok := c.tokens[website]; ok {
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
client := oauth2.NewClient(context.Background(), tokenSource)
return graphql.NewClient(ApiUrl[website], client), nil
}
return nil, errors.New("no token for " + website + " available")
}
func (c *Checker) queryGithub(client *graphql.Client, variables map[string]interface{}) (QueryResult, error) {
query := models.GithubQuery{}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := client.Query(ctx, &query, variables); err != nil {
return nil, err
}
return query, nil
}
func (c *Checker) queryGitlab(client *graphql.Client, variables map[string]interface{}) (QueryResult, error) {
query := models.GitlabQuery{}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := client.Query(ctx, &query, variables); err != nil {
return nil, err
}
return query, nil
}
func (c *Checker) query(website string, owner string, name string) (models.Repository, error) {
var client *graphql.Client
var queryResult QueryResult
var err error
client, err = c.getClient(website)
if err != nil {
return models.Repository{}, err
}
variables := map[string]interface{}{
"owner": models.String(owner),
"name": models.String(name),
}
if website == Github {
queryResult, err = c.queryGithub(client, variables)
} else if website == Gitlab {
// variables must be recreated because specifying unused map keys will throw error
variables = map[string]interface{}{
"fullPath": owner + "/" + name,
}
queryResult, err = c.queryGitlab(client, variables)
}
if err != nil {
return models.Repository{}, err
}
repositoryID, ok := queryResult.GetID().(string)
if !ok {
return models.Repository{}, fmt.Errorf("can't convert repository id to string: %v", queryResult.GetID())
}
if queryResult.GetReleasesCount() == 0 {
return models.Repository{}, fmt.Errorf("can't find any releases for %s/%s", owner, name)
}
releaseID, ok := queryResult.GetLatestReleaseID().(models.String)
if !ok {
rID, ok := queryResult.GetLatestReleaseID().(string)
if !ok {
return models.Repository{}, fmt.Errorf("can't convert release id to string: %v", queryResult.GetLatestReleaseID())
} else {
releaseID = models.String(rID)
}
}
return models.Repository{
ID: repositoryID,
Name: string(queryResult.GetName()),
Owner: owner,
Description: string(queryResult.GetDescription()),
URL: *queryResult.GetURL(),
Release: models.Release{
ID: string(releaseID),
Name: string(queryResult.GetLatestReleaseName()),
Description: string(queryResult.GetLatestReleaseDescription()),
URL: *queryResult.GetLatestReleaseURL(),
PublishedAt: queryResult.GetLatestReleasePublishingDate(),
},
}, nil
}