-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Patrick Zhao <[email protected]>
- Loading branch information
Showing
21 changed files
with
495 additions
and
33 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
pkg/cli/zadig-agent/internal/agent/step/scanning/step_sonar_get_metrics.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright 2022 The KodeRover Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package scanning | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"time" | ||
|
||
"gopkg.in/yaml.v2" | ||
|
||
"github.com/koderover/zadig/v2/pkg/cli/zadig-agent/helper/log" | ||
"github.com/koderover/zadig/v2/pkg/cli/zadig-agent/internal/common/types" | ||
"github.com/koderover/zadig/v2/pkg/setting" | ||
"github.com/koderover/zadig/v2/pkg/tool/sonar" | ||
"github.com/koderover/zadig/v2/pkg/types/step" | ||
"github.com/koderover/zadig/v2/pkg/util" | ||
) | ||
|
||
type SonarGetMetrics struct { | ||
spec *step.StepSonarGetMetricsSpec | ||
envs []string | ||
secretEnvs []string | ||
workspace string | ||
dirs *types.AgentWorkDirs | ||
Logger *log.JobLogger | ||
} | ||
|
||
func NewSonarGetMetricsStep(spec interface{}, dirs *types.AgentWorkDirs, envs, secretEnvs []string, logger *log.JobLogger) (*SonarGetMetrics, error) { | ||
sonarCheckStep := &SonarGetMetrics{dirs: dirs, workspace: dirs.Workspace, envs: envs, secretEnvs: secretEnvs} | ||
yamlBytes, err := yaml.Marshal(spec) | ||
if err != nil { | ||
return sonarCheckStep, fmt.Errorf("marshal spec %+v failed", spec) | ||
} | ||
if err := yaml.Unmarshal(yamlBytes, &sonarCheckStep.spec); err != nil { | ||
return sonarCheckStep, fmt.Errorf("unmarshal spec %s to shell spec failed", yamlBytes) | ||
} | ||
return sonarCheckStep, nil | ||
} | ||
|
||
func (s *SonarGetMetrics) Run(ctx context.Context) error { | ||
s.Logger.Infof("Start get Sonar scanning metrics.") | ||
sonarWorkDir := sonar.GetSonarWorkDir(s.spec.Parameter) | ||
if sonarWorkDir == "" { | ||
sonarWorkDir = ".scannerwork" | ||
} | ||
if !filepath.IsAbs(sonarWorkDir) { | ||
sonarWorkDir = filepath.Join(s.workspace, s.spec.CheckDir, sonarWorkDir) | ||
} | ||
taskReportDir := filepath.Join(sonarWorkDir, "report-task.txt") | ||
bytes, err := os.ReadFile(taskReportDir) | ||
if err != nil { | ||
s.Logger.Errorf("read sonar task report file: %s error :%v", time.Now().Format(setting.WorkflowTimeFormat), taskReportDir, err) | ||
return nil | ||
} | ||
taskReportContent := string(bytes) | ||
ceTaskID := sonar.GetSonarCETaskID(taskReportContent) | ||
if ceTaskID == "" { | ||
s.Logger.Errorf("can not get sonar ce task ID") | ||
return nil | ||
} | ||
|
||
outputFileName := filepath.Join(s.dirs.JobOutputsDir, setting.WorkflowScanningJobOutputKey) | ||
err = util.AppendToFile(outputFileName, ceTaskID) | ||
if err != nil { | ||
err = fmt.Errorf("append sonar ce task ID %s to output file %s error: %v", ceTaskID, outputFileName, err) | ||
s.Logger.Errorf(err.Error()) | ||
return nil | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...ice/aslan/core/common/service/workflowcontroller/stepcontroller/step_sonar_get_metrics.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
Copyright 2022 The KodeRover Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package stepcontroller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"go.uber.org/zap" | ||
"gopkg.in/yaml.v2" | ||
|
||
commonmodels "github.com/koderover/zadig/v2/pkg/microservice/aslan/core/common/repository/models" | ||
"github.com/koderover/zadig/v2/pkg/setting" | ||
"github.com/koderover/zadig/v2/pkg/tool/log" | ||
"github.com/koderover/zadig/v2/pkg/tool/sonar" | ||
"github.com/koderover/zadig/v2/pkg/types/job" | ||
"github.com/koderover/zadig/v2/pkg/types/step" | ||
) | ||
|
||
type sonarGetMetricsCtl struct { | ||
step *commonmodels.StepTask | ||
sonarGetMetricsSpec *step.StepSonarGetMetricsSpec | ||
log *zap.SugaredLogger | ||
workflowCtx *commonmodels.WorkflowTaskCtx | ||
} | ||
|
||
func NewSonarGetMetricsCtl(stepTask *commonmodels.StepTask, workflowCtx *commonmodels.WorkflowTaskCtx, log *zap.SugaredLogger) (*sonarGetMetricsCtl, error) { | ||
yamlString, err := yaml.Marshal(stepTask.Spec) | ||
if err != nil { | ||
return nil, fmt.Errorf("marshal sonar check spec error: %v", err) | ||
} | ||
sonarGetMetricsSpec := &step.StepSonarGetMetricsSpec{} | ||
if err := yaml.Unmarshal(yamlString, &sonarGetMetricsSpec); err != nil { | ||
return nil, fmt.Errorf("unmarshal sonar check error: %v", err) | ||
} | ||
stepTask.Spec = sonarGetMetricsSpec | ||
return &sonarGetMetricsCtl{sonarGetMetricsSpec: sonarGetMetricsSpec, log: log, step: stepTask, workflowCtx: workflowCtx}, nil | ||
} | ||
|
||
func (s *sonarGetMetricsCtl) PreRun(ctx context.Context) error { | ||
return nil | ||
} | ||
|
||
func (s *sonarGetMetricsCtl) AfterRun(ctx context.Context) error { | ||
key := job.GetJobOutputKey(s.step.JobKey, setting.WorkflowScanningJobOutputKey) | ||
id, ok := s.workflowCtx.GlobalContextGet(key) | ||
if !ok { | ||
err := fmt.Errorf("sonar check job output %s not found", key) | ||
log.Error(err) | ||
return err | ||
} | ||
|
||
client := sonar.NewSonarClient(s.sonarGetMetricsSpec.SonarServer, s.sonarGetMetricsSpec.SonarToken) | ||
resp, err := client.GetComponentMeasures(s.sonarGetMetricsSpec.ProjectKey) | ||
if err != nil { | ||
err = fmt.Errorf("get component measures error: %v", err) | ||
log.Error(err) | ||
return err | ||
} | ||
s.sonarGetMetricsSpec.SonarMetrics = &step.SonarMetrics{} | ||
for _, mesures := range resp.Component.Measures { | ||
switch mesures.Metric { | ||
case "coverage": | ||
s.sonarGetMetricsSpec.SonarMetrics.Coverage = mesures.Value | ||
case "bugs": | ||
s.sonarGetMetricsSpec.SonarMetrics.Bugs = mesures.Value | ||
case "vulnerabilities": | ||
s.sonarGetMetricsSpec.SonarMetrics.Vulnerabilities = mesures.Value | ||
case "code_smells": | ||
s.sonarGetMetricsSpec.SonarMetrics.CodeSmells = mesures.Value | ||
case "ncloc": | ||
s.sonarGetMetricsSpec.SonarMetrics.Ncloc = mesures.Value | ||
default: | ||
log.Warnf("unknown metric: %s: %s", mesures.Metric, mesures.Value) | ||
} | ||
} | ||
|
||
if s.sonarGetMetricsSpec.CheckQualityGate { | ||
log.Debugf("ce task id: %s", id) | ||
analysisID, err := client.WaitForCETaskTobeDone(id, time.Minute*10) | ||
if err != nil { | ||
log.Error(err) | ||
return err | ||
} | ||
gateInfo, err := client.GetQualityGateInfo(analysisID) | ||
if err != nil { | ||
log.Error(err) | ||
return err | ||
} | ||
s.sonarGetMetricsSpec.SonarMetrics.QualityGateStatus = gateInfo.ProjectStatus.Status | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.