func ExtractApiIssues()

in backend/plugins/gitlab/tasks/issue_extractor.go [136:258]


func ExtractApiIssues(subtaskCtx plugin.SubTaskContext) errors.Error {
	subtaskCommonArgs, data := CreateSubtaskCommonArgs(subtaskCtx, RAW_ISSUE_TABLE)

	db := subtaskCtx.GetDal()
	config := data.Options.ScopeConfig
	var issueSeverityRegex *regexp.Regexp
	var issueComponentRegex *regexp.Regexp
	var issuePriorityRegex *regexp.Regexp

	var issueSeverity = config.IssueSeverity
	var err error
	if len(issueSeverity) > 0 {
		issueSeverityRegex, err = regexp.Compile(issueSeverity)
		if err != nil {
			return errors.Default.Wrap(err, "regexp Compile issueSeverity failed")
		}
	}
	var issueComponent = config.IssueComponent
	if len(issueComponent) > 0 {
		issueComponentRegex, err = regexp.Compile(issueComponent)
		if err != nil {
			return errors.Default.Wrap(err, "regexp Compile issueComponent failed")
		}
	}
	var issuePriority = config.IssuePriority
	if len(issuePriority) > 0 {
		issuePriorityRegex, err = regexp.Compile(issuePriority)
		if err != nil {
			return errors.Default.Wrap(err, "regexp Compile issuePriority failed")
		}
	}
	subtaskCommonArgs.SubtaskConfig = map[string]interface{}{
		"issueSeverity":      issueSeverity,
		"issueComponent":     issueComponent,
		"issuePriorityRegex": issuePriorityRegex,
	}
	extractor, err := api.NewStatefulApiExtractor(&api.StatefulApiExtractorArgs[IssuesResponse]{
		SubtaskCommonArgs: subtaskCommonArgs,
		BeforeExtract: func(body *IssuesResponse, stateManager *api.SubtaskStateManager) errors.Error {
			if stateManager.IsIncremental() {
				err := db.Delete(
					&models.GitlabIssueLabel{},
					dal.Where("connection_id = ? AND issue_id = ?", data.Options.ConnectionId, body.Id),
				)
				if err != nil {
					return err
				}
				err = db.Delete(
					&models.GitlabIssueAssignee{},
					dal.Where("connection_id = ? AND gitlab_id = ?", data.Options.ConnectionId, body.Id),
				)
				if err != nil {
					return err
				}
			}
			return nil
		},
		Extract: func(body *IssuesResponse, row *api.RawData) ([]interface{}, errors.Error) {
			if body.ProjectId == 0 {
				return nil, nil
			}

			results := make([]interface{}, 0, 2)
			gitlabIssue, err := convertGitlabIssue(body, data.Options.ProjectId)
			if err != nil {
				return nil, err
			}
			for _, label := range body.Labels {
				results = append(results, &models.GitlabIssueLabel{
					ConnectionId: data.Options.ConnectionId,
					IssueId:      gitlabIssue.GitlabId,
					LabelName:    label,
				})
				if issueSeverityRegex != nil && issueSeverityRegex.MatchString(label) {
					gitlabIssue.Severity = label
				}
				if issueComponentRegex != nil && issueComponentRegex.MatchString(label) {
					gitlabIssue.Component = label
				}
				if issuePriorityRegex != nil && issuePriorityRegex.MatchString(label) {
					gitlabIssue.Priority = label
				}
			}

			gitlabIssue.ConnectionId = data.Options.ConnectionId
			if body.Author != nil {
				gitlabAuthor, err := convertGitlabAuthor(body, data.Options.ConnectionId)
				if err != nil {
					return nil, err
				}
				results = append(results, gitlabAuthor)
			}
			results = append(results, gitlabIssue)

			for _, v := range body.Assignees {
				assignee := &models.GitlabAccount{
					ConnectionId: data.Options.ConnectionId,
					Username:     v.Username,
					Name:         v.Name,
					State:        v.State,
					AvatarUrl:    v.AvatarUrl,
					WebUrl:       v.WebUrl,
				}
				issueAssignee := &models.GitlabIssueAssignee{
					ConnectionId: data.Options.ConnectionId,
					GitlabId:     gitlabIssue.GitlabId,
					ProjectId:    gitlabIssue.ProjectId,
					AssigneeId:   v.Id,
					AssigneeName: v.Username,
				}
				results = append(results, assignee, issueAssignee)
			}

			return results, nil
		},
	})

	if err != nil {
		return errors.Convert(err)
	}

	return extractor.Execute()
}