-
Notifications
You must be signed in to change notification settings - Fork 58
/
versions.go
55 lines (47 loc) · 1.05 KB
/
versions.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
package resource
import (
"regexp"
"strconv"
"time"
"github.com/google/go-github/v66/github"
)
var defaultTagFilter = "^v?([^v].*)"
type versionParser struct {
re *regexp.Regexp
}
func newVersionParser(filter string) (versionParser, error) {
if filter == "" {
filter = defaultTagFilter
}
re, err := regexp.Compile(filter)
if err != nil {
return versionParser{}, err
}
return versionParser{re: re}, nil
}
func (vp *versionParser) parse(tag string) string {
matches := vp.re.FindStringSubmatch(tag)
if len(matches) > 0 {
return matches[len(matches)-1]
}
return ""
}
func getTimestamp(release *github.RepositoryRelease) time.Time {
if release.PublishedAt != nil {
return release.PublishedAt.Time
} else if release.CreatedAt != nil {
return release.CreatedAt.Time
} else {
return time.Time{}
}
}
func versionFromRelease(release *github.RepositoryRelease) Version {
v := Version{
ID: strconv.FormatInt(*release.ID, 10),
Timestamp: getTimestamp(release),
}
if release.TagName != nil {
v.Tag = *release.TagName
}
return v
}