Skip to content

Commit

Permalink
fix gitignore and add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed Oct 20, 2023
1 parent 1a4b0e4 commit d8afc19
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ tmp/
.vscode/

# development
report
templates
reports/
templates/
47 changes: 47 additions & 0 deletions pkg/report/default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package report

import (
"bytes"
"text/template"

"github.com/pPrecel/PKUP/internal/file"
)

const (
defaultTemplate = `period:
{{.PeriodFrom}} - {{.PeriodTill}}
approvalDate:
{{.ApprovalDate}}
result:
{{range .Result}}
- {{ . -}}
{{end}}
`
)

type defaultRenderer struct {
template string
}

func newDefault() *defaultRenderer {
return &defaultRenderer{
template: defaultTemplate,
}
}

func (dr *defaultRenderer) RenderToFile(dir, filename string, values Values) error {
tmpl, err := template.New(filename).Parse(dr.template)
if err != nil {
return err
}

buf := bytes.NewBuffer(nil)
err = tmpl.Execute(buf, values)
if err != nil {
return err
}

return file.Create(dir, filename, buf.String())
}
67 changes: 67 additions & 0 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package report

import (
"fmt"
"path/filepath"
"time"

gh "github.com/google/go-github/v53/github"
"github.com/pPrecel/PKUP/internal/file"
)

const (
periodFormat = "02.01.2006"
)

type Result struct {
Org string
Repo string
PullRequests []*gh.PullRequest
}

type Options struct {
OutputDir string
TemplatePath string
PeriodFrom time.Time
PeriodTill time.Time
Results []Result
}

func Render(opts Options) error {
values := Values{
PeriodFrom: opts.PeriodFrom.Format(periodFormat),
PeriodTill: opts.PeriodTill.Format(periodFormat),
ApprovalDate: opts.PeriodTill.Add(time.Hour * 24).Format(periodFormat),
Result: buildreportResult(opts),
}

if opts.TemplatePath != "" {
return newFromTemplate(opts.TemplatePath).RenderToFile(
opts.OutputDir,
filepath.Base(opts.TemplatePath),
values,
)
}

return newDefault().RenderToFile(
opts.OutputDir,
"report.txt",
values,
)
}

func buildreportResult(opts Options) []string {
results := []string{}
for _, result := range opts.Results {
for i := range result.PullRequests {
org := result.Org
repo := result.Repo
pr := result.PullRequests[i]
results = append(
results,
fmt.Sprintf("%s (%s)", pr.GetTitle(), file.BuildDiffFilename(pr, org, repo)),
)
}
}
return results
}
52 changes: 52 additions & 0 deletions pkg/report/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package report

import (
"fmt"
"path"

"github.com/nguyenthenguyen/docx"
)

const (
DocxPeriodFromTmpl = "pkupGenPeriodFrom"
DocxPeriodTillTmpl = "pkupGenPeriodTill"
DocxApprovalDateTmpl = "pkupGenApprovalDate"
DocxResultsTmpl = "pkupGenResults"
)

type Values struct {
PeriodFrom string
PeriodTill string
ApprovalDate string
Result []string
}

type templateRenderer struct {
tmplPath string
}

func newFromTemplate(path string) *templateRenderer {
return &templateRenderer{
tmplPath: path,
}
}

func (tr *templateRenderer) RenderToFile(dir, filename string, values Values) error {
r, err := docx.ReadDocxFile(tr.tmplPath)
if err != nil {
return fmt.Errorf("failed to load docx template: %s", err.Error())
}

resultString := ""
for i := range values.Result {
resultString += fmt.Sprintf("- %s\n", values.Result[i])
}

docx1 := r.Editable()
docx1.Replace(DocxPeriodFromTmpl, values.PeriodFrom, -1)
docx1.Replace(DocxPeriodTillTmpl, values.PeriodTill, -1)
docx1.Replace(DocxApprovalDateTmpl, values.ApprovalDate, -1)
docx1.Replace(DocxResultsTmpl, resultString, -1)

return docx1.WriteToFile(path.Join(dir, filename))
}

0 comments on commit d8afc19

Please sign in to comment.