Skip to content

Commit

Permalink
Merge pull request #69 from gomicro/next-test
Browse files Browse the repository at this point in the history
next test
  • Loading branch information
dan9186 authored Apr 10, 2023
2 parents 87393df + 32af5cb commit ec2024f
Show file tree
Hide file tree
Showing 121 changed files with 9,685 additions and 2,536 deletions.
11 changes: 8 additions & 3 deletions client/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ package client

import (
"context"
"errors"
"fmt"
"strings"
)

var prBodyTemplate = `
var (
ErrNoCommits = errors.New("no commits")

prBodyTemplate = `
----
Release PR created with ` + "`train`"
)

func (c *Client) createChangeLog(ctx context.Context, owner, name, base, head string) (map[string][]string, error) {
changes := map[string][]string{
Expand All @@ -22,11 +27,11 @@ func (c *Client) createChangeLog(ctx context.Context, owner, name, base, head st

comp, _, err := c.ghClient.Repositories.CompareCommits(ctx, owner, name, base, head)
if err != nil {
return nil, fmt.Errorf("compare commits: %v", err.Error())
return nil, fmt.Errorf("compare commits: %w", err)
}

if len(comp.Commits) == 0 {
return nil, fmt.Errorf("no commits")
return nil, ErrNoCommits
}

for _, commit := range comp.Commits {
Expand Down
4 changes: 2 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *Client) GetLogins(ctx context.Context) ([]string, error) {
return nil, fmt.Errorf("github: hit rate limit")
}

return nil, fmt.Errorf("get user: %v", err.Error())
return nil, fmt.Errorf("get user: %w", err)
}

logins = append(logins, strings.ToLower(user.GetLogin()))
Expand All @@ -96,7 +96,7 @@ func (c *Client) GetLogins(ctx context.Context) ([]string, error) {
return nil, fmt.Errorf("github: hit rate limit")
}

return nil, fmt.Errorf("list orgs: %v", err.Error())
return nil, fmt.Errorf("list orgs: %w", err)
}

for i := range orgs {
Expand Down
79 changes: 79 additions & 0 deletions client/clienttest/clienttest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package clienttest

import (
"context"
"fmt"

"github.com/gomicro/crawl"
"github.com/google/go-github/github"
)

type ClientTest struct {
cfg *Config
}

type Config struct {
BaseBranchName string
Logins []string
LoginsError error
Repos []*github.Repository
ReposError error
ProcessReposError error
}

func New(cfg *Config) *ClientTest {
return &ClientTest{
cfg: cfg,
}
}

func (ct *ClientTest) GetBaseBranchName() string {
if ct.cfg != nil {
return ct.cfg.BaseBranchName
}

return ""
}

func (ct *ClientTest) GetLogins(context.Context) ([]string, error) {
if ct.cfg.LoginsError != nil {
return nil, ct.cfg.LoginsError
}

return ct.cfg.Logins, nil
}

func (ct *ClientTest) GetRepos(ctx context.Context, progress *crawl.Progress, name string) ([]*github.Repository, error) {
if ct.cfg.ReposError != nil {
return nil, ct.cfg.ReposError
}

return ct.cfg.Repos, nil
}

func (ct *ClientTest) ProcessRepos(ctx context.Context, progress *crawl.Progress, repos []*github.Repository, dryRun bool) ([]string, error) {
if ct.cfg.ProcessReposError != nil {
return nil, ct.cfg.ProcessReposError
}

urls := make([]string, len(ct.cfg.Repos))

for i, r := range repos {
name := r.GetName()
owner := r.GetOwner().GetLogin()
head := r.GetDefaultBranch()

if !dryRun {
urls = append(urls, fmt.Sprintf("https://github.com/%s/%s/pull/%d", owner, name, i))
continue
}

urls = append(urls, fmt.Sprintf("https://github.com/%s/%s/compare/%s...%s", owner, name, ct.cfg.BaseBranchName, head))
}

return urls, nil
}

func (ct *ClientTest) ReleaseRepos(ctx context.Context, progress *crawl.Progress, repos []*github.Repository, dryRun bool) ([]string, error) {
return nil, nil
}
17 changes: 17 additions & 0 deletions client/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package client

import (
"context"

"github.com/gomicro/crawl"
"github.com/google/go-github/github"
)

// interface for a train client
type Clienter interface {
GetBaseBranchName() string
GetLogins(context.Context) ([]string, error)
GetRepos(context.Context, *crawl.Progress, string) ([]*github.Repository, error)
ProcessRepos(context.Context, *crawl.Progress, []*github.Repository, bool) ([]string, error)
ReleaseRepos(context.Context, *crawl.Progress, []*github.Repository, bool) ([]string, error)
}
124 changes: 71 additions & 53 deletions client/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package client

import (
"context"
"errors"
"fmt"
"net/http"
"os"
"sort"
"strings"

"github.com/gomicro/crawl"
"github.com/gomicro/crawl/bar"
"github.com/google/go-github/github"
"github.com/gosuri/uiprogress"
)

func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repository, error) {
var ErrGetBranch = errors.New("get branch")

func (c *Client) GetRepos(ctx context.Context, progress *crawl.Progress, name string) ([]*github.Repository, error) {
count := 0
orgFound := true

Expand All @@ -24,7 +27,7 @@ func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repositor
return nil, fmt.Errorf("github: hit rate limit")
}

return nil, fmt.Errorf("get org: %v", err)
return nil, fmt.Errorf("get org: %w", err)
}

if resp.StatusCode == http.StatusNotFound {
Expand All @@ -49,12 +52,16 @@ func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repositor
return nil, fmt.Errorf("no repos found")
}

repoBar := uiprogress.AddBar(count).
AppendCompleted().
PrependElapsed().
PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Fetching (%d/%d)", b.Current(), count)
})
theme := bar.NewThemeFromTheme(bar.DefaultTheme)
theme.Append(func(b *bar.Bar) string {
return fmt.Sprintf(" %0.2f", b.CompletedPercent())
})
theme.Prepend(func(b *bar.Bar) string {
return fmt.Sprintf("Fetching (%d/%d) %s", b.Current(), b.Total(), b.Elapsed())
})

repoBar := bar.New(theme, count)
progress.AddBar(repoBar)

orgOpts := &github.RepositoryListByOrgOptions{
Type: "all",
Expand Down Expand Up @@ -139,21 +146,25 @@ func (c *Client) GetRepos(ctx context.Context, name string) ([]*github.Repositor
return repos, nil
}

func (c *Client) ProcessRepos(ctx context.Context, repos []*github.Repository, dryRun bool) ([]string, error) {
func (c *Client) ProcessRepos(ctx context.Context, progress *crawl.Progress, repos []*github.Repository, dryRun bool) ([]string, error) {
count := len(repos)
name := repos[0].GetName()
owner := repos[0].GetOwner().GetLogin()
appendStr := fmt.Sprintf("\nCurrent Repo: %v/%v", owner, name)

bar := uiprogress.AddBar(count).
AppendCompleted().
PrependElapsed().
PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Processing (%d/%d)", b.Current(), count)
}).
AppendFunc(func(b *uiprogress.Bar) string {
return appendStr
})
theme := bar.NewThemeFromTheme(bar.DefaultTheme)
theme.Append(func(b *bar.Bar) string {
return fmt.Sprintf(" %0.2f", b.CompletedPercent())
})
theme.Append(func(b *bar.Bar) string {
return appendStr
})
theme.Prepend(func(b *bar.Bar) string {
return fmt.Sprintf("Processing (%d/%d) %s", b.Current(), b.Total(), b.Elapsed())
})

repoBar := bar.New(theme, count)
progress.AddBar(repoBar)

urls := []string{}
for _, repo := range repos {
Expand All @@ -163,17 +174,16 @@ func (c *Client) ProcessRepos(ctx context.Context, repos []*github.Repository, d

url, err := c.processRepo(ctx, repo, dryRun)
if err != nil {
if strings.HasPrefix(err.Error(), "get branch: ") || strings.HasPrefix(err.Error(), "no commits") {
bar.Incr()
if errors.Is(err, ErrGetBranch) || errors.Is(err, ErrNoCommits) {
repoBar.Incr()
continue
}

fmt.Printf("process repo: %v\n", err.Error())
os.Exit(1)
return nil, fmt.Errorf("process repo: %w", err)
}

urls = append(urls, url)
bar.Incr()
repoBar.Incr()
}

appendStr = ""
Expand All @@ -191,7 +201,7 @@ func (c *Client) processRepo(ctx context.Context, repo *github.Repository, dryRu
c.rate.Wait(ctx) //nolint: errcheck
_, _, err := c.ghClient.Repositories.GetBranch(ctx, owner, name, c.cfg.ReleaseBranch)
if err != nil {
return "", fmt.Errorf("get branch: %v", err.Error())
return "", fmt.Errorf("%w: %w", ErrGetBranch, err)
}

opts := &github.PullRequestListOptions{
Expand All @@ -202,7 +212,7 @@ func (c *Client) processRepo(ctx context.Context, repo *github.Repository, dryRu
c.rate.Wait(ctx) //nolint: errcheck
prs, _, err := c.ghClient.PullRequests.List(ctx, owner, name, opts)
if err != nil {
return "", fmt.Errorf("list prs: %v", err.Error())
return "", fmt.Errorf("list prs: %w", err)
}

if len(prs) > 0 {
Expand Down Expand Up @@ -242,17 +252,17 @@ func (c *Client) processRepo(ctx context.Context, repo *github.Repository, dryRu
c.rate.Wait(ctx) //nolint: errcheck
pr, _, err := c.ghClient.PullRequests.Create(ctx, owner, name, newPR)
if err != nil {
return "", fmt.Errorf("create pr: %v", err.Error())
return "", fmt.Errorf("create pr: %w", err)
}

return pr.GetHTMLURL(), nil
}

return fmt.Sprintf("https://github.com/%v/%v/compare/%v...%v", owner, name, c.cfg.ReleaseBranch, head), nil
return fmt.Sprintf("https://github.com/%s/%s/compare/%s...%s", owner, name, c.cfg.ReleaseBranch, head), nil
}

func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, dryRun bool) ([]string, error) {
releases, err := c.getReleases(ctx, repos)
func (c *Client) ReleaseRepos(ctx context.Context, progress *crawl.Progress, repos []*github.Repository, dryRun bool) ([]string, error) {
releases, err := c.getReleases(ctx, progress, repos)
if err != nil {
return nil, fmt.Errorf("releases: %v\n", err.Error())
}
Expand All @@ -267,15 +277,19 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
owner := repo.GetOwner().GetLogin()
appendStr := fmt.Sprintf("\nCurrent Repo: %v/%v", owner, name)

bar := uiprogress.AddBar(count).
AppendCompleted().
PrependElapsed().
PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Processing Releases (%d/%d)", b.Current(), count)
}).
AppendFunc(func(b *uiprogress.Bar) string {
return appendStr
})
theme := bar.NewThemeFromTheme(bar.DefaultTheme)
theme.Append(func(b *bar.Bar) string {
return fmt.Sprintf(" %0.2f", b.CompletedPercent())
})
theme.Append(func(b *bar.Bar) string {
return appendStr
})
theme.Prepend(func(b *bar.Bar) string {
return fmt.Sprintf("Processing Releases (%d/%d) %s", b.Current(), b.Total(), b.Elapsed())
})

repoBar := bar.New(theme, count)
progress.AddBar(repoBar)

var released []string
for _, release := range releases {
Expand All @@ -292,7 +306,7 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
}

if strings.ToLower(release.GetMergeableState()) != "clean" {
bar.Incr()
repoBar.Incr()
continue
}

Expand All @@ -310,7 +324,7 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
released = append(released, release.GetHTMLURL())
}

bar.Incr()
repoBar.Incr()
}

appendStr = ""
Expand All @@ -320,23 +334,27 @@ func (c *Client) ReleaseRepos(ctx context.Context, repos []*github.Repository, d
return released, nil
}

func (c *Client) getReleases(ctx context.Context, repos []*github.Repository) ([]*github.PullRequest, error) {
func (c *Client) getReleases(ctx context.Context, progress *crawl.Progress, repos []*github.Repository) ([]*github.PullRequest, error) {
var releases []*github.PullRequest

count := len(repos)
name := repos[0].GetName()
owner := repos[0].GetOwner().GetLogin()
appendStr := fmt.Sprintf("\nCurrent Repo: %v/%v", owner, name)

bar := uiprogress.AddBar(count).
AppendCompleted().
PrependElapsed().
PrependFunc(func(b *uiprogress.Bar) string {
return fmt.Sprintf("Collecting Releases (%d/%d)", b.Current(), count)
}).
AppendFunc(func(b *uiprogress.Bar) string {
return appendStr
})
theme := bar.NewThemeFromTheme(bar.DefaultTheme)
theme.Append(func(b *bar.Bar) string {
return fmt.Sprintf(" %0.2f", b.CompletedPercent())
})
theme.Append(func(b *bar.Bar) string {
return appendStr
})
theme.Prepend(func(b *bar.Bar) string {
return fmt.Sprintf("Collecting Releases (%d/%d) %s", b.Current(), b.Total(), b.Elapsed())
})

repoBar := bar.New(theme, count)
progress.AddBar(repoBar)

for _, repo := range repos {
owner = repo.GetOwner().GetLogin()
Expand All @@ -356,7 +374,7 @@ func (c *Client) getReleases(ctx context.Context, repos []*github.Repository) ([
}

releases = append(releases, rs...)
bar.Incr()
repoBar.Incr()
}

appendStr = ""
Expand Down
Loading

0 comments on commit ec2024f

Please sign in to comment.