-
Notifications
You must be signed in to change notification settings - Fork 21
/
github.go
52 lines (42 loc) · 1.21 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
package release
import (
"context"
"fmt"
"regexp"
"github.com/google/go-github/v28/github"
"github.com/pkg/errors"
)
// GitHubRelease is a release implementation which provides version information with GitHub Release.
type GitHubRelease struct {
client *github.Client
owner string
repo string
}
// NewGitHubRelease is a factory method which returns an GitHubRelease instance.
func NewGitHubRelease(url string) (Release, error) {
re := regexp.MustCompile(`https://github.com/(.+)/(.+)`)
matched := re.FindStringSubmatch(url)
if len(matched) != 3 {
return nil, errors.Errorf("failed to parse url: %s, matched: %#v", url, matched)
}
owner := matched[1]
repo := matched[2]
return &GitHubRelease{
client: github.NewClient(nil),
owner: owner,
repo: repo,
}, nil
}
// Latest returns a latest version.
func (r *GitHubRelease) Latest() (string, error) {
release, _, err := r.client.Repositories.GetLatestRelease(context.Background(), r.owner, r.repo)
if err != nil {
return "", fmt.Errorf("failed to get the latest release from github.com/%s/%s: %s", r.owner, r.repo, err)
}
name := *release.Name
// if a name starts with `v`, remove it.
if name[0] == 'v' {
return name[1:], nil
}
return name, nil
}