Skip to content

Commit

Permalink
fix(sonarqube): create sonar qube project failed since pipeline id is…
Browse files Browse the repository at this point in the history
… empty (#650)
  • Loading branch information
zhujian7 authored and caicloud-bot committed Dec 6, 2018
1 parent 916fbf1 commit 5539f3b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/event/manager.go
Expand Up @@ -85,7 +85,7 @@ func (em *eventManager) HandleEvent(event *api.Event) error {
if codeScan != nil && codeScan.SonarQube != nil {
integration, err := em.ds.GetIntegration(codeScan.SonarQube.Name)
if err != nil {
log.Errorf("get integration %s failed: %v", codeScan.SonarQube.Name)
log.Errorf("get integration %s failed: %v", codeScan.SonarQube.Name, err)
return err
}
event.Pipeline.Build.Stages.CodeScan.SonarQube.SonarInfo = integration.SonarQube
Expand Down
1 change: 1 addition & 0 deletions pkg/integrate/manager.go
Expand Up @@ -105,6 +105,7 @@ func SetQualityGate(itype api.IntegrationType, url, token string, projectKey str
if err != nil {
return err
}

return p.SetQualityGate(url, token, projectKey, gateId)
}

Expand Down
42 changes: 30 additions & 12 deletions pkg/server/manager/pipeline.go
Expand Up @@ -138,7 +138,13 @@ func (m *pipelineManager) CreatePipeline(projectName string, pipeline *api.Pipel
codeScan := pipeline.Build.Stages.CodeScan
if codeScan != nil && codeScan.SonarQube != nil && codeScan.SonarQube.Config != nil &&
codeScan.SonarQube.Config.Threshold > 0 {
setSonarQualityGate(m.dataStore, pipeline)
if pipeline.ID == "" {
pipeline.ID = bson.NewObjectId().Hex()
}
err = setSonarQualityGate(m.dataStore, pipeline)
if err != nil {
return nil, err
}
}

createdPipeline, err := m.dataStore.CreatePipeline(pipeline)
Expand All @@ -165,16 +171,20 @@ func setSonarQualityGate(ds *store.DataStore, pipeline *api.Pipeline) error {
}

err = integrate.CreateProject(api.IntegrationTypeSonar, sonar.Address, sonar.Token, pipeline.ID, pipeline.Alias)
if strings.Contains(err.Error(), "key already exists") {
// If project already exist, will return:
// {"errors":[{"msg":"Could not create Project, key already exists: project-1"}]}
log.Infof("Project %s(%s) already exists.", pipeline.Alias, pipeline.ID)
} else {
return err
if err != nil {
if strings.Contains(err.Error(), "key already exists") {
// If project already exist, will return:
// {"errors":[{"msg":"Could not create Project, key already exists: project-1"}]}
log.Infof("Project %s(%s) already exists.", pipeline.Alias, pipeline.ID)
} else {
log.Errorf("Create sonar project %s error:%v", pipeline.Alias, err)
return err
}
}

err = integrate.SetQualityGate(api.IntegrationTypeSonar, sonar.Address, sonar.Token, pipeline.ID, gateID)
if err != nil {
log.Errorf("Set sonar quality gate %d for project %s failed as %v", gateID, pipeline.ID, err)
return err
}

Expand Down Expand Up @@ -392,11 +402,19 @@ func (m *pipelineManager) UpdatePipeline(projectName string, pipelineName string
pipeline.AutoTrigger = newPipeline.AutoTrigger

// set quality gate if codeScan is turned on.
cs := pipeline.Build.Stages.CodeScan
newcs := newPipeline.Build.Stages.CodeScan
if newcs != nil && newcs.SonarQube != nil && newcs.SonarQube.Config != nil && newcs.SonarQube.Config.Threshold > 0 &&
(cs == nil || cs.SonarQube.Config.Threshold != newcs.SonarQube.Config.Threshold) {
setSonarQualityGate(m.dataStore, newPipeline)
if pipeline.Build != nil && pipeline.Build.Stages != nil &&
newPipeline.Build != nil && newPipeline.Build.Stages != nil {
cs := pipeline.Build.Stages.CodeScan
newcs := newPipeline.Build.Stages.CodeScan

if newcs != nil && newcs.SonarQube != nil && newcs.SonarQube.Config != nil && newcs.SonarQube.Config.Threshold > 0 &&
(cs == nil || cs.SonarQube.Config.Threshold != newcs.SonarQube.Config.Threshold) {
newPipeline.ID = pipeline.ID
err = setSonarQualityGate(m.dataStore, newPipeline)
if err != nil {
return nil, err
}
}
}

// Update the properties of the pipeline.
Expand Down

0 comments on commit 5539f3b

Please sign in to comment.