Skip to content

Commit

Permalink
Merge pull request #17 from odair-pedro/feature/refactory-sonarrestapi
Browse files Browse the repository at this point in the history
Support to Sonar version 6.x, 7.x and 8.x
  • Loading branch information
odair-pedro authored Oct 20, 2020
2 parents 34a8fb9 + 8ca2de8 commit c3f5607
Show file tree
Hide file tree
Showing 43 changed files with 588 additions and 235 deletions.
37 changes: 20 additions & 17 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package cmd
import (
"fmt"
"github.com/spf13/cobra"
connFactory "sonarci/net/factory"
"sonarci/sonar"
sonarFactory "sonarci/sonar/factory"
"time"
)

const (
Expand All @@ -12,29 +16,28 @@ const (
timeoutDefault = 30000
)

var rootCmd = &cobra.Command{
Use: "sonarci",
Short: "A simple tool for SonarQube integration",
Long: "SonarCI is a CLI library for help you integrate and use SonarQube inspections.",
}

func Execute() error {
return rootCmd.Execute()
}

func SetVersion(version string) {
rootCmd.Version = version
}
func NewRootCmd() *cobra.Command {
rootCmd := &cobra.Command{
Use: "sonarci",
Short: "A simple tool for SonarQube integration",
Long: "SonarCI is a CLI library for help you integrate and use SonarQube inspections.",
}

func init() {
rootCmd.PersistentFlags().StringP(flagServer, "s", "", "SonarQube server address")
rootCmd.PersistentFlags().StringP(flagToken, "o", "", "Authentication Token")
rootCmd.PersistentFlags().IntP(flagTimout, "t", 0, fmt.Sprintf("Timeout in milliseconds. Default value is %d ms", timeoutDefault))

_ = rootCmd.MarkPersistentFlagRequired(flagServer)
_ = rootCmd.MarkPersistentFlagRequired(flagToken)

rootCmd.AddCommand(serverVersionCmd)
rootCmd.AddCommand(searchCmd)
rootCmd.AddCommand(validateCmd)
rootCmd.AddCommand(NewServerVersionCmd())
rootCmd.AddCommand(NewSearchCmd())
rootCmd.AddCommand(NewValidateCmd())

return rootCmd
}

func createSonarApi(server string, token string, timeout time.Duration) sonar.Api {
conn := connFactory.CreateConnection(server, token, timeout)
return sonarFactory.CreateSonarApi(conn)
}
9 changes: 4 additions & 5 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"testing"
)

func TestSetVersion(t *testing.T) {
const version = "1.0"
SetVersion(version)
if rootCmd.Version != version {
t.Errorf("Version %s has not been defined", version)
func Test_NewRootCmd_CheckReturn(t *testing.T) {
cmd := NewRootCmd()
if cmd == nil {
t.Errorf("NewRootCmd() = nil")
}
}
24 changes: 13 additions & 11 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,30 @@ package cmd
import (
"github.com/spf13/cobra"
"log"
"sonarci/sonar/sonarrestapi"
"strings"
)

const (
flagProjects = "projects"
)

var searchCmd = &cobra.Command{
Use: "search",
Short: "Search for SonarQube projects",
Long: "Search and retrieve information about the specified SonarQube projects.",
Run: search,
}
func NewSearchCmd() *cobra.Command {
searchCmd := &cobra.Command{
Use: "search",
Short: "Search for SonarQube projects",
Long: "Search and retrieve information about the specified SonarQube projects.",
Run: search,
}

func init() {
searchCmd.Flags().StringP(flagProjects, "p", "", "SonarQube projects key. Eg: my-sonar-project | my-sonar-project-1,my-sonar-project-2")
_ = searchCmd.MarkFlagRequired(flagProjects)

return searchCmd
}

func search(cmd *cobra.Command, args []string) {
_ = args

projects, _ := cmd.Flags().GetString(flagProjects)
if !validateFlag(flagProjects, projects) {
return
Expand All @@ -35,7 +37,7 @@ func search(cmd *cobra.Command, args []string) {
return
}

api := sonarrestapi.NewApi(pFlags.Server, pFlags.Token, pFlags.Timeout)
api := createSonarApi(pFlags.Server, pFlags.Token, pFlags.Timeout)
results, err := api.SearchProjects(projects)
if err != nil {
log.Fatal("Failure to search projects: ", err)
Expand All @@ -52,6 +54,6 @@ func search(cmd *cobra.Command, args []string) {
}
}

func padRight(str string, suffix string, count int) string {
return str + strings.Repeat(suffix, count-len(str))
func padRight(str string, suffix string, length int) string {
return (str + strings.Repeat(suffix, length-len(str)))[:length]
}
35 changes: 35 additions & 0 deletions cmd/search_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import "testing"

func Test_padRight(t *testing.T) {
type args struct {
str string
suffix string
length int
}
tests := []struct {
name string
args args
want string
}{
{"test-0", args{"test", "0", 10}, "test000000"},
{"test-01", args{"test", "01", 10}, "test010101"},
{"test-010", args{"test", "010", 10}, "test010010"},
{"test-01010101", args{"test", "01010101", 10}, "test010101"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := padRight(tt.args.str, tt.args.suffix, tt.args.length); got != tt.want {
t.Errorf("padRight() = %v, want %v", got, tt.want)
}
})
}
}

func Test_NewSearchCmd_CheckReturn(t *testing.T) {
cmd := NewSearchCmd()
if cmd == nil {
t.Errorf("NewSearchCmd() = nil")
}
}
17 changes: 10 additions & 7 deletions cmd/serverversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ package cmd
import (
"github.com/spf13/cobra"
"log"
"sonarci/sonar/sonarrestapi"
)

var serverVersionCmd = &cobra.Command{
Use: "server-version",
Short: "Get SonarQube server version",
Long: "Get SonarQube server version",
Run: serverVersion,
func NewServerVersionCmd() *cobra.Command {
serverVersionCmd := &cobra.Command{
Use: "server-version",
Short: "Get SonarQube server version",
Long: "Get SonarQube server version",
Run: serverVersion,
}

return serverVersionCmd
}

func serverVersion(cmd *cobra.Command, args []string) {
Expand All @@ -21,7 +24,7 @@ func serverVersion(cmd *cobra.Command, args []string) {
return
}

api := sonarrestapi.NewApi(pFlags.Server, pFlags.Token, pFlags.Timeout)
api := createSonarApi(pFlags.Server, pFlags.Token, pFlags.Timeout)
version, err := api.GetServerVersion()
if err != nil {
log.Fatal("Failure to get server version: ", err)
Expand Down
12 changes: 12 additions & 0 deletions cmd/serverversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmd

import (
"testing"
)

func Test_NewServerVersionCmd_CheckReturn(t *testing.T) {
cmd := NewServerVersionCmd()
if cmd == nil {
t.Errorf("NewServerVersionCmd() = nil")
}
}
57 changes: 33 additions & 24 deletions cmd/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"github.com/spf13/cobra"
"log"
"sonarci/sonar/sonarrestapi"
)

const (
Expand All @@ -12,37 +11,47 @@ const (
flagProjectUsage = "SonarQube projects key"
)

var validateCmd = &cobra.Command{
Use: "validate",
Short: "Validate quality gate status",
Long: "Validate a branch or pull request status on SonarQube.",
}
func NewValidateCmd() *cobra.Command {
validateCmd := &cobra.Command{
Use: "validate",
Short: "Validate quality gate status",
Long: "Validate a branch or pull request status on SonarQube.",
}

var branchCmd = &cobra.Command{
Use: "branch [branch name]",
Short: "Validate branch status",
Long: "Validate a branch status on SonarQube.",
Args: cobra.MinimumNArgs(1),
Run: validateBranch,
}
validateCmd.AddCommand(newBranchCmd())
validateCmd.AddCommand(newPullRequestCmd())

var pullRequestCmd = &cobra.Command{
Use: "pr [pull request id]",
Short: "Validate pull request status",
Long: "Validate a pull request status on SonarQube.",
Args: cobra.MinimumNArgs(1),
Run: validatePullRequest,
return validateCmd
}

func init() {
func newBranchCmd() *cobra.Command {
branchCmd := &cobra.Command{
Use: "branch [branch name]",
Short: "Validate branch status",
Long: "Validate a branch status on SonarQube.",
Args: cobra.MinimumNArgs(1),
Run: validateBranch,
}

branchCmd.Flags().StringP(flagProject, flagProjectShort, "", flagProjectUsage)
_ = branchCmd.MarkFlagRequired(flagProject)

return branchCmd
}

func newPullRequestCmd() *cobra.Command {
pullRequestCmd := &cobra.Command{
Use: "pr [pull request id]",
Short: "Validate pull request status",
Long: "Validate a pull request status on SonarQube.",
Args: cobra.MinimumNArgs(1),
Run: validatePullRequest,
}

pullRequestCmd.Flags().StringP(flagProject, flagProjectShort, "", flagProjectUsage)
_ = pullRequestCmd.MarkFlagRequired(flagProject)

validateCmd.AddCommand(branchCmd)
validateCmd.AddCommand(pullRequestCmd)
return pullRequestCmd
}

func validateBranch(cmd *cobra.Command, args []string) {
Expand All @@ -58,7 +67,7 @@ func validateBranch(cmd *cobra.Command, args []string) {
return
}

api := sonarrestapi.NewApi(pFlags.Server, pFlags.Token, pFlags.Timeout)
api := createSonarApi(pFlags.Server, pFlags.Token, pFlags.Timeout)
err := api.ValidateBranch(project, branch)
if err != nil {
log.Fatal(err)
Expand All @@ -78,7 +87,7 @@ func validatePullRequest(cmd *cobra.Command, args []string) {
return
}

api := sonarrestapi.NewApi(pFlags.Server, pFlags.Token, pFlags.Timeout)
api := createSonarApi(pFlags.Server, pFlags.Token, pFlags.Timeout)
err := api.ValidatePullRequest(project, pr)
if err != nil {
log.Fatal(err)
Expand Down
12 changes: 12 additions & 0 deletions cmd/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cmd

import (
"testing"
)

func Test_NewValidateCmd_CheckReturn(t *testing.T) {
cmd := NewValidateCmd()
if cmd == nil {
t.Errorf("NewValidateCmd() = nil")
}
}
13 changes: 13 additions & 0 deletions logging/log_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package logging

import (
"log"
"testing"
)

func Test_Setup_CheckFlags(t *testing.T) {
Setup()
if log.Flags() != 0 {
t.Error("Setup() not set flas to 0")
}
}
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ func main() {
}

func startRootCommand() {
cmd.SetVersion(version)
if err := cmd.Execute(); err != nil {
rootCmd := cmd.NewRootCmd()
rootCmd.Version = version
if err := rootCmd.Execute(); err != nil {
log.Fatalln(err.Error())
}
}
1 change: 1 addition & 0 deletions net/connection.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net

type Connection interface {
GetHostServer() string
DoGet(route string) (<-chan []byte, <-chan error)
}
11 changes: 11 additions & 0 deletions net/factory/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package factory

import (
"sonarci/net"
"sonarci/net/http"
"time"
)

func CreateConnection(server string, token string, timeout time.Duration) net.Connection {
return http.NewConnection(server, token, timeout)
}
18 changes: 18 additions & 0 deletions net/factory/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package factory

import (
"sonarci/net/http"
"testing"
"time"
)

func Test_CreateConnection_CheckReturn(t *testing.T) {
api := CreateConnection("server", "token", time.Second)

switch tp := api.(type) {
case *http.Connection:
return
default:
t.Errorf("Invalid returned type: %T", tp)
}
}
Loading

0 comments on commit c3f5607

Please sign in to comment.