Skip to content

Commit

Permalink
feat(log): format non-verbose result in table-style format (#87)
Browse files Browse the repository at this point in the history
* feat(log): format non-verbose result in table-style format

* feat: use default logger for logging result

* test: write unit test for new console logger
  • Loading branch information
Namchee authored Jun 16, 2023
1 parent d513947 commit 15e7836
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor
19 changes: 10 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
module github.com/Namchee/conventional-pr

go 1.16

require (
github.com/google/go-github/v32 v32.1.0
github.com/stretchr/testify v1.7.0
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
)
module github.com/Namchee/conventional-pr

go 1.16

require (
github.com/google/go-github/v32 v32.1.0
github.com/jedib0t/go-pretty/v6 v6.4.6 // indirect
github.com/stretchr/testify v1.7.4
golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDk
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -102,6 +104,8 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jedib0t/go-pretty/v6 v6.4.6 h1:v6aG9h6Uby3IusSSEjHaZNXpHFhzqMmjXcPq1Rjl9Jw=
github.com/jedib0t/go-pretty/v6 v6.4.6/go.mod h1:Ndk3ase2CkQbXLLNf5QDHoYb6J9WtVfmHZu9n8rk2xs=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
Expand All @@ -110,14 +114,23 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.4 h1:wZRexSlwd7ZXfKINDLsO4r7WBt3gTKONc6K/VesHvHM=
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -229,6 +242,8 @@ golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -364,6 +379,8 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
12 changes: 6 additions & 6 deletions internal/constants/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ var (
ValidationPass = "Pull request satisfies all enabled pull request rules."
ValidationFail = "Pull request is invalid."

LogHeader = "Pull request validation report"

ActiveLabel = "ACTIVE"
InactiveLabel = "INACTIVE"
PassLabel = "PASS"
FailLabel = "FAIL"
LogHeader = `
____ _ _ _ ____ ____
/ ___| ___ _ ____ _____ _ __ | |_(_) ___ _ __ __ _| | _ \| _ \
| | / _ \| '_ \ \ / / _ \ '_ \| __| |/ _ \| '_ \ / _ || | |_) | |_) |
| |__| (_) | | | \ V / __/ | | | |_| | (_) | | | | (_||| | __/| _ <
\____ \___/|_| |_|\_/ \___|_| |_|\__|_|\___/|_| |_|\__,_|_|_| |_| \_\`
)

// Emojis
Expand Down
92 changes: 62 additions & 30 deletions internal/formatter/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,79 @@ package formatter

import (
"fmt"
"log"
"strings"

"github.com/Namchee/conventional-pr/internal/constants"
"github.com/Namchee/conventional-pr/internal/entity"
"github.com/Namchee/conventional-pr/internal/utils"
"github.com/jedib0t/go-pretty/v6/table"
)

func formatWhitelistResultToConsole(
whitelistResults []*entity.WhitelistResult,
logger *log.Logger,
) {
) string {

t := table.NewWriter()

t.SetStyle(table.StyleLight)
t.SetTitle("Whitelist Result")
t.AppendHeader(table.Row{"Whitelist", "Active", "Result"})

flag := false

for _, r := range whitelistResults {
active := constants.InactiveLabel
verdict := constants.FailLabel
active := constants.FailEmoji
verdict := constants.FailEmoji

if r.Active {
active = constants.ActiveLabel
active = constants.PassEmoji
}

if r.Result {
flag = true
verdict = constants.PassLabel
verdict = constants.PassEmoji
}

logger.Printf("%s — %s — %s\n", r.Name, active, verdict)
t.AppendRow(table.Row{r.Name, active, verdict})
}

summary := constants.WhitelistFail
if flag {
summary = constants.WhitelistPass
}

logger.Println(summary)
return fmt.Sprintf(
"%s\n\nResult: %s",
t.Render(),
summary,
)
}

func formatValidationResultToConsole(
validationResults []*entity.ValidationResult,
logger *log.Logger,
) {
) string {
var errors []error

t := table.NewWriter()

t.SetStyle(table.StyleLight)
t.SetTitle("Validation Result")
t.AppendHeader(table.Row{"Validation", "Active", "Result"})

for _, r := range validationResults {
active := constants.ActiveLabel
verdict := constants.PassLabel
active := constants.PassEmoji
verdict := constants.PassEmoji

if !r.Active {
active = constants.InactiveLabel
active = constants.FailEmoji
}

if r.Result != nil {
errors = append(errors, r.Result)
verdict = constants.FailLabel
verdict = constants.FailEmoji
}

logger.Printf("%s — %s — %s\n", r.Name, active, verdict)
t.AppendRow(table.Row{r.Name, active, verdict})
}

var reasons []string
Expand All @@ -72,24 +88,40 @@ func formatValidationResultToConsole(
}
}

logger.Printf("Result: %s\n", verdict)

for _, reason := range reasons {
logger.Println(reason)
report := fmt.Sprintf(
"%s\n\nResult: %s",
t.Render(),
verdict,
)

if len(reasons) > 0 {
report = fmt.Sprintf(
"%s\n\nReason:\n%s",
report,
strings.Join(reasons, "\n"),
)
}

return report
}

// FormatResultToTables formats both whitelist and validation results for workflow reporting to console
func FormatResultToConsole(
whitelistResults []*entity.WhitelistResult,
validationResults []*entity.ValidationResult,
logger *log.Logger,
) {
logger.Println(constants.LogHeader)

formatWhitelistResultToConsole(whitelistResults, logger)

if len(validationResults) > 0 {
formatValidationResultToConsole(validationResults, logger)
results *entity.PullRequestResult,
) string {
report := fmt.Sprintf(
"%s\n\n%s",
constants.LogHeader,
formatWhitelistResultToConsole(results.Whitelist),
)

if len(results.Validation) > 0 {
report = fmt.Sprintf(
"%s\n\n%s",
report,
formatValidationResultToConsole(results.Validation),
)
}

return report
}
Loading

0 comments on commit 15e7836

Please sign in to comment.