d4x1 commented on code in PR #6535:
URL: 
https://github.com/apache/incubator-devlake/pull/6535#discussion_r1410104475


##########
backend/plugins/webhook/api/deployments.go:
##########
@@ -84,60 +98,123 @@ func PostDeploymentCicdTask(input 
*plugin.ApiResourceInput) (*plugin.ApiResource
        txHelper := dbhelper.NewTxHelper(basicRes, &err)
        defer txHelper.End()
        tx := txHelper.Begin()
-       urlHash16 := fmt.Sprintf("%x", md5.Sum([]byte(request.RepoUrl)))[:16]
-       scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
-       deploymentCommitId := fmt.Sprintf("%s:%d:%s:%s", "webhook", 
connection.ID, urlHash16, request.CommitSha)
+
        pipelineId := request.PipelineId
-       if pipelineId == "" {
-               pipelineId = deploymentCommitId
-       }
+       scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
        if request.CreatedDate == nil {
                request.CreatedDate = request.StartedDate
        }
-       if request.Environment == "" {
-               request.Environment = devops.PRODUCTION
-       }
        if request.FinishedDate == nil {
                now := time.Now()
                request.FinishedDate = &now
        }
        if request.Result == "" {
                request.Result = devops.RESULT_SUCCESS
        }
-       duration := request.FinishedDate.Sub(*request.StartedDate).Seconds()
-
-       // create a deployment_commit record
-       deploymentCommit := &devops.CicdDeploymentCommit{
-               DomainEntity: domainlayer.DomainEntity{
-                       Id: deploymentCommitId,
-               },
-               CicdDeploymentId: pipelineId,
-               CicdScopeId:      scopeId,
-               Name:             fmt.Sprintf(`deployment for %s`, 
request.CommitSha),
-               Result:           request.Result,
-               Status:           devops.STATUS_DONE,
-               OriginalResult:   request.Result,
-               OriginalStatus:   devops.STATUS_DONE,
-               Environment:      request.Environment,
-               CreatedDate:      *request.CreatedDate,
-               StartedDate:      request.StartedDate,
-               FinishedDate:     request.FinishedDate,
-               DurationSec:      &duration,
-               CommitSha:        request.CommitSha,
-               RefName:          request.RefName,
-               RepoId:           request.RepoId,
-               RepoUrl:          request.RepoUrl,
+       if request.Environment == "" {
+               request.Environment = devops.PRODUCTION
        }
-       err = tx.CreateOrUpdate(deploymentCommit)
-       if err != nil {
-               logger.Error(err, "create deployment commit")
-               return nil, err
+       duration := request.FinishedDate.Sub(*request.StartedDate).Seconds()
+       name := request.Name
+       if name == "" {
+               if request.DeploymentCommits == nil {
+                       name = fmt.Sprintf(`deployment for %s`, 
request.CommitSha)
+               } else {
+                       commit_sha_list := []string{}

Review Comment:
   why not name it with `commitShaList`? 



##########
backend/plugins/webhook/api/deployments.go:
##########
@@ -36,18 +38,30 @@ import (
 type WebhookDeployTaskRequest struct {
        PipelineId string `mapstructure:"pipeline_id"`
        // RepoUrl should be unique string, fill url or other unique data
-       RepoId    string `mapstructure:"repo_id"`
-       RepoUrl   string `mapstructure:"repo_url" validate:"required"`
-       CommitSha string `mapstructure:"commit_sha" validate:"required"`
-       RefName   string `mapstructure:"ref_name"`
-       Result    string `mapstructure:"result"`
+       RepoId string `mapstructure:"repo_id"`
+       Result string `mapstructure:"result"`
        // start_time and end_time is more readable for users,
        // StartedDate and FinishedDate is same as columns in db.
        // So they all keep.
        CreatedDate  *time.Time `mapstructure:"create_time"`
        StartedDate  *time.Time `mapstructure:"start_time" validate:"required"`
        FinishedDate *time.Time `mapstructure:"end_time"`
+       RepoUrl      string     `mapstructure:"repo_url"`
        Environment  string     `validate:"omitempty,oneof=PRODUCTION STAGING 
TESTING DEVELOPMENT"`

Review Comment:
   why doesn't  this field have `mapstructure`?



##########
backend/plugins/webhook/api/deployments.go:
##########
@@ -84,60 +98,123 @@ func PostDeploymentCicdTask(input 
*plugin.ApiResourceInput) (*plugin.ApiResource
        txHelper := dbhelper.NewTxHelper(basicRes, &err)
        defer txHelper.End()
        tx := txHelper.Begin()
-       urlHash16 := fmt.Sprintf("%x", md5.Sum([]byte(request.RepoUrl)))[:16]
-       scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
-       deploymentCommitId := fmt.Sprintf("%s:%d:%s:%s", "webhook", 
connection.ID, urlHash16, request.CommitSha)
+
        pipelineId := request.PipelineId
-       if pipelineId == "" {
-               pipelineId = deploymentCommitId
-       }
+       scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
        if request.CreatedDate == nil {
                request.CreatedDate = request.StartedDate
        }
-       if request.Environment == "" {
-               request.Environment = devops.PRODUCTION
-       }
        if request.FinishedDate == nil {
                now := time.Now()
                request.FinishedDate = &now
        }
        if request.Result == "" {
                request.Result = devops.RESULT_SUCCESS
        }
-       duration := request.FinishedDate.Sub(*request.StartedDate).Seconds()
-
-       // create a deployment_commit record
-       deploymentCommit := &devops.CicdDeploymentCommit{
-               DomainEntity: domainlayer.DomainEntity{
-                       Id: deploymentCommitId,
-               },
-               CicdDeploymentId: pipelineId,
-               CicdScopeId:      scopeId,
-               Name:             fmt.Sprintf(`deployment for %s`, 
request.CommitSha),
-               Result:           request.Result,
-               Status:           devops.STATUS_DONE,
-               OriginalResult:   request.Result,
-               OriginalStatus:   devops.STATUS_DONE,
-               Environment:      request.Environment,
-               CreatedDate:      *request.CreatedDate,
-               StartedDate:      request.StartedDate,
-               FinishedDate:     request.FinishedDate,
-               DurationSec:      &duration,
-               CommitSha:        request.CommitSha,
-               RefName:          request.RefName,
-               RepoId:           request.RepoId,
-               RepoUrl:          request.RepoUrl,
+       if request.Environment == "" {
+               request.Environment = devops.PRODUCTION
        }
-       err = tx.CreateOrUpdate(deploymentCommit)
-       if err != nil {
-               logger.Error(err, "create deployment commit")
-               return nil, err
+       duration := request.FinishedDate.Sub(*request.StartedDate).Seconds()
+       name := request.Name
+       if name == "" {
+               if request.DeploymentCommits == nil {
+                       name = fmt.Sprintf(`deployment for %s`, 
request.CommitSha)
+               } else {
+                       commit_sha_list := []string{}
+                       for _, commit := range request.DeploymentCommits {
+                               commit_sha_list = append(commit_sha_list, 
commit.CommitSha)

Review Comment:
   same as above.



##########
backend/plugins/webhook/api/deployments.go:
##########
@@ -84,60 +98,123 @@ func PostDeploymentCicdTask(input 
*plugin.ApiResourceInput) (*plugin.ApiResource
        txHelper := dbhelper.NewTxHelper(basicRes, &err)
        defer txHelper.End()
        tx := txHelper.Begin()
-       urlHash16 := fmt.Sprintf("%x", md5.Sum([]byte(request.RepoUrl)))[:16]
-       scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
-       deploymentCommitId := fmt.Sprintf("%s:%d:%s:%s", "webhook", 
connection.ID, urlHash16, request.CommitSha)
+
        pipelineId := request.PipelineId
-       if pipelineId == "" {
-               pipelineId = deploymentCommitId
-       }
+       scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
        if request.CreatedDate == nil {
                request.CreatedDate = request.StartedDate
        }
-       if request.Environment == "" {
-               request.Environment = devops.PRODUCTION
-       }
        if request.FinishedDate == nil {
                now := time.Now()
                request.FinishedDate = &now
        }
        if request.Result == "" {
                request.Result = devops.RESULT_SUCCESS
        }
-       duration := request.FinishedDate.Sub(*request.StartedDate).Seconds()
-
-       // create a deployment_commit record
-       deploymentCommit := &devops.CicdDeploymentCommit{
-               DomainEntity: domainlayer.DomainEntity{
-                       Id: deploymentCommitId,
-               },
-               CicdDeploymentId: pipelineId,
-               CicdScopeId:      scopeId,
-               Name:             fmt.Sprintf(`deployment for %s`, 
request.CommitSha),
-               Result:           request.Result,
-               Status:           devops.STATUS_DONE,
-               OriginalResult:   request.Result,
-               OriginalStatus:   devops.STATUS_DONE,
-               Environment:      request.Environment,
-               CreatedDate:      *request.CreatedDate,
-               StartedDate:      request.StartedDate,
-               FinishedDate:     request.FinishedDate,
-               DurationSec:      &duration,
-               CommitSha:        request.CommitSha,
-               RefName:          request.RefName,
-               RepoId:           request.RepoId,
-               RepoUrl:          request.RepoUrl,
+       if request.Environment == "" {
+               request.Environment = devops.PRODUCTION
        }
-       err = tx.CreateOrUpdate(deploymentCommit)
-       if err != nil {
-               logger.Error(err, "create deployment commit")
-               return nil, err
+       duration := request.FinishedDate.Sub(*request.StartedDate).Seconds()
+       name := request.Name
+       if name == "" {
+               if request.DeploymentCommits == nil {
+                       name = fmt.Sprintf(`deployment for %s`, 
request.CommitSha)
+               } else {
+                       commit_sha_list := []string{}
+                       for _, commit := range request.DeploymentCommits {
+                               commit_sha_list = append(commit_sha_list, 
commit.CommitSha)
+                       }
+                       name = fmt.Sprintf(`deployment for %s`, 
strings.Join(commit_sha_list, ","))
+               }
        }
 
-       // create a deployment record
-       if err = tx.CreateOrUpdate(deploymentCommit.ToDeployment()); err != nil 
{
-               logger.Error(err, "create deployment")
-               return nil, err
+       if request.DeploymentCommits == nil {
+               if request.CommitSha == "" || request.RepoUrl == "" {
+                       return nil, errors.Convert(fmt.Errorf("commit_sha or 
repo_url is required"))
+               }
+               urlHash16 := fmt.Sprintf("%x", 
md5.Sum([]byte(request.RepoUrl)))[:16]
+               deploymentCommitId := fmt.Sprintf("%s:%d:%s:%s", "webhook", 
connection.ID, urlHash16, request.CommitSha)
+               if pipelineId == "" {
+                       pipelineId = deploymentCommitId
+               }
+               // create a deployment_commit record
+               deploymentCommit := &devops.CicdDeploymentCommit{
+                       DomainEntity: domainlayer.DomainEntity{
+                               Id: deploymentCommitId,
+                       },
+                       CicdDeploymentId: pipelineId,
+                       CicdScopeId:      scopeId,
+                       Name:             name,
+                       Result:           request.Result,
+                       Status:           devops.STATUS_DONE,
+                       OriginalResult:   request.Result,
+                       OriginalStatus:   devops.STATUS_DONE,
+                       CreatedDate:      *request.CreatedDate,
+                       StartedDate:      request.StartedDate,
+                       FinishedDate:     request.FinishedDate,
+                       DurationSec:      &duration,
+                       RepoId:           request.RepoId,
+                       RepoUrl:          request.RepoUrl,
+                       Environment:      request.Environment,
+                       RefName:          request.RefName,
+                       CommitSha:        request.CommitSha,
+                       CommitMsg:        request.CommitMsg,
+               }
+               err = tx.CreateOrUpdate(deploymentCommit)
+               if err != nil {
+                       logger.Error(err, "create deployment commit")
+                       return nil, err
+               }
+
+               // create a deployment record
+               if err = tx.CreateOrUpdate(deploymentCommit.ToDeployment()); 
err != nil {
+                       logger.Error(err, "create deployment")
+                       return nil, err
+               }
+       } else {
+               for _, commit := range request.DeploymentCommits {
+                       urlHash16 := fmt.Sprintf("%x", 
md5.Sum([]byte(commit.RepoUrl)))[:16]

Review Comment:
   I think it is duplicated code, and can be wrapped to a function.



##########
backend/plugins/webhook/api/deployments.go:
##########
@@ -84,60 +98,123 @@ func PostDeploymentCicdTask(input 
*plugin.ApiResourceInput) (*plugin.ApiResource
        txHelper := dbhelper.NewTxHelper(basicRes, &err)
        defer txHelper.End()
        tx := txHelper.Begin()
-       urlHash16 := fmt.Sprintf("%x", md5.Sum([]byte(request.RepoUrl)))[:16]
-       scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
-       deploymentCommitId := fmt.Sprintf("%s:%d:%s:%s", "webhook", 
connection.ID, urlHash16, request.CommitSha)
+
        pipelineId := request.PipelineId
-       if pipelineId == "" {
-               pipelineId = deploymentCommitId
-       }
+       scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
        if request.CreatedDate == nil {
                request.CreatedDate = request.StartedDate
        }
-       if request.Environment == "" {
-               request.Environment = devops.PRODUCTION
-       }
        if request.FinishedDate == nil {
                now := time.Now()
                request.FinishedDate = &now
        }
        if request.Result == "" {
                request.Result = devops.RESULT_SUCCESS
        }
-       duration := request.FinishedDate.Sub(*request.StartedDate).Seconds()
-
-       // create a deployment_commit record
-       deploymentCommit := &devops.CicdDeploymentCommit{
-               DomainEntity: domainlayer.DomainEntity{
-                       Id: deploymentCommitId,
-               },
-               CicdDeploymentId: pipelineId,
-               CicdScopeId:      scopeId,
-               Name:             fmt.Sprintf(`deployment for %s`, 
request.CommitSha),
-               Result:           request.Result,
-               Status:           devops.STATUS_DONE,
-               OriginalResult:   request.Result,
-               OriginalStatus:   devops.STATUS_DONE,
-               Environment:      request.Environment,
-               CreatedDate:      *request.CreatedDate,
-               StartedDate:      request.StartedDate,
-               FinishedDate:     request.FinishedDate,
-               DurationSec:      &duration,
-               CommitSha:        request.CommitSha,
-               RefName:          request.RefName,
-               RepoId:           request.RepoId,
-               RepoUrl:          request.RepoUrl,
+       if request.Environment == "" {
+               request.Environment = devops.PRODUCTION
        }
-       err = tx.CreateOrUpdate(deploymentCommit)
-       if err != nil {
-               logger.Error(err, "create deployment commit")
-               return nil, err
+       duration := request.FinishedDate.Sub(*request.StartedDate).Seconds()

Review Comment:
   `duration` is float type now. So please use `.Milliseconds()`, and divide 
1000, get the float64 duration.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to