@@ -599,7 +599,21 @@ func (p Plugin) Exec() error {
599599 fmt .Printf ("#######################################\n " )
600600 fmt .Printf ("Waiting for quality gate validation...\n " )
601601 fmt .Printf ("#######################################\n " )
602- statusID , err := getStatusID (p .Config .TaskId , p .Config .Host , p .Config .Key )
602+
603+ // statusID, err := getStatusID(p.Config.TaskId, p.Config.Host, p.Config.Key)
604+ var statusID string
605+ var err error
606+ if p .Config .PRKey != "" {
607+ fmt .Printf ("==> PR Key: " + p .Config .PRKey + "\n " )
608+ statusID , err = getStatusV2 ("pr" , p .Config .PRKey , p .Config .Host , p .Config .Key )
609+ } else if p .Config .Branch != "" {
610+ fmt .Printf ("==> Branch: " + p .Config .Branch + "\n " )
611+ statusID , err = getStatusV2 ("branch" , p .Config .Branch , p .Config .Host , p .Config .Key )
612+ } else {
613+ fmt .Printf ("==> Project Key: " + p .Config .Key + "\n " )
614+ statusID , err = getStatusID (p .Config .TaskId , p .Config .Host , p .Config .Key )
615+ }
616+
603617 if err != nil {
604618 fmt .Printf ("\n \n ==> Error getting the latest scanID\n \n " )
605619 fmt .Printf ("Error: %s" , err .Error ())
@@ -834,6 +848,7 @@ func getStatus(task *TaskResponse, report *SonarReport) string {
834848
835849func getStatusID (taskIDOld string , sonarHost string , projectSlug string ) (string , error ) {
836850 // token := os.Getenv("PLUGIN_SONAR_TOKEN")
851+
837852 taskID , err := GetLatestTaskID (sonarHost , projectSlug )
838853 if err != nil {
839854 fmt .Println ("Failed to get the latest task ID:" , err )
@@ -906,6 +921,90 @@ func getStatusID(taskIDOld string, sonarHost string, projectSlug string) (string
906921 return project .ProjectStatus .Status , nil
907922}
908923
924+ func getStatusV2 (scanType string , scanValue string , sonarHost string , projectSlug string ) (string , error ) {
925+ // token := os.Getenv("PLUGIN_SONAR_TOKEN")
926+
927+ fmt .Println ("Searchng last analysis" )
928+
929+ var reportRequest url.Values
930+
931+ if scanType == "branch" {
932+ fmt .Println ("Searchng last analysis by branch" )
933+ reportRequest = url.Values {
934+ "branch" : {scanValue },
935+ "projectKey" : {projectSlug },
936+ }
937+ } else {
938+ fmt .Println ("Searchng last analysis by pull request" )
939+ reportRequest = url.Values {
940+ "pullRequest" : {scanValue },
941+ "projectKey" : {projectSlug },
942+ }
943+ }
944+
945+ fmt .Printf ("==> Job Status Request:\n " )
946+ fmt .Printf (sonarHost + "/api/qualitygates/project_status?" + reportRequest .Encode ())
947+ fmt .Printf ("\n " )
948+ fmt .Printf ("\n " )
949+ fmt .Printf ("scanType:" + scanType )
950+ fmt .Printf ("scanValue:" + scanValue )
951+ fmt .Printf ("\n " )
952+
953+ // projectRequest, err := http.NewRequest("GET", sonarHost+"/api/qualitygates/project_status?"+reportRequest.Encode(), nil)
954+ // projectRequest.Header.Add("Authorization", basicAuth+token)
955+ // projectResponse, err := netClient.Do(projectRequest)
956+ // if err != nil {
957+ // logrus.WithFields(logrus.Fields{
958+ // "error": err,
959+ // }).Fatal("Failed get status")
960+ // return "", err
961+ // }
962+ // buf, _ := ioutil.ReadAll(projectResponse.Body)
963+ buf , err := GetProjectStatus (sonarHost , reportRequest .Encode (), projectSlug )
964+
965+ if err != nil {
966+ logrus .WithFields (logrus.Fields {
967+ "error" : err ,
968+ }).Error ("Failed to get project status" )
969+ }
970+
971+ project := ProjectStatusResponse {}
972+ if err := json .Unmarshal (buf , & project ); err != nil {
973+ logrus .WithFields (logrus.Fields {
974+ "error" : err ,
975+ }).Fatal ("Failed" )
976+ return "" , nil
977+ }
978+
979+ fmt .Printf ("==> Report Result:\n " )
980+ fmt .Printf (string (buf ))
981+
982+ // JUNUT
983+ junitReport := ""
984+ junitReport = string (buf ) // returns a string of what was written to it
985+ fmt .Printf ("\n ---------------------> JUNIT Exporter <---------------------\n " )
986+ bytesReport := []byte (junitReport )
987+ var projectReport Project
988+ err = json .Unmarshal (bytesReport , & projectReport )
989+ if err != nil {
990+ panic (err )
991+ }
992+
993+ fmt .Printf ("%+v" , projectReport )
994+ fmt .Printf ("\n " )
995+ result := ParseJunit (projectReport , "BankingApp" )
996+ file , _ := xml .MarshalIndent (result , "" , " " )
997+ _ = ioutil .WriteFile ("sonarResults.xml" , file , 0644 )
998+
999+ fmt .Printf ("\n " )
1000+ fmt .Printf ("\n ======> JUNIT Exporter <======\n " )
1001+
1002+ //JUNIT
1003+ fmt .Printf ("\n ======> Harness Drone/CIE SonarQube Plugin <======\n \n ====> Results:" )
1004+
1005+ return project .ProjectStatus .Status , nil
1006+ }
1007+
9091008func GetProjectStatus (sonarHost string , analysisId string , projectSlug string ) ([]byte , error ) {
9101009 token := os .Getenv ("PLUGIN_SONAR_TOKEN" )
9111010 fmt .Printf ("\n " )
0 commit comments