-
Notifications
You must be signed in to change notification settings - Fork 23
/
github.go
166 lines (136 loc) · 4.71 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
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
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package upstream
import (
"fmt"
"strings"
"github.com/blang/semver/v4"
log "github.com/sirupsen/logrus"
"sigs.k8s.io/release-sdk/github"
)
// Github upstream representation.
type Github struct {
Base `mapstructure:",squash"`
// Github URL, e.g. hashicorp/terraform or helm/helm
URL string
// Optional: semver constraints, e.g. < 2.0.0
// Will have no effect if the dependency does not follow Semver
Constraints string
// If branch is specified, the version should be a commit SHA
// Will look for new commits on the branch
Branch string
}
// LatestVersion returns the latest non-draft, non-prerelease Github Release
// for the given repository (depending on the Constraints if set).
//
// # Authentication
//
// The Github API allows unauthenticated requests, but the API limits are very
// strict: https://developer.github.com/v3/#rate-limiting
//
// To authenticate your requests, use the GITHUB_ACCESS_TOKEN environment variable.
func (upstream Github) LatestVersion() (string, error) {
log.Debug("Using GitHub flavour")
return latestVersion(upstream)
}
func latestVersion(upstream Github) (string, error) {
if upstream.Branch == "" {
return latestRelease(upstream)
}
return latestCommit(upstream)
}
func latestRelease(upstream Github) (string, error) {
client := github.New()
if !strings.Contains(upstream.URL, "/") {
return "", fmt.Errorf(
"invalid github repo: %s\nGithub repo should be in the form owner/repo e.g., kubernetes/kubernetes",
upstream.URL,
)
}
semverConstraints := upstream.Constraints
if semverConstraints == "" {
// If no range is passed, just use the broadest possible range
semverConstraints = DefaultSemVerConstraints
}
expectedRange, err := semver.ParseRange(semverConstraints)
if err != nil {
return "", fmt.Errorf("invalid semver constraints range: %#v: %w", upstream.Constraints, err)
}
splitURL := strings.Split(upstream.URL, "/")
owner := splitURL[0]
repo := splitURL[1]
log.Debugf("Retrieving repository information for %s/%s...", owner, repo)
repoInfo, err := client.GetRepository(owner, repo)
if err != nil {
return "", fmt.Errorf("retrieving GitHub repository: %w", err)
}
if repoInfo.GetArchived() {
log.Warnf("GitHub repository %s/%s is archived", owner, repo)
}
var tags []string
// We'll need to fetch all releases, as Github doesn't provide sorting options.
// If we don't do that, we risk running into the case where for example:
// - Version 1.0.0 and 2.0.0 exist
// - A bugfix 1.0.1 gets released
//
// Now the "latest" (date-wise) release is not the highest semver, and not necessarily the one we want
log.Debugf("Retrieving releases for %s/%s...", owner, repo)
releases, err := client.Releases(owner, repo, false)
if err != nil {
return "", fmt.Errorf("retrieving GitHub releases: %w", err)
}
// if there is no releases we will try to get the tags, the project might just use tags to release.
if len(releases) == 0 {
gitHubTags, err := client.ListTags(owner, repo)
if err != nil {
return "", fmt.Errorf("retrieving GitHub tags: %w", err)
}
for _, tag := range gitHubTags {
tags = append(tags, tag.GetName())
}
} else {
for _, release := range releases {
if release.TagName == nil {
log.Debug("Skipping release without TagName")
}
if release.Draft != nil && *release.Draft {
log.Debugf("Skipping draft release: %s\n", release.GetTagName())
continue
}
tags = append(tags, release.GetTagName())
}
}
return selectHighestVersion(upstream.Constraints, expectedRange, tags)
}
func latestCommit(upstream Github) (string, error) {
client := github.New()
if !strings.Contains(upstream.URL, "/") {
return "", fmt.Errorf(
"invalid github repo: %s\nGithub repo should be in the form owner/repo e.g., kubernetes/kubernetes",
upstream.URL,
)
}
splitURL := strings.Split(upstream.URL, "/")
owner := splitURL[0]
repo := splitURL[1]
branches, err := client.ListBranches(owner, repo)
if err != nil {
return "", fmt.Errorf("retrieving GitHub branches: %w", err)
}
for _, branch := range branches {
if branch.GetName() == upstream.Branch {
return *branch.GetCommit().SHA, nil
}
}
return "", fmt.Errorf("branch '%s' not found", upstream.Branch)
}