klesh commented on code in PR #3052:
URL: https://github.com/apache/incubator-devlake/pull/3052#discussion_r973863262
##########
plugins/webhook/api/cicd_pipeline.go:
##########
@@ -18,41 +18,199 @@ limitations under the License.
package api
import (
+ "fmt"
"github.com/apache/incubator-devlake/errors"
+ "github.com/apache/incubator-devlake/models/domainlayer"
+ "github.com/apache/incubator-devlake/models/domainlayer/devops"
+ "github.com/apache/incubator-devlake/models/domainlayer/ticket"
"github.com/apache/incubator-devlake/plugins/core"
+ "github.com/apache/incubator-devlake/plugins/core/dal"
+ "github.com/apache/incubator-devlake/plugins/helper"
"github.com/apache/incubator-devlake/plugins/webhook/models"
+ "github.com/go-playground/validator/v10"
"net/http"
"time"
)
-type WebhookPipelineRequest struct {
- Id string `validate:"required"`
- Result string `validate:"oneof=SUCCESS FAILURE ABORT"`
+type WebhookTaskRequest struct {
+ // PipelineName can be filled by any string unique in one pipeline
+ PipelineName string `mapstructure:"pipeline_name" validate:"required"`
+
+ Name string `validate:"required"` // Name should be unique
in one pipeline
+ Result string `validate:"oneof=SUCCESS FAILURE ABORT
IN_PROGRESS"`
Status string `validate:"oneof=IN_PROGRESS DONE"`
- Type string `validate:"oneof=CI CD CI/CD"`
- CreatedDate time.Time `mapstructure:"created_date"
validate:"required"`
+ Type string `validate:"oneof=TEST LINT BUILD DEPLOYMENT"`
+ Environment string `validate:"oneof=PRODUCTION STAGING TESTING"`
+ StartedDate time.Time `mapstructure:"created_date"
validate:"required"`
FinishedDate *time.Time `mapstructure:"finished_date"`
- Repo string `validate:"required"`
+ RepoId string `mapstructure:"repo_id" validate:"required"` // RepoId
should be unique string
Branch string
CommitSha string `mapstructure:"commit_sha"`
}
-// PostCicdPipeline
+// PostCicdTask
// @Summary create pipeline by webhook
-// @Description Create pipeline by webhook, example:
{"id":"A123123","result":"one of SUCCESS/FAILURE/ABORT","status":"one of
IN_PROGRESS/DONE","type":"CI/CD","created_date":"2020-01-01T12:00:00+00:00","finished_date":"2020-01-01T12:59:59+00:00","repo":"devlake","branch":"main","commit_sha":"015e3d3b480e417aede5a1293bd61de9b0fd051d"}
+// @Description Create pipeline by webhook.<br/>
+// @Description example1:
{"pipeline_name":"A123","name":"unit-test","result":"IN_PROGRESS","status":"IN_PROGRESS","type":"TEST","environment":"PRODUCTION","created_date":"2020-01-01T12:00:00+00:00","finished_date":"2020-01-01T12:59:59+00:00","repo_id":"devlake","branch":"main","commit_sha":"015e3d3b480e417aede5a1293bd61de9b0fd051d"}<br/>
+// @Description example2:
{"pipeline_name":"A123","name":"unit-test","result":"SUCCESS","status":"DONE","type":"DEPLOYMENT","environment":"PRODUCTION","created_date":"2020-01-01T12:00:00+00:00","finished_date":"2020-01-01T12:59:59+00:00","repo_id":"devlake","branch":"main","commit_sha":"015e3d3b480e417aede5a1293bd61de9b0fd051d"}<br/>
+// @Description When request webhook first time for each pipeline, it will be
created.
+// @Description So we suggest request before task start and after pipeline
finish.
+// @Description Remember fill all data to request after pipeline finish.
// @Tags plugins/webhook
-// @Param body body WebhookPipelineRequest true "json body"
+// @Param body body WebhookTaskRequest 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/:connectionId/cicd_pipelines [POST]
-func PostCicdPipeline(input *core.ApiResourceInput) (*core.ApiResourceOutput,
errors.Error) {
+// @Router /plugins/webhook/:connectionId/cicd_tasks [POST]
+func PostCicdTask(input *core.ApiResourceInput) (*core.ApiResourceOutput,
errors.Error) {
connection := &models.WebhookConnection{}
err := connectionHelper.First(connection, input.Params)
if err != nil {
return nil, err
}
- // TODO save pipeline
+ // get request
+ request := &WebhookTaskRequest{}
+ err = helper.DecodeMapStruct(input.Body, request)
+ if err != nil {
+ return &core.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`)
+ }
+
+ db := basicRes.GetDal()
+ pipelineId := fmt.Sprintf("%s:%d:%s", "webhook", connection.ID,
request.PipelineName)
+ domainCicdTask := &devops.CICDTask{
+ DomainEntity: domainlayer.DomainEntity{
+ Id: fmt.Sprintf("%s:%d:%s:%s", "webhook",
connection.ID, request.PipelineName, request.Name),
+ },
+ PipelineId: pipelineId,
+ Name: request.Name,
+ Result: request.Result,
+ Status: request.Status,
+ Type: request.Type,
+ Environment: request.Environment,
+ StartedDate: request.StartedDate,
+ FinishedDate: request.FinishedDate,
+ }
+ if domainCicdTask.FinishedDate != nil {
+ domainCicdTask.DurationSec =
uint64(domainCicdTask.FinishedDate.Sub(domainCicdTask.StartedDate).Seconds())
+ }
+
+ domainPipeline := &devops.CICDPipeline{}
+ err = db.First(domainPipeline, dal.Where("id = ?", pipelineId))
+ if err != nil {
+ domainPipeline = &devops.CICDPipeline{
+ DomainEntity: domainlayer.DomainEntity{
+ Id: pipelineId,
+ },
+ Name: request.PipelineName,
+ Result: ``,
+ Status: `IN_PROGRESS`,
+ Type: ``,
+ CreatedDate: request.StartedDate,
+ FinishedDate: nil,
+ }
+ } else if domainPipeline.Status == `DONE` {
+ return nil, errors.Forbidden.New(`can not receive this task
because pipeline has already been done.`)
+ }
+
+ domainPipelineRepo := &devops.CiCDPipelineCommit{
Review Comment:
Please rename this to `commit` rather than `repo`
--
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]