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


##########
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:
   no, this is in the loop



-- 
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