-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upstreams: add gitlab as upstream option
- Loading branch information
Showing
11 changed files
with
614 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// +build tools | ||
|
||
/* | ||
Copyright 2021 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. | ||
*/ | ||
|
||
// This is used to import things required by build scripts, to force `go mod` to see them as dependencies | ||
|
||
package internal | ||
|
||
import ( | ||
_ "github.com/maxbrunsfeld/counterfeiter/v6" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
Copyright 2021 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 gitlab | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
"github.com/xanzy/go-gitlab" | ||
"k8s.io/release/pkg/util" | ||
) | ||
|
||
const ( | ||
// TokenEnvKey is the default GitLab token environemt variable key | ||
TokenEnvKey = "GITLAB_TOKEN" | ||
// PrivateTokenEnvKey is the priavete GitLAb token environemt variable key | ||
PrivateTokenEnvKey = "GITLAB_PRIVATE_TOKEN" | ||
apiVersionPath = "api/v4/" | ||
) | ||
|
||
// GitLab is a wrapper around GitLab related functionality | ||
type GitLab struct { | ||
client Client | ||
} | ||
|
||
type gitlabClient struct { | ||
*gitlab.Client | ||
} | ||
|
||
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate | ||
//counterfeiter:generate . Client | ||
type Client interface { | ||
ListReleases( | ||
string, string, *gitlab.ListReleasesOptions, | ||
) ([]*gitlab.Release, *gitlab.Response, error) | ||
} | ||
|
||
// New creates a new default GitLab client. Tokens set via the $GITLAB_TOKEN | ||
// environment variable will result in an authenticated client. | ||
// If the $GITLAB_TOKEN is not set, then it will return a nil client. | ||
// GitLab requires autenticated users. | ||
func New() *GitLab { | ||
token := util.EnvDefault(TokenEnvKey, "") | ||
var git *gitlab.Client | ||
if token == "" { | ||
logrus.Debug("No GITLAB_TOKEN configured") | ||
return nil | ||
} | ||
|
||
logrus.Debug("Using GitLab client") | ||
var err error | ||
git, err = gitlab.NewClient(token) | ||
if err != nil { | ||
logrus.Errorf("failed to create the GitLab client: %v", err.Error()) | ||
return nil | ||
} | ||
|
||
return &GitLab{&gitlabClient{git}} | ||
} | ||
|
||
func NewPrivate(baseURL string) *GitLab { | ||
token := util.EnvDefault(PrivateTokenEnvKey, "") | ||
var git *gitlab.Client | ||
if token == "" { | ||
logrus.Debug("No GITLAB_PRIVATE_TOKEN configured") | ||
return nil | ||
} | ||
|
||
logrus.Debug("Using GitLab client") | ||
var err error | ||
git, err = gitlab.NewClient(token, gitlab.WithBaseURL(baseURL+apiVersionPath)) | ||
if err != nil { | ||
logrus.Errorf("failed to create the GitLab client: %v", err.Error()) | ||
return nil | ||
} | ||
|
||
return &GitLab{&gitlabClient{git}} | ||
} | ||
|
||
func (g *gitlabClient) ListReleases( | ||
owner, repo string, opt *gitlab.ListReleasesOptions, | ||
) ([]*gitlab.Release, *gitlab.Response, error) { | ||
// TODO: add retry similar in what we have the pkg/github | ||
project := fmt.Sprintf("%s/%s", owner, repo) | ||
releases, resp, err := g.Releases.ListReleases(project, opt) | ||
return releases, resp, err | ||
} | ||
|
||
// SetClient can be used to manually set the internal GitLab client | ||
func (g *GitLab) SetClient(client Client) { | ||
g.client = client | ||
} | ||
|
||
// Client can be used to retrieve the Client type | ||
func (g *GitLab) Client() Client { | ||
return g.client | ||
} | ||
|
||
// Releases returns a list of GitLab releases for the provided `owner` and | ||
// `repo`. | ||
func (g *GitLab) Releases(owner, repo string) ([]*gitlab.Release, error) { | ||
allReleases, _, err := g.client.ListReleases(owner, repo, nil) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "unable to retrieve GitLab releases for %s/%s", owner, repo) | ||
} | ||
|
||
return allReleases, nil | ||
} |
Oops, something went wrong.