You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ly...@apache.org on 2024/05/16 04:05:06 UTC

(incubator-devlake) branch dev-multi-env updated (06e0299dd -> ac4b4fc39)

This is an automated email from the ASF dual-hosted git repository.

lynwee pushed a change to branch dev-multi-env
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


    from 06e0299dd Merge branch 'main' into dev-multi-env
     new 65670c4bd feat(webhook): add display_title in payload
     new ac4b4fc39 feat(webhook): update post-deployment API

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 backend/plugins/webhook/api/deployments.go         | 141 +++++++++++----------
 .../register/webhook/components/create-dialog.tsx  |   4 +-
 .../register/webhook/components/view-dialog.tsx    |   4 +-
 3 files changed, 77 insertions(+), 72 deletions(-)


(incubator-devlake) 01/02: feat(webhook): add display_title in payload

Posted by ly...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

lynwee pushed a commit to branch dev-multi-env
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit 65670c4bd2073a33842c29752d0940298e95ee13
Author: d4x1 <15...@qq.com>
AuthorDate: Thu May 16 11:47:36 2024 +0800

    feat(webhook): add display_title in payload
---
 backend/plugins/webhook/api/deployments.go | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/backend/plugins/webhook/api/deployments.go b/backend/plugins/webhook/api/deployments.go
index 64b9e91e5..f39814c82 100644
--- a/backend/plugins/webhook/api/deployments.go
+++ b/backend/plugins/webhook/api/deployments.go
@@ -36,7 +36,8 @@ import (
 )
 
 type WebhookDeployTaskRequest struct {
-	PipelineId string `mapstructure:"pipeline_id"`
+	DisplayTitle string `mapstructure:"display_title"`
+	PipelineId   string `mapstructure:"pipeline_id"`
 	// RepoUrl should be unique string, fill url or other unique data
 	RepoId string `mapstructure:"repo_id"`
 	Result string `mapstructure:"result"`
@@ -58,11 +59,12 @@ type WebhookDeployTaskRequest struct {
 }
 
 type DeploymentCommit struct {
-	RepoUrl   string `mapstructure:"repo_url" validate:"required"`
-	Name      string `mapstructure:"name"`
-	RefName   string `mapstructure:"ref_name"`
-	CommitSha string `mapstructure:"commit_sha" validate:"required"`
-	CommitMsg string `mapstructure:"commit_msg"`
+	DisplayTitle string `mapstructure:"display_title"`
+	RepoUrl      string `mapstructure:"repo_url" validate:"required"`
+	Name         string `mapstructure:"name"`
+	RefName      string `mapstructure:"ref_name"`
+	CommitSha    string `mapstructure:"commit_sha" validate:"required"`
+	CommitMsg    string `mapstructure:"commit_msg"`
 }
 
 // PostDeploymentCicdTask
@@ -158,6 +160,7 @@ func PostDeploymentCicdTask(input *plugin.ApiResourceInput) (*plugin.ApiResource
 			CicdDeploymentId: pipelineId,
 			CicdScopeId:      scopeId,
 			Name:             name,
+			DisplayTitle:     request.DisplayTitle,
 			Result:           request.Result,
 			Status:           devops.STATUS_DONE,
 			OriginalResult:   request.Result,
@@ -207,6 +210,7 @@ func PostDeploymentCicdTask(input *plugin.ApiResourceInput) (*plugin.ApiResource
 				//QueuedDurationSec: queuedDuration,
 				RepoId:              request.RepoId,
 				Name:                fmt.Sprintf(`deployment for %s`, commit.CommitSha),
+				DisplayTitle:        commit.DisplayTitle,
 				RepoUrl:             commit.RepoUrl,
 				Environment:         request.Environment,
 				OriginalEnvironment: request.Environment,


(incubator-devlake) 02/02: feat(webhook): update post-deployment API

Posted by ly...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

lynwee pushed a commit to branch dev-multi-env
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit ac4b4fc39c756dfacbdd7aca658e7b1c8640c019
Author: d4x1 <15...@qq.com>
AuthorDate: Thu May 16 12:04:56 2024 +0800

    feat(webhook): update post-deployment API
---
 backend/plugins/webhook/api/deployments.go         | 127 +++++++++++----------
 .../register/webhook/components/create-dialog.tsx  |   4 +-
 .../register/webhook/components/view-dialog.tsx    |   4 +-
 3 files changed, 68 insertions(+), 67 deletions(-)

diff --git a/backend/plugins/webhook/api/deployments.go b/backend/plugins/webhook/api/deployments.go
index f39814c82..b1c8b2d28 100644
--- a/backend/plugins/webhook/api/deployments.go
+++ b/backend/plugins/webhook/api/deployments.go
@@ -20,6 +20,8 @@ package api
 import (
 	"crypto/md5"
 	"fmt"
+	"github.com/apache/incubator-devlake/core/dal"
+	"github.com/apache/incubator-devlake/core/log"
 	"net/http"
 	"strings"
 	"time"
@@ -37,7 +39,7 @@ import (
 
 type WebhookDeployTaskRequest struct {
 	DisplayTitle string `mapstructure:"display_title"`
-	PipelineId   string `mapstructure:"pipeline_id"`
+	PipelineId   string `mapstructure:"pipeline_id" validate:"required"`
 	// RepoUrl should be unique string, fill url or other unique data
 	RepoId string `mapstructure:"repo_id"`
 	Result string `mapstructure:"result"`
@@ -55,7 +57,7 @@ type WebhookDeployTaskRequest struct {
 	CommitSha    string     `mapstructure:"commit_sha"`
 	CommitMsg    string     `mapstructure:"commit_msg"`
 	// DeploymentCommits is used for multiple commits in one deployment
-	DeploymentCommits []DeploymentCommit `mapstructure:"deploymentCommits" validate:"omitempty,dive"`
+	DeploymentCommits []DeploymentCommit `mapstructure:"deployment_commits" validate:"omitempty,dive"`
 }
 
 type DeploymentCommit struct {
@@ -67,42 +69,12 @@ type DeploymentCommit struct {
 	CommitMsg    string `mapstructure:"commit_msg"`
 }
 
-// PostDeploymentCicdTask
-// @Summary create deployment by webhook
-// @Description Create deployment pipeline by webhook.<br/>
-// @Description example1: {"repo_url":"devlake","commit_sha":"015e3d3b480e417aede5a1293bd61de9b0fd051d","start_time":"2020-01-01T12:00:00+00:00","end_time":"2020-01-01T12:59:59+00:00","environment":"PRODUCTION"}<br/>
-// @Description So we suggest request before task after deployment pipeline finish.
-// @Description Both cicd_pipeline and cicd_task will be created
-// @Tags plugins/webhook
-// @Param body body WebhookDeployTaskRequest true "json body"
-// @Success 200
-// @Failure 400  {string} errcode.Error "Bad Request"
-// @Failure 403  {string} errcode.Error "Forbidden"
-// @Failure 500  {string} errcode.Error "Internal Error"
-// @Router /plugins/webhook/connections/:connectionId/deployments [POST]
-func PostDeploymentCicdTask(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
-	connection := &models.WebhookConnection{}
-	err := connectionHelper.First(connection, input.Params)
-	if err != nil {
-		return nil, err
-	}
-	// get request
-	request := &WebhookDeployTaskRequest{}
-	err = api.DecodeMapStruct(input.Body, request, true)
-	if err != nil {
-		return &plugin.ApiResourceOutput{Body: err.Error(), Status: http.StatusBadRequest}, nil
-	}
-	// validate
-	vld = validator.New()
-	err = errors.Convert(vld.Struct(request))
-	if err != nil {
-		return nil, errors.BadInput.Wrap(vld.Struct(request), `input json error`)
-	}
-	txHelper := dbhelper.NewTxHelper(basicRes, &err)
-	defer txHelper.End()
-	tx := txHelper.Begin()
+func generateDeploymentCommitId(connectionId uint64, repoUrl string, commitSha string) string {
+	urlHash16 := fmt.Sprintf("%x", md5.Sum([]byte(repoUrl)))[:16]
+	return fmt.Sprintf("%s:%d:%s:%s", "webhook", connectionId, urlHash16, commitSha)
+}
 
-	pipelineId := request.PipelineId
+func CreateDeploymentAndDeploymentCommits(connection *models.WebhookConnection, request *WebhookDeployTaskRequest, tx dal.Transaction, logger log.Logger) errors.Error {
 	scopeId := fmt.Sprintf("%s:%d", "webhook", connection.ID)
 	if request.CreatedDate == nil {
 		request.CreatedDate = request.StartedDate
@@ -145,19 +117,14 @@ func PostDeploymentCicdTask(input *plugin.ApiResourceInput) (*plugin.ApiResource
 	// queuedDuration := dateInfo.CalculateQueueDuration()
 	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
+			return errors.Convert(fmt.Errorf("commit_sha or repo_url is required"))
 		}
 		// create a deployment_commit record
 		deploymentCommit := &devops.CicdDeploymentCommit{
 			DomainEntity: domainlayer.DomainEntity{
-				Id: deploymentCommitId,
+				Id: generateDeploymentCommitId(connection.ID, request.RepoUrl, request.CommitSha),
 			},
-			CicdDeploymentId: pipelineId,
+			CicdDeploymentId: request.PipelineId,
 			CicdScopeId:      scopeId,
 			Name:             name,
 			DisplayTitle:     request.DisplayTitle,
@@ -176,30 +143,23 @@ func PostDeploymentCicdTask(input *plugin.ApiResourceInput) (*plugin.ApiResource
 			CommitSha:           request.CommitSha,
 			CommitMsg:           request.CommitMsg,
 		}
-		err = tx.CreateOrUpdate(deploymentCommit)
-		if err != nil {
+		if err := tx.CreateOrUpdate(deploymentCommit); err != nil {
 			logger.Error(err, "create deployment commit")
-			return nil, err
+			return err
 		}
-
 		// create a deployment record
-		if err = tx.CreateOrUpdate(deploymentCommit.ToDeployment()); err != nil {
+		if err := tx.CreateOrUpdate(deploymentCommit.ToDeployment()); err != nil {
 			logger.Error(err, "create deployment")
-			return nil, err
+			return err
 		}
 	} else {
 		for _, commit := range request.DeploymentCommits {
-			urlHash16 := fmt.Sprintf("%x", md5.Sum([]byte(commit.RepoUrl)))[:16]
-			deploymentCommitId := fmt.Sprintf("%s:%d:%s:%s", "webhook", connection.ID, urlHash16, commit.CommitSha)
-			if pipelineId == "" {
-				pipelineId = deploymentCommitId
-			}
 			// create a deployment_commit record
 			deploymentCommit := &devops.CicdDeploymentCommit{
 				DomainEntity: domainlayer.DomainEntity{
-					Id: deploymentCommitId,
+					Id: generateDeploymentCommitId(connection.ID, commit.RepoUrl, commit.CommitSha),
 				},
-				CicdDeploymentId: pipelineId,
+				CicdDeploymentId: request.PipelineId,
 				CicdScopeId:      scopeId,
 				Result:           request.Result,
 				Status:           devops.STATUS_DONE,
@@ -218,20 +178,61 @@ func PostDeploymentCicdTask(input *plugin.ApiResourceInput) (*plugin.ApiResource
 				CommitSha:           commit.CommitSha,
 				CommitMsg:           commit.CommitMsg,
 			}
-			err = tx.CreateOrUpdate(deploymentCommit)
-			if err != nil {
+
+			if err := tx.CreateOrUpdate(deploymentCommit); err != nil {
 				logger.Error(err, "create deployment commit")
-				return nil, err
+				return err
 			}
 
 			// create a deployment record
 			deploymentCommit.Name = name
-			if err = tx.CreateOrUpdate(deploymentCommit.ToDeployment()); err != nil {
+			if err := tx.CreateOrUpdate(deploymentCommit.ToDeployment()); err != nil {
 				logger.Error(err, "create deployment")
-				return nil, err
+				return err
 			}
 		}
 	}
+	return nil
+}
+
+// PostDeploymentCicdTask
+// @Summary create deployment by webhook
+// @Description Create deployment pipeline by webhook.<br/>
+// @Description example1: {"repo_url":"devlake","commit_sha":"015e3d3b480e417aede5a1293bd61de9b0fd051d","start_time":"2020-01-01T12:00:00+00:00","end_time":"2020-01-01T12:59:59+00:00","environment":"PRODUCTION"}<br/>
+// @Description So we suggest request before task after deployment pipeline finish.
+// @Description Both cicd_pipeline and cicd_task will be created
+// @Tags plugins/webhook
+// @Param body body WebhookDeployTaskRequest true "json body"
+// @Success 200
+// @Failure 400  {string} errcode.Error "Bad Request"
+// @Failure 403  {string} errcode.Error "Forbidden"
+// @Failure 500  {string} errcode.Error "Internal Error"
+// @Router /plugins/webhook/connections/:connectionId/deployments [POST]
+func PostDeploymentCicdTask(input *plugin.ApiResourceInput) (*plugin.ApiResourceOutput, errors.Error) {
+	connection := &models.WebhookConnection{}
+	err := connectionHelper.First(connection, input.Params)
+	if err != nil {
+		return nil, err
+	}
+	// get request
+	request := &WebhookDeployTaskRequest{}
+	err = api.DecodeMapStruct(input.Body, request, true)
+	if err != nil {
+		return &plugin.ApiResourceOutput{Body: err.Error(), Status: http.StatusBadRequest}, nil
+	}
+	// validate
+	vld = validator.New()
+	err = errors.Convert(vld.Struct(request))
+	if err != nil {
+		return nil, errors.BadInput.Wrap(vld.Struct(request), `input json error`)
+	}
+	txHelper := dbhelper.NewTxHelper(basicRes, &err)
+	defer txHelper.End()
+	tx := txHelper.Begin()
+	if err := CreateDeploymentAndDeploymentCommits(connection, request, tx, logger); err != nil {
+		logger.Error(err, "create deployments")
+		return nil, err
+	}
 
 	return &plugin.ApiResourceOutput{Body: nil, Status: http.StatusOK}, nil
 }
diff --git a/config-ui/src/plugins/register/webhook/components/create-dialog.tsx b/config-ui/src/plugins/register/webhook/components/create-dialog.tsx
index 233ee908f..d293508f7 100644
--- a/config-ui/src/plugins/register/webhook/components/create-dialog.tsx
+++ b/config-ui/src/plugins/register/webhook/components/create-dialog.tsx
@@ -80,13 +80,13 @@ export const CreateDialog = ({ open, onCancel, onSubmitAfter }: Props) => {
    "title":"a feature from DLK",
    "type":"INCIDENT",
    "original_status":"TODO",
-   "status":"TODO",    
+   "status":"TODO",
    "created_date":"2020-01-01T12:00:00+00:00",
    "updated_date":"2020-01-01T12:00:00+00:00"
 }'`,
         closeIssuesEndpoint: `curl ${prefix}${res.closeIssuesEndpoint} -X 'POST' -H 'Authorization: Bearer ${res.apiKey}'`,
         postDeploymentsCurl: `curl ${prefix}${res.postPipelineDeployTaskEndpoint} -X 'POST' -H 'Authorization: Bearer ${res.apiKey}' -d '{
-    "deploymentCommits":[
+    "deployment_commits":[
       {
       "commit_sha":"the sha of deployment commit1",
       "repo_url":"the repo URL of the deployment commit"
diff --git a/config-ui/src/plugins/register/webhook/components/view-dialog.tsx b/config-ui/src/plugins/register/webhook/components/view-dialog.tsx
index 342f701e8..bb44c7a39 100644
--- a/config-ui/src/plugins/register/webhook/components/view-dialog.tsx
+++ b/config-ui/src/plugins/register/webhook/components/view-dialog.tsx
@@ -41,7 +41,7 @@ const transformURI = (prefix: string, webhook: IWebhook, apiKey: string) => {
         "title":"a feature from DLK",
         "type":"INCIDENT",
         "original_status":"TODO",
-        "status":"TODO",    
+        "status":"TODO",
         "created_date":"2020-01-01T12:00:00+00:00",
         "updated_date":"2020-01-01T12:00:00+00:00"
      }'`,
@@ -51,7 +51,7 @@ const transformURI = (prefix: string, webhook: IWebhook, apiKey: string) => {
     postDeploymentsCurl: `curl ${prefix}${webhook.postPipelineDeployTaskEndpoint} -X 'POST' -H 'Authorization: Bearer ${
       apiKey ?? '{API_KEY}'
     }' -d '{
-      "deploymentCommits":[
+      "deployment_commits":[
         {
         "commit_sha":"the sha of deployment commit1",
         "repo_url":"the repo URL of the deployment commit"