Skip to content

Commit

Permalink
refactor: make repocounter reuse repo/pr printing (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
lindell authored Jan 1, 2025
1 parent 03e501f commit fc21bc1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 19 deletions.
34 changes: 15 additions & 19 deletions internal/multigitter/repocounter/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ type repoInfo struct {
pullRequest scm.PullRequest
}

func (ri repoInfo) String() string {
if ri.pullRequest == nil {
return ri.repository.FullName()
} else {
if urler, hasURL := ri.pullRequest.(urler); hasURL && urler.URL() != "" {
return terminal.Link(ri.pullRequest.String(), urler.URL())
} else {
return ri.pullRequest.String()
}
}
}

// NewCounter create a new repo counter
func NewCounter() *Counter {
return &Counter{
Expand Down Expand Up @@ -76,30 +88,14 @@ func (r *Counter) Info() string {
for _, errMsg := range errors {
exitInfo += fmt.Sprintf("%s:\n", strings.ToUpper(errMsg[0:1])+errMsg[1:])
for _, errInfo := range r.errors[errMsg] {
if errInfo.pullRequest == nil {
exitInfo += fmt.Sprintf(" %s\n", errInfo.repository.FullName())
} else {
if urler, hasURL := errInfo.pullRequest.(urler); hasURL && urler.URL() != "" {
exitInfo += fmt.Sprintf(" %s\n", terminal.Link(errInfo.pullRequest.String(), urler.URL()))
} else {
exitInfo += fmt.Sprintf(" %s\n", errInfo.pullRequest.String())
}
}
exitInfo += fmt.Sprintf(" %s\n", errInfo.String())
}
}

if len(r.successRepositories) > 0 {
exitInfo += "Repositories with a successful run:\n"
for _, repo := range r.successRepositories {
if repo.pullRequest != nil {
if urler, hasURL := repo.pullRequest.(urler); hasURL && urler.URL() != "" {
exitInfo += fmt.Sprintf(" %s\n", terminal.Link(repo.pullRequest.String(), urler.URL()))
} else {
exitInfo += fmt.Sprintf(" %s\n", repo.pullRequest.String())
}
} else {
exitInfo += fmt.Sprintf(" %s\n", repo.repository.FullName())
}
for _, repoInfo := range r.successRepositories {
exitInfo += fmt.Sprintf(" %s\n", repoInfo.String())
}
}

Expand Down
39 changes: 39 additions & 0 deletions internal/multigitter/repocounter/counter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

"github.com/lindell/multi-gitter/internal/multigitter/repocounter"
"github.com/lindell/multi-gitter/internal/multigitter/terminal"
"github.com/lindell/multi-gitter/internal/scm"
"github.com/lindell/multi-gitter/tests/vcmock"
)
Expand Down Expand Up @@ -56,6 +57,25 @@ Repositories with a successful run:
want: `
Repositories with a successful run:
owner-1/repo-1 #42
`,
},
{
name: "one success with pr and link",
changeFn: func(r *repocounter.Counter) {
repo := vcmock.Repository{
OwnerName: "owner-1",
RepoName: "has-url",
}
pr := vcmock.PullRequest{
PRStatus: scm.PullRequestStatusPending,
Repository: repo,
PRNumber: 42,
}
r.AddSuccessPullRequest(repo, pr)
},
want: `
Repositories with a successful run:
` + terminal.Link("owner-1/has-url #42", "https://github.com/owner/has-url/pull/1") + `
`,
},
{
Expand All @@ -76,6 +96,25 @@ Test error:
want: `
Test error:
owner-1/repo-1 #42
`,
},
{
name: "one error with pr and link",
changeFn: func(r *repocounter.Counter) {
repo := vcmock.Repository{
OwnerName: "owner-1",
RepoName: "has-url",
}
pr := vcmock.PullRequest{
PRStatus: scm.PullRequestStatusPending,
Repository: repo,
PRNumber: 42,
}
r.AddError(errors.New("test error"), repo, pr)
},
want: `
Test error:
` + terminal.Link("owner-1/has-url #42", "https://github.com/owner/has-url/pull/1") + `
`,
},
{
Expand Down

0 comments on commit fc21bc1

Please sign in to comment.