Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion cmd/krel/cmd/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,11 @@ func generateReleaseNotes(opts *changelogOptions, branch, startRev, endRev strin
return "", err
}

gatherer := notes.NewGatherer(context.Background(), notesOptions)
gatherer, err := notes.NewGatherer(context.Background(), notesOptions)
if err != nil {
return "", errors.Wrapf(err, "retrieving notes gatherer")
}

releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return "", errors.Wrapf(err, "listing release notes")
Expand Down
5 changes: 4 additions & 1 deletion cmd/krel/cmd/release_notes.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,10 @@ func releaseNotesFrom(startTag string) (*releaseNotesResult, error) {
}

// Fetch the notes
gatherer := notes.NewGatherer(context.Background(), notesOptions)
gatherer, err := notes.NewGatherer(context.Background(), notesOptions)
if err != nil {
return nil, errors.Wrapf(err, "retrieving notes gatherer")
}
releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return nil, errors.Wrapf(err, "listing release notes")
Expand Down
5 changes: 4 additions & 1 deletion cmd/release-notes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ func init() {
func GetReleaseNotes() (notes.ReleaseNotes, notes.ReleaseNotesHistory, error) {
logrus.Info("fetching all commits. This might take a while...")

gatherer := notes.NewGatherer(context.Background(), opts)
gatherer, err := notes.NewGatherer(context.Background(), opts)
if err != nil {
return nil, nil, errors.Wrapf(err, "retrieving notes gatherer")
}
releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return nil, nil, errors.Wrapf(err, "listing release notes")
Expand Down
8 changes: 7 additions & 1 deletion pkg/github/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["github.go"],
srcs = [
"github.go",
"record.go",
"replay.go",
],
importpath = "k8s.io/release/pkg/github",
visibility = ["//visibility:public"],
deps = [
"//pkg/git:go_default_library",
"//pkg/github/internal:go_default_library",
"//pkg/util:go_default_library",
"@com_github_google_go_github_v29//github:go_default_library",
"@com_github_pkg_errors//:go_default_library",
Expand Down Expand Up @@ -39,6 +44,7 @@ filegroup(
srcs = [
":package-srcs",
"//pkg/github/githubfakes:all-srcs",
"//pkg/github/internal:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
Expand Down
122 changes: 114 additions & 8 deletions pkg/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"net/http"
"os"
"strings"

"github.com/google/go-github/v29/github"
Expand All @@ -28,6 +29,7 @@ import (
"golang.org/x/oauth2"

"k8s.io/release/pkg/git"
"k8s.io/release/pkg/github/internal"
"k8s.io/release/pkg/util"
)

Expand All @@ -48,13 +50,33 @@ type githubClient struct {
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 -generate
//counterfeiter:generate . Client
type Client interface {
ListTags(
context.Context, string, string, *github.ListOptions,
) ([]*github.RepositoryTag, *github.Response, error)
GetCommit(
context.Context, string, string, string,
) (*github.Commit, *github.Response, error)

GetPullRequest(
context.Context, string, string, int,
) (*github.PullRequest, *github.Response, error)

GetRepoCommit(
context.Context, string, string, string,
) (*github.RepositoryCommit, *github.Response, error)

ListCommits(
context.Context, string, string, *github.CommitsListOptions,
) ([]*github.RepositoryCommit, *github.Response, error)

ListPullRequestsWithCommit(
context.Context, string, string, string, *github.PullRequestListOptions,
) ([]*github.PullRequest, *github.Response, error)

ListReleases(
context.Context, string, string, *github.ListOptions,
) ([]*github.RepositoryRelease, *github.Response, error)

ListTags(
context.Context, string, string, *github.ListOptions,
) ([]*github.RepositoryTag, *github.Response, error)
}

// New creates a new default GitHub client. Tokens set via the $GITHUB_TOKEN
Expand All @@ -76,23 +98,107 @@ func New() *GitHub {
return &GitHub{&githubClient{github.NewClient(client)}}
}

func (g *githubClient) ListTags(
ctx context.Context, owner, repo string, opt *github.ListOptions,
) ([]*github.RepositoryTag, *github.Response, error) {
return g.Client.Repositories.ListTags(ctx, owner, repo, opt)
// NewWithToken can be used to specify a GITHUB_TOKEN before retrieving the
// client to enforce authenticated GitHub requests
func NewWithToken(token string) (*GitHub, error) {
if err := os.Setenv(TokenEnvKey, token); err != nil {
return nil, errors.Wrapf(err, "unable to export %s", TokenEnvKey)
}
return New(), nil
}

func (g *githubClient) GetCommit(
ctx context.Context, owner, repo, sha string,
) (*github.Commit, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
commit, resp, err := g.Git.GetCommit(ctx, owner, repo, sha)
if !shouldRetry(err) {
return commit, resp, err
}
}
}

func (g *githubClient) GetPullRequest(
ctx context.Context, owner, repo string, number int,
) (*github.PullRequest, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
pr, resp, err := g.PullRequests.Get(ctx, owner, repo, number)
if !shouldRetry(err) {
return pr, resp, err
}
}
}

func (g *githubClient) GetRepoCommit(
ctx context.Context, owner, repo, sha string,
) (*github.RepositoryCommit, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
commit, resp, err := g.Repositories.GetCommit(ctx, owner, repo, sha)
if !shouldRetry(err) {
return commit, resp, err
}
}
}

func (g *githubClient) ListCommits(
ctx context.Context, owner, repo string, opt *github.CommitsListOptions,
) ([]*github.RepositoryCommit, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
commits, resp, err := g.Repositories.ListCommits(ctx, owner, repo, opt)
if !shouldRetry(err) {
return commits, resp, err
}
}
}

func (g *githubClient) ListPullRequestsWithCommit(
ctx context.Context, owner, repo, sha string,
opt *github.PullRequestListOptions,
) ([]*github.PullRequest, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
prs, resp, err := g.PullRequests.ListPullRequestsWithCommit(
ctx, owner, repo, sha, opt,
)
if !shouldRetry(err) {
return prs, resp, err
}
}
}

func (g *githubClient) ListReleases(
ctx context.Context, owner, repo string, opt *github.ListOptions,
) ([]*github.RepositoryRelease, *github.Response, error) {
return g.Client.Repositories.ListReleases(ctx, owner, repo, opt)
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
releases, resp, err := g.Repositories.ListReleases(
ctx, owner, repo, opt,
)
if !shouldRetry(err) {
return releases, resp, err
}
}
}

func (g *githubClient) ListTags(
ctx context.Context, owner, repo string, opt *github.ListOptions,
) ([]*github.RepositoryTag, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
tags, resp, err := g.Repositories.ListTags(ctx, owner, repo, opt)
if !shouldRetry(err) {
return tags, resp, err
}
}
}

// SetClient can be used to manually set the internal GitHub client
func (g *GitHub) SetClient(client Client) {
g.client = client
}

// Client can be used to retrieve the Client type
func (g *GitHub) Client() Client {
return g.client
}

// TagsPerBranch is an abstraction over a simple branch to latest tag association
type TagsPerBranch map[string]string

Expand Down
Loading