Skip to content

Commit

Permalink
add new --report-field flag
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed Oct 27, 2023
1 parent 6b35513 commit ce5f3f0
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,19 @@ For more go to the [examples](https://github.com/pPrecel/pkup-gen/tree/main/exam

By default the `pkup-gen` generates the `report.txt` files with all info needed to fill true report.

The `.docx` report template can be specified using the `--template` flag. The `pkup-gen` will replace any repeat of the following key-words with the tru data:
The `.docx` report template can be specified using the `--template` flag. The `pkup-gen` will replace any repeat of the following key-words with the true data:

* `pkupGenPeriodFrom` - date of the first day for the actual period
* `pkupGenPeriodTill` - date of the last day for the actual period
* `pkupGenApprovalDate` - date of the last day of the period plus one day
* `pkupGenResults` - list of all PullRequests if format <PR_TITLE>( DIFF_FILE_NAME )

The `pkup-gen` allows to add new formula to replace in the output `.docx` file. It can be achieved by adding flag `--report-field` like in the following example:

```bash
--report-field "pkupGenEmployeesName=John Wick"
```

## Access Token

The `pkup-gen` needs credentials to connect with the GitHub API. There are two possible ways to pass such credentials:
Expand Down
1 change: 1 addition & 0 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func genCommandAction(ctx *cli.Context, opts *genActionOpts) error {
PeriodFrom: *opts.since.Value(),
PeriodTill: *opts.until.Value(),
Results: reportResults,
CustomValues: opts.reportFields,
})
if err != nil {
return err
Expand Down
23 changes: 23 additions & 0 deletions cmd/gen_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ func getGenFlags(opts *genActionOpts) []cli.Flag {
return nil
},
},
&cli.StringSliceFlag{
Name: "report-field",
Usage: "custom field that will be replace in the output report - in format FIELD=VALUE",
Action: func(_ *cli.Context, fields []string) error {
reportFields, err := parseReportFields(fields)
opts.reportFields = reportFields
return err
},
},
&cli.BoolFlag{
Name: "ci",
Usage: "print output using standard log and JSON format",
Expand Down Expand Up @@ -156,6 +165,20 @@ func getGenFlags(opts *genActionOpts) []cli.Flag {
}
}

func parseReportFields(args []string) (map[string]string, error) {
reportFields := map[string]string{}
for _, field := range args {
vals := strings.Split(field, "=")
if len(vals) != 2 {
return nil, fmt.Errorf("failed to parse '%s' report field", field)
}

reportFields[vals[0]] = vals[1]
}

return reportFields, nil
}

func parseReposMap(log *pterm.Logger, args []string) (map[string][]string, error) {
repos := map[string][]string{}
for i := range args {
Expand Down
1 change: 1 addition & 0 deletions cmd/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type genActionOpts struct {
enterpriseURL string
templatePath string
repos map[string][]string
reportFields map[string]string
ci bool
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Options struct {
PeriodFrom time.Time
PeriodTill time.Time
Results []Result
CustomValues map[string]string
}

func Render(opts Options) error {
Expand All @@ -34,6 +35,7 @@ func Render(opts Options) error {
PeriodTill: opts.PeriodTill.Format(PeriodFormat),
ApprovalDate: opts.PeriodTill.Add(time.Hour * 24).Format(PeriodFormat),
Result: buildreportResult(opts),
CustomValues: opts.CustomValues,
}

if opts.TemplatePath != "" {
Expand Down
5 changes: 5 additions & 0 deletions pkg/report/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Values struct {
PeriodTill string
ApprovalDate string
Result []string
CustomValues map[string]string
}

type templateRenderer struct {
Expand Down Expand Up @@ -48,5 +49,9 @@ func (tr *templateRenderer) RenderToFile(dir, filename string, values Values) er
docx1.Replace(DocxApprovalDateTmpl, values.ApprovalDate, -1)
docx1.Replace(DocxResultsTmpl, resultString, -1)

for tmpl, val := range values.CustomValues {
docx1.Replace(tmpl, val, -1)
}

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

0 comments on commit ce5f3f0

Please sign in to comment.