-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathfeed.go
95 lines (79 loc) · 2.35 KB
/
feed.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
package feeds
import (
"errors"
"fmt"
"time"
)
const (
schemaVer = "1.1"
DefaultUserAgent = "package-feeds (github.com/ossf/package-feeds)"
)
var ErrNoPackagesPolled = errors.New("no packages were successfully polled")
type UnsupportedOptionError struct {
Option string
Feed string
}
type ScheduledFeed interface {
Latest(cutoff time.Time) ([]*Package, time.Time, []error)
GetFeedOptions() FeedOptions
GetName() string
}
// General configuration options for feeds.
type FeedOptions struct {
// A collection of package names to poll instead of standard firehose behaviour.
// Not supported by all feeds.
Packages *[]string `yaml:"packages"`
// Cron string for scheduling the polling for the feed.
PollRate string `yaml:"poll_rate"`
}
// Marshalled json output validated against package.schema.json.
type Package struct {
Name string `json:"name"`
Version string `json:"version"`
CreatedDate time.Time `json:"created_date"`
Type string `json:"type"`
ArtifactID string `json:"artifact_id"`
SchemaVer string `json:"schema_ver"`
}
type PackagePollError struct {
Err error
Name string
}
func (err PackagePollError) Error() string {
return fmt.Sprintf("Polling for package %s returned error: %v", err.Name, err.Err)
}
// NewPackage creates a Package object without the artifact ID field populated.
func NewPackage(created time.Time, name, version, feed string) *Package {
return NewArtifact(created, name, version, "", feed)
}
// NewArtifact creates a Package object with the artifact ID field populated.
func NewArtifact(created time.Time, name, version, artifactID, feed string) *Package {
return &Package{
Name: name,
Version: version,
CreatedDate: created,
Type: feed,
ArtifactID: artifactID,
SchemaVer: schemaVer,
}
}
func ApplyCutoff(pkgs []*Package, cutoff time.Time) []*Package {
filteredPackages := []*Package{}
for _, pkg := range pkgs {
if pkg.CreatedDate.After(cutoff) {
filteredPackages = append(filteredPackages, pkg)
}
}
return filteredPackages
}
func FindCutoff(cutoff time.Time, pkgs []*Package) time.Time {
for _, pkg := range pkgs {
if pkg.CreatedDate.After(cutoff) {
cutoff = pkg.CreatedDate
}
}
return cutoff
}
func (err UnsupportedOptionError) Error() string {
return fmt.Sprintf("unsupported option `%v` supplied to %v feed", err.Option, err.Feed)
}