Skip to content

Commit

Permalink
render docx raport
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed Oct 20, 2023
1 parent 14ab3db commit e8267a2
Show file tree
Hide file tree
Showing 15 changed files with 262 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ tmp/
# development
*.diff
*.patch
*.txt
*.docx
*.dotx
29 changes: 24 additions & 5 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"fmt"
"time"

gh "github.com/google/go-github/v53/github"
"github.com/pPrecel/PKUP/internal/logo"
"github.com/pPrecel/PKUP/internal/token"
"github.com/pPrecel/PKUP/internal/view"
"github.com/pPrecel/PKUP/pkg/artifacts"
"github.com/pPrecel/PKUP/pkg/github"
"github.com/pPrecel/PKUP/pkg/period"
"github.com/pPrecel/PKUP/pkg/raport"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -86,12 +88,13 @@ func genCommandAction(ctx *cli.Context, opts *genActionOpts) error {
"before", mergedBefore.Local().Format(logTimeFormat),
))

raportResults := []raport.Result{}
for org, repos := range opts.repos {
for i := range repos {
org := org
repo := repos[i]

valChan := make(chan []string)
valChan := make(chan []*gh.PullRequest)
errChan := make(chan error)
multiView.Add(fmt.Sprintf("%s/%s", org, repo), valChan, errChan)
go func() {
Expand All @@ -118,26 +121,42 @@ func genCommandAction(ctx *cli.Context, opts *genActionOpts) error {
"mergedBefore", config.MergedBefore.String(),
))

prs, err := artifacts.GenUserArtifactsToDir(client, config)
prs, processErr := artifacts.GenUserArtifactsToDir(client, config)

log.Debug("ending process for repo", log.Args(
"org", config.Org,
"repo", config.Repo,
"prs", prs,
"error", err,
"error", processErr,
))
if err != nil {
errChan <- err
if processErr != nil {
errChan <- processErr
return
}

valChan <- prs
raportResults = append(raportResults, raport.Result{
Org: org,
Repo: repo,
PullRequests: prs,
})
}()
}
}

multiView.Run()

err = raport.Render(raport.Options{
OutputDir: opts.dir,
TemplatePath: opts.templatePath,
PeriodFrom: mergedAfter,
PeriodTill: mergedBefore,
Results: raportResults,
})
if err != nil {
return err
}

opts.Log.Info("all patch files saved to dir", opts.Log.Args("dir", opts.dir))
return nil
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/gen_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ func getGenFlags(opts *genActionOpts) []cli.Flag {
return nil
},
},
&cli.StringFlag{
Name: "template-path",
Usage: "full path to the docx template - go to project repo for more info",
Aliases: []string{"tp"},
Destination: &opts.templatePath,
Action: func(_ *cli.Context, path string) error {
if path == "" {
return fmt.Errorf("'%s' template path is empty", path)
}

return nil
},
},
&cli.BoolFlag{
Name: "with-closed",
Usage: "count closed (not merged) PullRequests",
Expand Down
1 change: 1 addition & 0 deletions cmd/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type genActionOpts struct {
token string
username string
enterpriseURL string
templatePath string
repos map[string][]string
withClosed bool
ci bool
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/cli/oauth v1.0.1
github.com/google/go-github/v53 v53.1.0
github.com/nguyenthenguyen/docx v0.0.0-20230621112118-9c8e795a11db
github.com/pkg/errors v0.9.1
github.com/pterm/pterm v0.12.69
github.com/stretchr/testify v1.8.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/nguyenthenguyen/docx v0.0.0-20230621112118-9c8e795a11db h1:v0cW/tTMrJQyZr7r6t+t9+NhH2OBAjydHisVYxuyObc=
github.com/nguyenthenguyen/docx v0.0.0-20230621112118-9c8e795a11db/go.mod h1:BZyH8oba3hE/BTt2FfBDGPOHhXiKs9RFmUvvXRdzrhM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
14 changes: 14 additions & 0 deletions internal/file/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ package file
import (
"fmt"
"os"

gh "github.com/google/go-github/v53/github"
)

func BuildDiffFilename(pr *gh.PullRequest, org, repo string) string {
return fmt.Sprintf("%s_%s_%s.diff", org, repo, cutSHA(pr.GetMergeCommitSHA()))
}

func Create(dir, filename, content string) error {
file, err := os.Create(fmt.Sprintf("%s/%s", dir, filename))
if err != nil {
Expand All @@ -14,3 +20,11 @@ func Create(dir, filename, content string) error {
_, err = file.WriteString(content)
return err
}

func cutSHA(fullSHA string) string {
if len(fullSHA) < 8 {
return fullSHA
}

return fullSHA[0:8]
}
27 changes: 24 additions & 3 deletions internal/view/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"

gh "github.com/google/go-github/v53/github"
"github.com/pkg/errors"
"github.com/pterm/pterm"
)
Expand Down Expand Up @@ -41,7 +42,7 @@ func (mtv *dynamicMultiView) NewWriter() io.Writer {
return mtv.multiPrinter.NewWriter()
}

func (mtv *dynamicMultiView) Add(name string, valuesChan chan []string, errorChan chan error) {
func (mtv *dynamicMultiView) Add(name string, valuesChan chan []*gh.PullRequest, errorChan chan error) {
mtv.tasks[name] = taskChannels{
valuesChan: valuesChan,
errorChan: errorChan,
Expand Down Expand Up @@ -83,8 +84,8 @@ func selectChannelsForSpinners(workingSpinners map[string]*pterm.SpinnerPrinter,
text := buildPRsTreeString(
fmt.Sprintf(
"found %d PRs for repo '%s'",
len(PRs), taskName,
), PRs,
len(PRs), taskName),
prsToStringList(PRs),
)
workingSpinners[taskName].Success(text)
}
Expand Down Expand Up @@ -128,3 +129,23 @@ func startSpinnersWithPrinter(tasks map[string]taskChannels, multi *pterm.MultiP

return spinners, nil
}

func prsToStringList(prs []*gh.PullRequest) []string {
list := []string{}
for i := range prs {
pr := *prs[i]

title := fmt.Sprintf(" %s (#%d) %s", getStatePrefix(pr), pr.GetNumber(), pr.GetTitle())
list = append(list, title)
}

return list
}

func getStatePrefix(pr gh.PullRequest) string {
if !pr.GetMergedAt().IsZero() {
return pterm.Magenta("[M]")
}

return pterm.Red("[C]")
}
5 changes: 3 additions & 2 deletions internal/view/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"os"

gh "github.com/google/go-github/v53/github"
"github.com/pterm/pterm"
)

Expand All @@ -24,7 +25,7 @@ func newStatic(log *pterm.Logger) MultiTaskView {
}
}

func (sv *staticView) Add(name string, valuesChan chan []string, errorChan chan error) {
func (sv *staticView) Add(name string, valuesChan chan []*gh.PullRequest, errorChan chan error) {
sv.tasks[name] = taskChannels{
valuesChan: valuesChan,
errorChan: errorChan,
Expand Down Expand Up @@ -61,7 +62,7 @@ func selectChannelsForLogger(log *pterm.Logger, taskName string, channels taskCh
)
} else {
text := fmt.Sprintf("found %d PRs for repo '%s'", len(PRs), taskName)
log.Info(text, log.Args("prs", PRs))
log.Info(text, log.Args("prs", prsToStringList(PRs)))
}
}
default:
Expand Down
5 changes: 3 additions & 2 deletions internal/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ package view
import (
"io"

gh "github.com/google/go-github/v53/github"
"github.com/pterm/pterm"
)

type MultiTaskView interface {
Run() error
Add(string, chan []string, chan error)
Add(string, chan []*gh.PullRequest, chan error)
NewWriter() io.Writer
}

type taskChannels struct {
valuesChan chan []string
valuesChan chan []*gh.PullRequest
errorChan chan error
}

Expand Down
36 changes: 3 additions & 33 deletions pkg/artifacts/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
gh "github.com/google/go-github/v53/github"
"github.com/pPrecel/PKUP/internal/file"
"github.com/pPrecel/PKUP/pkg/github"
"github.com/pterm/pterm"
)

type Options struct {
Expand All @@ -20,7 +19,7 @@ type Options struct {
MergedBefore time.Time
}

func GenUserArtifactsToDir(client github.Client, opts Options) ([]string, error) {
func GenUserArtifactsToDir(client github.Client, opts Options) ([]*gh.PullRequest, error) {
filters := []github.FilterFunc{github.FilterPRsByMergedAt}
if opts.WithClosed {
filters = append(filters, github.FilterPRsByClosedAt)
Expand All @@ -41,7 +40,7 @@ func GenUserArtifactsToDir(client github.Client, opts Options) ([]string, error)
return nil, err
}

return prsToStringList(prs), nil
return prs, nil
}

func savePRsDiffToFiles(client github.Client, prs []*gh.PullRequest, opts Options) error {
Expand All @@ -53,7 +52,7 @@ func savePRsDiffToFiles(client github.Client, prs []*gh.PullRequest, opts Option
}

if diff != "" {
filename := fmt.Sprintf("%s_%s_%s.diff", opts.Org, opts.Repo, cutSHA(pr.GetMergeCommitSHA()))
filename := file.BuildDiffFilename(pr, opts.Org, opts.Repo)
err = file.Create(opts.Dir, filename, diff)
if err != nil {
return fmt.Errorf("save file '%s' error: %s", filename, err.Error())
Expand All @@ -63,32 +62,3 @@ func savePRsDiffToFiles(client github.Client, prs []*gh.PullRequest, opts Option

return nil
}

func cutSHA(fullSHA string) string {
if len(fullSHA) < 8 {
return fullSHA
}

return fullSHA[0:8]
}

func prsToStringList(prs []*gh.PullRequest) []string {
list := []string{}
for i := range prs {
pr := *prs[i]

title := fmt.Sprintf(" %s (#%d) %s", getStatePrefix(pr), pr.GetNumber(), pr.GetTitle())
list = append(list, title)
}

return list
}

func getStatePrefix(pr gh.PullRequest) string {
// pull request - 
if !pr.GetMergedAt().IsZero() {
return pterm.Magenta("[M]")
}

return pterm.Red("[C]")
}
9 changes: 1 addition & 8 deletions pkg/artifacts/artifacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package artifacts

import (
"errors"
"fmt"
"os"
"path"
"testing"
Expand All @@ -11,7 +10,6 @@ import (
gh "github.com/google/go-github/v53/github"
"github.com/pPrecel/PKUP/pkg/github"
"github.com/pPrecel/PKUP/pkg/github/automock"
"github.com/pterm/pterm"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -57,12 +55,7 @@ func TestGenUserArtifactsToDir(t *testing.T) {
})

require.NoError(t, err)

expectedPRs := []string{
fmt.Sprint(" ", pterm.Red("[C]"), " (#123) test PR 1"),
fmt.Sprint(" ", pterm.Magenta("[M]"), " (#124) test PR 2"),
}
require.ElementsMatch(t, expectedPRs, prs)
require.ElementsMatch(t, testPRs, prs)

expectedDiffFile := path.Join(tmpDir, "test-org_test-repo_sha1.diff")
require.FileExists(t, expectedDiffFile)
Expand Down
Loading

0 comments on commit e8267a2

Please sign in to comment.