func ConvertBug()

in backend/plugins/zentao/tasks/bug_convertor.go [44:147]


func ConvertBug(taskCtx plugin.SubTaskContext) errors.Error {
	data := taskCtx.GetData().(*ZentaoTaskData)
	db := taskCtx.GetDal()
	bugIdGen := didgen.NewDomainIdGenerator(&models.ZentaoBug{})
	accountIdGen := didgen.NewDomainIdGenerator(&models.ZentaoAccount{})
	executionIdGen := didgen.NewDomainIdGenerator(&models.ZentaoExecution{})
	boardIdGen := didgen.NewDomainIdGenerator(&models.ZentaoProduct{})
	stdTypeMappings := getStdTypeMappings(data)
	if data.Options.ProjectId != 0 {
		boardIdGen = didgen.NewDomainIdGenerator(&models.ZentaoProject{})
	}

	storyIdGen := didgen.NewDomainIdGenerator(&models.ZentaoStory{})
	cursor, err := db.Cursor(
		dal.From(&models.ZentaoBug{}),
		dal.Where(`project = ? and
			connection_id = ?`, data.Options.ProjectId, data.Options.ConnectionId),
	)
	if err != nil {
		return err
	}
	defer cursor.Close()
	convertor, err := api.NewDataConverter(api.DataConverterArgs{
		InputRowType: reflect.TypeOf(models.ZentaoBug{}),
		Input:        cursor,
		RawDataSubTaskArgs: api.RawDataSubTaskArgs{
			Ctx:     taskCtx,
			Options: data.Options,
			Table:   RAW_BUG_TABLE,
		},
		Convert: func(inputRow interface{}) ([]interface{}, errors.Error) {
			toolEntity := inputRow.(*models.ZentaoBug)
			domainEntity := &ticket.Issue{
				DomainEntity: domainlayer.DomainEntity{
					Id: bugIdGen.Generate(toolEntity.ConnectionId, toolEntity.ID),
				},
				IssueKey:        strconv.FormatInt(toolEntity.ID, 10),
				Title:           toolEntity.Title,
				Type:            toolEntity.StdType,
				OriginalType:    "bug",
				OriginalStatus:  toolEntity.Status,
				ResolutionDate:  toolEntity.ClosedDate.ToNullableTime(),
				CreatedDate:     toolEntity.OpenedDate.ToNullableTime(),
				UpdatedDate:     toolEntity.LastEditedDate.ToNullableTime(),
				Priority:        getPriority(toolEntity.Pri),
				CreatorName:     toolEntity.OpenedByName,
				AssigneeName:    toolEntity.AssignedToName,
				Severity:        string(rune(toolEntity.Severity)),
				Url:             convertIssueURL(toolEntity.Url, "bug", toolEntity.ID),
				OriginalProject: getOriginalProject(data),
				Status:          toolEntity.StdStatus,
			}
			if mappingType, ok := stdTypeMappings[domainEntity.OriginalType]; ok && mappingType != "" {
				domainEntity.Type = mappingType
			}
			if toolEntity.Story != 0 {
				domainEntity.ParentIssueId = storyIdGen.Generate(data.Options.ConnectionId, toolEntity.Story)
			}
			if toolEntity.OpenedById != 0 {
				domainEntity.CreatorId = accountIdGen.Generate(data.Options.ConnectionId, toolEntity.OpenedById)
			}
			if toolEntity.AssignedToId != 0 {
				domainEntity.AssigneeId = accountIdGen.Generate(data.Options.ConnectionId, toolEntity.AssignedToId)
			}
			if toolEntity.DueDate != nil {
				domainEntity.DueDate = toolEntity.DueDate
			}
			closedDate := toolEntity.ClosedDate
			openedDate := toolEntity.OpenedDate
			if closedDate != nil && closedDate.ToTime().After(openedDate.ToTime()) {
				temp := uint(closedDate.ToNullableTime().Sub(openedDate.ToTime()).Minutes())
				domainEntity.LeadTimeMinutes = &temp
			}
			var results []interface{}
			if domainEntity.AssigneeId != "" {
				issueAssignee := &ticket.IssueAssignee{
					IssueId:      domainEntity.Id,
					AssigneeId:   domainEntity.AssigneeId,
					AssigneeName: domainEntity.AssigneeName,
				}
				results = append(results, issueAssignee)
			}

			domainBoardIssue := &ticket.BoardIssue{
				BoardId: boardIdGen.Generate(data.Options.ConnectionId, data.Options.ProjectId),
				IssueId: domainEntity.Id,
			}
			if toolEntity.Execution != 0 {
				sprintIssue := &ticket.SprintIssue{
					SprintId: executionIdGen.Generate(data.Options.ConnectionId, toolEntity.Execution),
					IssueId:  domainEntity.Id,
				}
				results = append(results, sprintIssue)
			}
			results = append(results, domainEntity, domainBoardIssue)
			return results, nil
		},
	})
	if err != nil {
		return err
	}

	return convertor.Execute()
}