From 008bc223b576d150a748acda867b4426c84bc369 Mon Sep 17 00:00:00 2001 From: Dan Rollo Date: Wed, 27 Jan 2021 16:16:03 -0500 Subject: [PATCH] use AbsoluteReportHTMLURL to handle relative report url from IQ 104+ (#67) * use AbsoluteReportHTMLURL to handle relative report url from IQ 104+ --- cmd/iq.go | 28 ++++++++++++++++++++-------- cmd/iq_test.go | 21 +++++++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/cmd/iq.go b/cmd/iq.go index 11aae55..109eb92 100644 --- a/cmd/iq.go +++ b/cmd/iq.go @@ -23,6 +23,7 @@ import ( "github.com/sonatype-nexus-community/go-sona-types/configuration" "github.com/sonatype-nexus-community/go-sona-types/ossindex/types" "github.com/spf13/viper" + "io" "os" "path" @@ -170,21 +171,32 @@ var iqCmd = &cobra.Command{ panic(fmt.Errorf("Uh oh! There was an error with your request to Nexus IQ Server")) } - if res.PolicyAction == "Failure" { - logLady.WithField("res", res).Debug("Successful in communicating with IQ Server") - fmt.Println("Ahoy, Ahab here matey, avast ye work, ye have some policy violations to clean up!") - fmt.Println("Report URL: ", res.ReportHTMLURL) + showPolicyActionMessage(res, os.Stdout) + if res.PolicyAction == iq.PolicyActionFailure { os.Exit(1) return } - - logLady.WithField("res", res).Debug("Successful in communicating with IQ Server") - fmt.Println("Wonderbar! No policy violations reported for this audit!") - fmt.Println("Report URL: ", res.ReportHTMLURL) return }, } +func showPolicyActionMessage(res iq.StatusURLResult, writer io.Writer) { + switch res.PolicyAction { + case iq.PolicyActionFailure: + logLady.WithField("res", res).Debug("Successful in communicating with IQ Server") + _, _ = fmt.Fprintln(writer, "Ahoy, Ahab here matey, avast ye work, ye have some policy violations to clean up!") + _, _ = fmt.Fprintln(writer, "Report URL: ", res.AbsoluteReportHTMLURL) + case iq.PolicyActionWarning: + logLady.WithField("res", res).Debug("Successful in communicating with IQ Server") + _, _ = fmt.Fprintln(writer, "A shot across the bow, there be policy warnings!") + _, _ = fmt.Fprintln(writer, "Report URL: ", res.AbsoluteReportHTMLURL) + default: + logLady.WithField("res", res).Debug("Successful in communicating with IQ Server") + _, _ = fmt.Fprintln(writer, "Wonderbar! No policy violations reported for this audit!") + _, _ = fmt.Fprintln(writer, "Report URL: ", res.AbsoluteReportHTMLURL) + } +} + func bindViperIq(cmd *cobra.Command) { // need to defer bind call until command is run. see: https://github.com/spf13/viper/issues/233 diff --git a/cmd/iq_test.go b/cmd/iq_test.go index 58f9530..ad2e070 100644 --- a/cmd/iq_test.go +++ b/cmd/iq_test.go @@ -17,14 +17,19 @@ package cmd import ( + "bufio" + "bytes" "fmt" + "github.com/sirupsen/logrus/hooks/test" "github.com/sonatype-nexus-community/go-sona-types/configuration" + "github.com/sonatype-nexus-community/go-sona-types/iq" "github.com/sonatype-nexus-community/go-sona-types/ossindex/types" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "io/ioutil" "os" "path" + "strings" "testing" ) @@ -126,3 +131,19 @@ func TestInitIQConfigWithNoConfigFile(t *testing.T) { assert.Equal(t, "", viper.GetString(configuration.ViperKeyIQToken)) assert.Equal(t, "", viper.GetString(configuration.ViperKeyIQServer)) } + +func Test_showPolicyActionMessage(t *testing.T) { + logLady, _ = test.NewNullLogger() + verifyReportURL(t, "anythingElse") //default policy action + verifyReportURL(t, iq.PolicyActionWarning) + verifyReportURL(t, iq.PolicyActionFailure) +} + +func verifyReportURL(t *testing.T, policyAction string) { + var buf bytes.Buffer + bufWriter := bufio.NewWriter(&buf) + theURL := "someURL" + showPolicyActionMessage(iq.StatusURLResult{AbsoluteReportHTMLURL: theURL, PolicyAction: policyAction}, bufWriter) + bufWriter.Flush() + assert.True(t, strings.Contains(buf.String(), "Report URL: "+theURL), buf.String()) +} diff --git a/go.mod b/go.mod index aa39289..a34064c 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/shopspring/decimal v1.2.0 github.com/sirupsen/logrus v1.7.0 - github.com/sonatype-nexus-community/go-sona-types v0.0.8 + github.com/sonatype-nexus-community/go-sona-types v0.0.10 github.com/spf13/cobra v1.0.0 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.7.1 diff --git a/go.sum b/go.sum index 49b18f2..74f408d 100644 --- a/go.sum +++ b/go.sum @@ -193,8 +193,8 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1 github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= -github.com/sonatype-nexus-community/go-sona-types v0.0.8 h1:6xb9BIC2w3y4kF/xQA25Zs6Yrl8ZqFkfH77AKPaG4RA= -github.com/sonatype-nexus-community/go-sona-types v0.0.8/go.mod h1:uou8FGf9R5Nz1c6BfSM3v9K7g0R6faTYoxLh9Ybeht8= +github.com/sonatype-nexus-community/go-sona-types v0.0.10 h1:VW+OE1vAjBwwRCz7jz4xcBbUn1j3bz1gFnEw1YYmYGs= +github.com/sonatype-nexus-community/go-sona-types v0.0.10/go.mod h1:uou8FGf9R5Nz1c6BfSM3v9K7g0R6faTYoxLh9Ybeht8= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.1.2 h1:m8/z1t7/fwjysjQRYbP0RD+bUIF/8tJwPdEZsI83ACI= github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=