Skip to content

Commit 211f5d6

Browse files
reduced duplicated lines
1 parent 2fac764 commit 211f5d6

File tree

1 file changed

+10
-175
lines changed

1 file changed

+10
-175
lines changed

plugin.go

Lines changed: 10 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,6 @@ type (
154154
ActualValue string `json:"actualValue"`
155155
}
156156

157-
// type Period struct {
158-
// Index int `json:"index"`
159-
// Mode string `json:"mode"`
160-
// Date string `json:"date"`
161-
// Parameter string `json:"parameter,omitempty"` // this might not always be present
162-
// }
163-
164157
Testsuites struct {
165158
XMLName xml.Name `xml:"testsuites"`
166159
Text string `xml:",chardata"`
@@ -273,20 +266,6 @@ func displaySummary(total, passed, failed int, errors int, newErrors int, projec
273266
fmt.Println("Error writing to .env file:", err)
274267
// return
275268
}
276-
// file, err := os.OpenFile(filePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
277-
// if err != nil {
278-
// fmt.Println("Error opening/creating .env file:", err)
279-
// // return
280-
// }
281-
282-
// for key, value := range vars {
283-
// fmt.Println("Writing to .env file:", key, value)
284-
// _, err = file.WriteString(fmt.Sprintf("%s=%s\n", key, value))
285-
// if err != nil {
286-
// fmt.Println("Error writing to .env file:", err)
287-
// // return
288-
// }
289-
// }
290269

291270
fmt.Println("Successfully wrote to .env file")
292271
// defer file.Close()
@@ -398,19 +377,22 @@ func GetProjectKey(key string) string {
398377
return projectKey
399378
}
400379

401-
func PreFlightGetLatestTaskID(config Config) (string, error) {
402-
// token := os.Getenv("PLUGIN_SONAR_TOKEN")
380+
func logConfigInfo(configType, configValue string) {
381+
fmt.Printf("==> %s: %s\n", configType, configValue)
382+
}
403383

384+
func PreFlightGetLatestTaskID(config Config) (string, error) {
404385
var statusID string
405386
var err error
387+
406388
if config.PRKey != "" {
407-
fmt.Printf("==> PR Key: " + config.PRKey + "\n")
389+
logConfigInfo("PR Key", config.PRKey)
408390
statusID, err = getStatusV2("pr", config.PRKey, config.Host, config.Key)
409391
} else if config.Branch != "" {
410-
fmt.Printf("==> Branch: " + config.Branch + "\n")
392+
logConfigInfo("Branch", config.Branch)
411393
statusID, err = getStatusV2("branch", config.Branch, config.Host, config.Key)
412394
} else {
413-
fmt.Printf("==> Project Key: " + config.Key + "\n")
395+
logConfigInfo("Project Key", config.Key)
414396
statusID, err = getStatusID(config.TaskId, config.Host, config.Key)
415397
}
416398

@@ -496,113 +478,6 @@ func (p Plugin) Exec() error {
496478
fmt.Printf("\n\n==> Sonar Java Plugin Jacoco Path configured!\n\n")
497479
}
498480

499-
// args := []string{
500-
// "-Dsonar.host.url=" + p.Config.Host,
501-
// "-Dsonar.login=" + p.Config.Token,
502-
// }
503-
// projectFinalKey := p.Config.Key
504-
505-
// if len(p.Config.Verbose) >= 1 {
506-
// args = append(args, "-X")
507-
// }
508-
509-
// if !p.Config.UsingProperties {
510-
// argsParameter := []string{
511-
// "-Dsonar.projectKey=" + projectFinalKey,
512-
// "-Dsonar.projectName=" + p.Config.Name,
513-
// "-Dsonar.projectVersion=" + p.Config.Version,
514-
// "-Dsonar.sources=" + p.Config.Sources,
515-
// "-Dsonar.ws.timeout=" + p.Config.Timeout,
516-
// "-Dsonar.inclusions=" + p.Config.Inclusions,
517-
// "-Dsonar.exclusions=" + p.Config.Exclusions,
518-
// "-Dsonar.log.level=" + p.Config.Level,
519-
// "-Dsonar.showProfiling=" + p.Config.ShowProfiling,
520-
// "-Dsonar.scm.provider=git",
521-
// "-Dsonar.java.binaries=" + p.Config.Binaries,
522-
// }
523-
// args = append(args, argsParameter...)
524-
// }
525-
// if p.Config.BranchAnalysis {
526-
// args = append(args, "-Dsonar.branch.name="+p.Config.Branch)
527-
// }
528-
// if p.Config.QualityEnabled == "true" {
529-
// args = append(args, "-Dsonar.qualitygate.wait="+p.Config.QualityEnabled)
530-
// args = append(args, "-Dsonar.qualitygate.timeout="+p.Config.QualityTimeout)
531-
// }
532-
// if len(p.Config.JavascitptIcovReport) >= 1 {
533-
// args = append(args, "-Dsonar.javascript.lcov.reportPaths="+p.Config.JavascitptIcovReport)
534-
// }
535-
// if len(p.Config.JacocoReportPath) >= 1 {
536-
// args = append(args, "-Dsonar.coverage.jacoco.xmlReportPaths="+p.Config.JacocoReportPath)
537-
// fmt.Printf("\n\n==> Sonar Java Plugin Jacoco configured!\n\n")
538-
// fmt.Printf("\n\n==> -Dsonar.coverage.jacoco.xmlReportPaths=" + p.Config.JacocoReportPath + "\n\n")
539-
// }
540-
// if len(p.Config.JavaCoveragePlugin) >= 1 {
541-
// args = append(args, "-Dsonar.java.coveragePlugin="+p.Config.JavaCoveragePlugin)
542-
// fmt.Printf("\n\n==> Sonar Java Plugin Jacoco Path configured!\n\n")
543-
// }
544-
// if len(p.Config.JunitReportPaths) >= 1 {
545-
// args = append(args, "-Dsonar.junit.reportPaths="+p.Config.JunitReportPaths)
546-
// }
547-
// if len(p.Config.SourceEncoding) >= 1 {
548-
// args = append(args, "-Dsonar.sourceEncoding="+p.Config.SourceEncoding)
549-
// }
550-
// if len(p.Config.SonarTests) >= 1 {
551-
// args = append(args, "-Dsonar.tests="+p.Config.SonarTests)
552-
// }
553-
// if len(p.Config.JavaTest) >= 1 {
554-
// args = append(args, "-Dsonar.java.test.binaries="+p.Config.JavaTest)
555-
// }
556-
// if len(p.Config.CoverageExclusion) >= 1 {
557-
// args = append(args, "-Dsonar.coverage.exclusions="+p.Config.CoverageExclusion)
558-
// }
559-
// if len(p.Config.JavaSource) >= 1 {
560-
// args = append(args, "-Dsonar.java.source="+p.Config.JavaSource)
561-
// }
562-
// if len(p.Config.JavaLibraries) >= 1 {
563-
// args = append(args, "-Dsonar.java.libraries="+p.Config.JavaLibraries)
564-
// }
565-
// if len(p.Config.SurefireReportsPath) >= 1 {
566-
// args = append(args, "-Dsonar.surefire.reportsPath="+p.Config.SurefireReportsPath)
567-
// }
568-
// if len(p.Config.TypescriptLcovReportPaths) >= 1 {
569-
// args = append(args, "-Dsonar.sonar.typescript.lcov.reportPaths="+p.Config.TypescriptLcovReportPaths)
570-
// }
571-
// if len(p.Config.Verbose) >= 1 {
572-
// args = append(args, "-Dsonar.verbose="+p.Config.Verbose)
573-
// }
574-
575-
// if len(p.Config.CustomJvmParams) >= 1 {
576-
577-
// params := strings.Split(p.Config.CustomJvmParams, ",")
578-
579-
// for _, param := range params {
580-
// //fmt.Println(i, param)
581-
// args = append(args, param)
582-
// }
583-
584-
// }
585-
586-
// if len(p.Config.PRKey) >= 1 {
587-
// args = append(args, "-Dsonar.pullrequest.key="+p.Config.PRKey)
588-
// }
589-
590-
// if len(p.Config.PRBranch) >= 1 {
591-
// args = append(args, "-Dsonar.pullrequest.branch="+p.Config.PRBranch)
592-
// }
593-
594-
// if len(p.Config.PRBase) >= 1 {
595-
// args = append(args, "-Dsonar.pullrequest.base="+p.Config.PRBase)
596-
// }
597-
598-
// if len(p.Config.SSLKeyStorePassword) >= 1 {
599-
// args = append(args, "-Djavax.net.ssl.trustStorePassword="+p.Config.SSLKeyStorePassword)
600-
// }
601-
602-
// if len(p.Config.CacertsLocation) >= 1 {
603-
// args = append(args, "-Djavax.net.ssl.trustStore="+p.Config.CacertsLocation)
604-
// }
605-
606481
os.Setenv("SONAR_USER_HOME", ".sonar")
607482

608483
fmt.Printf("\n\n")
@@ -624,31 +499,11 @@ func (p Plugin) Exec() error {
624499
fmt.Printf("#######################################\n")
625500
fmt.Printf("Waiting for quality gate validation...\n")
626501
fmt.Printf("#######################################\n")
627-
628-
// statusID, err := getStatusID(p.Config.TaskId, p.Config.Host, p.Config.Key)
629-
// var statusID string
630-
// var err error
631-
// if p.Config.PRKey != "" {
632-
// fmt.Printf("==> PR Key: " + p.Config.PRKey + "\n")
633-
// statusID, err = getStatusV2("pr", p.Config.PRKey, p.Config.Host, p.Config.Key)
634-
// } else if p.Config.Branch != "" {
635-
// fmt.Printf("==> Branch: " + p.Config.Branch + "\n")
636-
// statusID, err = getStatusV2("branch", p.Config.Branch, p.Config.Host, p.Config.Key)
637-
// } else {
638-
// fmt.Printf("==> Project Key: " + p.Config.Key + "\n")
639-
// statusID, err = getStatusID(p.Config.TaskId, p.Config.Host, p.Config.Key)
640-
// }
641-
642-
// if err != nil {
643-
// fmt.Printf("\n\n==> Error getting the latest scanID\n\n")
644-
// fmt.Printf("Error: %s", err.Error())
645-
// return err
646-
// }
647502
var err error
648503
status, err = PreFlightGetLatestTaskID(p.Config)
649504
if err != nil {
650505
fmt.Printf("\n\n==> Error getting the latest scanID\n\n")
651-
fmt.Printf("Error: %s", err.Error())
506+
logConfigInfo("Error", err.Error())
652507
return err
653508
}
654509
} else {
@@ -660,7 +515,7 @@ func (p Plugin) Exec() error {
660515
err := cmd.Run()
661516
if err != nil {
662517
fmt.Printf("\n\n==> Error in Analysis\n\n")
663-
fmt.Printf("Error: %s", err.Error())
518+
logConfigInfo("Error", err.Error())
664519
//return err
665520
}
666521
fmt.Printf("\n==> Sonar Analysis Finished!\n\n")
@@ -897,16 +752,6 @@ func getStatusID(taskIDOld string, sonarHost string, projectSlug string) (string
897752
fmt.Printf("analysisId:" + taskID)
898753
fmt.Printf("\n")
899754

900-
// projectRequest, err := http.NewRequest("GET", sonarHost+"/api/qualitygates/project_status?"+reportRequest.Encode(), nil)
901-
// projectRequest.Header.Add("Authorization", basicAuth+token)
902-
// projectResponse, err := netClient.Do(projectRequest)
903-
// if err != nil {
904-
// logrus.WithFields(logrus.Fields{
905-
// "error": err,
906-
// }).Fatal("Failed get status")
907-
// return "", err
908-
// }
909-
// buf, _ := ioutil.ReadAll(projectResponse.Body)
910755
buf, err := GetProjectStatus(sonarHost, reportRequest.Encode(), projectSlug)
911756

912757
if err != nil {
@@ -981,16 +826,6 @@ func getStatusV2(scanType string, scanValue string, sonarHost string, projectSlu
981826
fmt.Printf("scanValue:" + scanValue)
982827
fmt.Printf("\n")
983828

984-
// projectRequest, err := http.NewRequest("GET", sonarHost+"/api/qualitygates/project_status?"+reportRequest.Encode(), nil)
985-
// projectRequest.Header.Add("Authorization", basicAuth+token)
986-
// projectResponse, err := netClient.Do(projectRequest)
987-
// if err != nil {
988-
// logrus.WithFields(logrus.Fields{
989-
// "error": err,
990-
// }).Fatal("Failed get status")
991-
// return "", err
992-
// }
993-
// buf, _ := ioutil.ReadAll(projectResponse.Body)
994829
buf, err := GetProjectStatus(sonarHost, reportRequest.Encode(), projectSlug)
995830

996831
if err != nil {

0 commit comments

Comments
 (0)