Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add release notes API record feature #1039

Merged
merged 1 commit into from
Jan 30, 2020
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
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ issues:
- path: fake_client\.go
linters:
- gocritic
- dupl
linters:
disable-all: true
enable:
Expand Down
3 changes: 1 addition & 2 deletions cmd/krel/cmd/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ go_library(
deps = [
"//pkg/git:go_default_library",
"//pkg/notes:go_default_library",
"//pkg/notes/options:go_default_library",
"//pkg/release:go_default_library",
"//pkg/util:go_default_library",
"@com_github_blang_semver//:go_default_library",
"@com_github_google_go_github_v28//github:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_spf13_cobra//:go_default_library",
"@com_google_cloud_go//storage:go_default_library",
"@in_gopkg_russross_blackfriday_v2//:go_default_library",
"@org_golang_x_oauth2//:go_default_library",
],
)

Expand Down
23 changes: 4 additions & 19 deletions cmd/krel/cmd/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ import (
"text/template"

"github.com/blang/semver"
"github.com/google/go-github/v28/github"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/oauth2"
"gopkg.in/russross/blackfriday.v2"

"k8s.io/release/pkg/git"
"k8s.io/release/pkg/notes"
"k8s.io/release/pkg/notes/options"
"k8s.io/release/pkg/util"
)

Expand Down Expand Up @@ -189,7 +188,7 @@ func runChangelog() (err error) {
func generateReleaseNotes(branch, startRev, endRev string) (string, error) {
logrus.Info("Generating release notes")

notesOptions := notes.NewOptions()
notesOptions := options.New()
notesOptions.Branch = branch
notesOptions.StartRev = startRev
notesOptions.EndRev = endRev
Expand All @@ -205,22 +204,8 @@ func generateReleaseNotes(branch, startRev, endRev string) (string, error) {
return "", err
}

// Create the GitHub API client
ctx := context.Background()
httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: changelogOpts.token},
))
githubClient := github.NewClient(httpClient)

gatherer := &notes.Gatherer{
Client: notes.WrapGithubClient(githubClient),
Context: ctx,
Org: git.DefaultGithubOrg,
Repo: git.DefaultGithubRepo,
}
releaseNotes, history, err := gatherer.ListReleaseNotes(
branch, notesOptions.StartSHA, notesOptions.EndSHA, "", "",
)
gatherer := notes.NewGatherer(context.Background(), notesOptions)
releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return "", errors.Wrapf(err, "listing release notes")
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/release-notes/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ go_library(
deps = [
"//pkg/git:go_default_library",
"//pkg/notes:go_default_library",
"//pkg/notes/options:go_default_library",
"//pkg/util:go_default_library",
"@com_github_google_go_github_v28//github:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_spf13_cobra//:go_default_library",
"@org_golang_x_oauth2//:go_default_library",
],
)

Expand Down
49 changes: 23 additions & 26 deletions cmd/release-notes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,18 @@ import (
"path/filepath"
"strings"

"github.com/google/go-github/v28/github"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/oauth2"

"k8s.io/release/pkg/git"
"k8s.io/release/pkg/notes"
"k8s.io/release/pkg/notes/options"
"k8s.io/release/pkg/util"
)

var (
opts = notes.NewOptions()
opts = options.New()
cmd = &cobra.Command{
Short: "release-notes - The Kubernetes Release Notes Generator",
Use: "release-notes",
Expand Down Expand Up @@ -182,13 +181,13 @@ func init() {
cmd.PersistentFlags().StringVar(
&opts.DiscoverMode,
"discover",
util.EnvDefault("DISCOVER", notes.RevisionDiscoveryModeNONE),
util.EnvDefault("DISCOVER", options.RevisionDiscoveryModeNONE),
fmt.Sprintf("The revision discovery mode for automatic revision retrieval (options: %s)",
strings.Join([]string{
notes.RevisionDiscoveryModeNONE,
notes.RevisionDiscoveryModeMergeBaseToLatest,
notes.RevisionDiscoveryModePatchToPatch,
notes.RevisionDiscoveryModeMinorToMinor,
options.RevisionDiscoveryModeNONE,
options.RevisionDiscoveryModeMergeBaseToLatest,
options.RevisionDiscoveryModePatchToPatch,
options.RevisionDiscoveryModeMinorToMinor,
}, ", "),
),
)
Expand All @@ -213,29 +212,27 @@ func init() {
util.IsEnvSet("TOC"),
"Enable the rendering of the table of contents",
)

cmd.PersistentFlags().StringVar(
&opts.RecordDir,
"record",
util.EnvDefault("RECORD", ""),
"Record the API into a directory",
)

cmd.PersistentFlags().StringVar(
&opts.ReplayDir,
"replay",
util.EnvDefault("REPLAY", ""),
"Replay a previously recorded API from a directory",
)
}

func GetReleaseNotes() (notes.ReleaseNotes, notes.ReleaseNotesHistory, error) {
// Create the GitHub API client
ctx := context.Background()
httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: opts.GithubToken},
))
githubClient := github.NewClient(httpClient)

// Fetch a list of fully-contextualized release notes
logrus.Info("fetching all commits. This might take a while...")

gatherer := &notes.Gatherer{
Client: notes.WrapGithubClient(githubClient),
Context: ctx,
Org: opts.GithubOrg,
Repo: opts.GithubRepo,
}
releaseNotes, history, err := gatherer.ListReleaseNotes(
opts.Branch, opts.StartSHA, opts.EndSHA,
opts.RequiredAuthor, opts.ReleaseVersion,
)
gatherer := notes.NewGatherer(context.Background(), opts)
releaseNotes, history, err := gatherer.ListReleaseNotes()
if err != nil {
return nil, nil, errors.Wrapf(err, "listing release notes")
}
Expand Down
17 changes: 6 additions & 11 deletions pkg/notes/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"client.go",
"document.go",
"notes.go",
"options.go",
"toc.go",
],
importpath = "k8s.io/release/pkg/notes",
visibility = ["//visibility:public"],
deps = [
"//pkg/git:go_default_library",
"//pkg/notes/internal:go_default_library",
"//pkg/notes/client:go_default_library",
"//pkg/notes/options:go_default_library",
"@com_github_google_go_github_v28//github:go_default_library",
"@com_github_nozzle_throttler//:go_default_library",
"@com_github_pkg_errors//:go_default_library",
Expand All @@ -27,20 +25,16 @@ go_test(
"document_test.go",
"notes_gatherer_test.go",
"notes_test.go",
"options_test.go",
"toc_test.go",
],
embed = [":go_default_library"],
deps = [
"//pkg/command:go_default_library",
"//pkg/git:go_default_library",
"//pkg/notes/notesfakes:go_default_library",
"//pkg/notes/client:go_default_library",
"//pkg/notes/client/clientfakes:go_default_library",
"@com_github_google_go_github_v28//github:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
"@in_gopkg_src_d_go_git_v4//:go_default_library",
"@in_gopkg_src_d_go_git_v4//config:go_default_library",
"@in_gopkg_src_d_go_git_v4//plumbing/object:go_default_library",
"@org_golang_x_oauth2//:go_default_library",
],
)
Expand All @@ -56,8 +50,9 @@ filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//pkg/notes/client:all-srcs",
"//pkg/notes/internal:all-srcs",
"//pkg/notes/notesfakes:all-srcs",
"//pkg/notes/options:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
Expand Down
35 changes: 35 additions & 0 deletions pkg/notes/client/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = [
"client.go",
"record.go",
"replay.go",
],
importpath = "k8s.io/release/pkg/notes/client",
visibility = ["//visibility:public"],
deps = [
"//pkg/notes/internal:go_default_library",
"@com_github_google_go_github_v28//github:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_sirupsen_logrus//:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)

filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//pkg/notes/client/clientfakes:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
18 changes: 9 additions & 9 deletions pkg/notes/client.go → pkg/notes/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package notes
package client

import (
"context"
Expand All @@ -38,19 +38,19 @@ type Client interface {
GetRepoCommit(ctx context.Context, owner, repo, sha string) (*github.RepositoryCommit, *github.Response, error)
}

func WrapGithubClient(ghc *github.Client) Client {
return &githubNotesClient{ghc: ghc}
func New(ghc *github.Client) Client {
return &githubNotesClient{ghc}
}

type githubNotesClient struct {
ghc *github.Client
*github.Client
saschagrunert marked this conversation as resolved.
Show resolved Hide resolved
}

var _ Client = &githubNotesClient{}

func (c *githubNotesClient) GetCommit(ctx context.Context, owner, repo, sha string) (*github.Commit, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
commit, resp, err := c.ghc.Git.GetCommit(ctx, owner, repo, sha)
commit, resp, err := c.Git.GetCommit(ctx, owner, repo, sha)
if !shouldRetry(err) {
return commit, resp, err
}
Expand All @@ -59,7 +59,7 @@ func (c *githubNotesClient) GetCommit(ctx context.Context, owner, repo, sha stri

func (c *githubNotesClient) ListCommits(ctx context.Context, owner, repo string, opt *github.CommitsListOptions) ([]*github.RepositoryCommit, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
commits, resp, err := c.ghc.Repositories.ListCommits(ctx, owner, repo, opt)
commits, resp, err := c.Repositories.ListCommits(ctx, owner, repo, opt)
if !shouldRetry(err) {
return commits, resp, err
}
Expand All @@ -68,7 +68,7 @@ func (c *githubNotesClient) ListCommits(ctx context.Context, owner, repo string,

func (c *githubNotesClient) ListPullRequestsWithCommit(ctx context.Context, owner, repo, sha string, opt *github.PullRequestListOptions) ([]*github.PullRequest, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
prs, resp, err := c.ghc.PullRequests.ListPullRequestsWithCommit(ctx, owner, repo, sha, opt)
prs, resp, err := c.PullRequests.ListPullRequestsWithCommit(ctx, owner, repo, sha, opt)
if !shouldRetry(err) {
return prs, resp, err
}
Expand All @@ -77,7 +77,7 @@ func (c *githubNotesClient) ListPullRequestsWithCommit(ctx context.Context, owne

func (c *githubNotesClient) GetPullRequest(ctx context.Context, owner, repo string, number int) (*github.PullRequest, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
pr, resp, err := c.ghc.PullRequests.Get(ctx, owner, repo, number)
pr, resp, err := c.PullRequests.Get(ctx, owner, repo, number)
if !shouldRetry(err) {
return pr, resp, err
}
Expand All @@ -86,7 +86,7 @@ func (c *githubNotesClient) GetPullRequest(ctx context.Context, owner, repo stri

func (c *githubNotesClient) GetRepoCommit(ctx context.Context, owner, repo, sha string) (*github.RepositoryCommit, *github.Response, error) {
for shouldRetry := internal.DefaultGithubErrChecker(); ; {
commit, resp, err := c.ghc.Repositories.GetCommit(ctx, owner, repo, sha)
commit, resp, err := c.Repositories.GetCommit(ctx, owner, repo, sha)
if !shouldRetry(err) {
return commit, resp, err
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "go_default_library",
srcs = ["fake_client.go"],
importpath = "k8s.io/release/pkg/notes/client/clientfakes",
visibility = ["//visibility:public"],
deps = [
"//pkg/notes/client:go_default_library",
"@com_github_google_go_github_v28//github:go_default_library",
],
)

filegroup(
name = "package-srcs",
srcs = glob(["**"]),
Expand All @@ -13,14 +24,3 @@ filegroup(
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

go_library(
name = "go_default_library",
srcs = ["fake_client.go"],
importpath = "k8s.io/release/pkg/notes/notesfakes",
visibility = ["//visibility:public"],
deps = [
"//pkg/notes:go_default_library",
"@com_github_google_go_github_v28//github:go_default_library",
],
)
Loading