-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
168 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ tmp/ | |
.vscode/ | ||
|
||
# development | ||
report | ||
templates | ||
reports/ | ||
templates/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |