Skip to content

Commit

Permalink
use AbsoluteReportHTMLURL to handle relative report url from IQ 104+ (#…
Browse files Browse the repository at this point in the history
…67)

* use AbsoluteReportHTMLURL to handle relative report url from IQ 104+
  • Loading branch information
bhamail authored Jan 27, 2021
1 parent f544aaf commit 008bc22
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
28 changes: 20 additions & 8 deletions cmd/iq.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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

Expand Down
21 changes: 21 additions & 0 deletions cmd/iq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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())
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 008bc22

Please sign in to comment.