Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: move scheduleApproval to application runner (#3676)

p0nyccc188f95ce9
diff --git a/api/issue.go b/api/issue.go
index b2d7559838..ee49ac274a 100644
--- a/api/issue.go
+++ b/api/issue.go
@@ -110,12 +110,13 @@ type IssueCreate struct {
Pipeline PipelineCreate `jsonapi:"attr,pipeline"`
// Domain specific fields
- Name string `jsonapi:"attr,name"`
- Type IssueType `jsonapi:"attr,type"`
- Description string `jsonapi:"attr,description"`
- AssigneeID int `jsonapi:"attr,assigneeId"`
- SubscriberIDList []int `jsonapi:"attr,subscriberIdList"`
- Payload string `jsonapi:"attr,payload"`
+ Name string `jsonapi:"attr,name"`
+ Type IssueType `jsonapi:"attr,type"`
+ Description string `jsonapi:"attr,description"`
+ AssigneeID int `jsonapi:"attr,assigneeId"`
+ AssigneeNeedAttention bool `jsonapi:"attr,assigneeNeedAttention"`
+ SubscriberIDList []int `jsonapi:"attr,subscriberIdList"`
+ Payload string `jsonapi:"attr,payload"`
// CreateContext is used to create the issue pipeline and not persisted.
// The context format depends on the issue type. For example, create database issue corresponds to CreateDatabaseContext.
// This consolidates the pipeline generation to backend because both frontend and VCS pipeline could create issues and
@@ -248,9 +249,10 @@ type IssueFind struct {
PrincipalID *int
// To support pagination, we add into creator, assignee and subscriber.
// Only principleID or one of the following three fields can be set.
- CreatorID *int
- AssigneeID *int
- SubscriberID *int
+ CreatorID *int
+ AssigneeID *int
+ AssigneeNeedAttention *bool
+ SubscriberID *int
StatusList []IssueStatus
// If specified, only find issues whose ID is smaller that SinceID.
diff --git a/server/application_runner.go b/server/application_runner.go
index 1051199f30..8d946b2ac9 100644
--- a/server/application_runner.go
+++ b/server/application_runner.go
@@ -83,6 +83,23 @@ func (r *ApplicationRunner) Run(ctx context.Context, wg *sync.WaitGroup) {
return
}
+ assigneeNeedAtterntion := true
+ find := &api.IssueFind{
+ StatusList: []api.IssueStatus{api.IssueOpen},
+ AssigneeNeedAttention: &assigneeNeedAtterntion,
+ }
+
+ issueByID := make(map[int]*api.Issue)
+ issues, err := r.store.FindIssue(ctx, find)
+ if err != nil {
+ log.Error("failed to find issue stripped", zap.Any("issueFind", find), zap.Error(err))
+ return
+ }
+ for _, issue := range issues {
+ issueByID[issue.ID] = issue
+ r.scheduleApproval(ctx, issue, &value)
+ }
+
externalApprovalList, err := r.store.FindExternalApproval(ctx, &api.ExternalApprovalFind{})
if err != nil {
log.Error("failed to find external approval list", zap.Error(err))
@@ -102,9 +119,9 @@ func (r *ApplicationRunner) Run(ctx context.Context, wg *sync.WaitGroup) {
continue
}
- issue, err := r.store.GetIssueByID(ctx, externalApproval.IssueID)
- if err != nil {
- log.Error("failed to get issue by issue id", zap.Int("issueID", externalApproval.IssueID), zap.Error(err))
+ issue, ok := issueByID[externalApproval.IssueID]
+ if !ok {
+ log.Error("expect to have found issue in application runner", zap.Int("issue_id", externalApproval.IssueID))
continue
}
stage := getActiveStage(issue.Pipeline.StageList)
@@ -514,27 +531,8 @@ func (r *ApplicationRunner) createExternalApproval(ctx context.Context, issue *a
return nil
}
-// ScheduleApproval tries to cancel old external apporvals and create new external approvals if needed.
-func (r *ApplicationRunner) ScheduleApproval(ctx context.Context, pipeline *api.Pipeline) {
- settingName := api.SettingAppIM
- setting, err := r.store.GetSetting(ctx, &api.SettingFind{Name: &settingName})
- if err != nil {
- log.Error("failed to get IM setting", zap.String("settingName", string(settingName)), zap.Error(err))
- return
- }
- if setting == nil {
- log.Error("cannot find IM setting")
- return
- }
- if setting.Value == "" {
- return
- }
- var settingValue api.SettingAppIMValue
- if err := json.Unmarshal([]byte(setting.Value), &settingValue); err != nil {
- log.Error("failed to unmarshal IM setting", zap.String("settingName", string(settingName)), zap.Error(err))
- return
- }
-
+// scheduleApproval tries to cancel old external apporvals and create new external approvals if needed.
+func (r *ApplicationRunner) scheduleApproval(ctx context.Context, issue *api.Issue, settingValue *api.SettingAppIMValue) {
if !settingValue.ExternalApproval.Enabled {
return
}
@@ -544,30 +542,12 @@ func (r *ApplicationRunner) ScheduleApproval(ctx context.Context, pipeline *api.
return
}
- find := &api.IssueFind{
- PipelineID: &pipeline.ID,
- StatusList: []api.IssueStatus{api.IssueOpen},
- }
- issues, err := r.store.FindIssueStripped(ctx, find)
- if err != nil {
- log.Error("failed to find issues", zap.Any("issueFind", find), zap.Error(err))
- return
- }
- if len(issues) > 1 {
- log.Error("expect 0 or 1 issue, get more than 1 issues", zap.Any("issues", issues))
- return
- }
- if len(issues) == 0 {
- // no containing issue, skip
- return
- }
- issue := issues[0]
- stage := getActiveStage(pipeline.StageList)
+ stage := getActiveStage(issue.Pipeline.StageList)
if stage == nil {
- stage = pipeline.StageList[len(pipeline.StageList)-1]
+ stage = issue.Pipeline.StageList[len(issue.Pipeline.StageList)-1]
}
- oldApproval, err := r.cancelOldExternalApprovalIfNeeded(ctx, issue, stage, &settingValue)
+ oldApproval, err := r.cancelOldExternalApprovalIfNeeded(ctx, issue, stage, settingValue)
if err != nil {
log.Error("failed to cancelOldExternalApprovalIfNeeded", zap.Error(err))
return
@@ -587,7 +567,7 @@ func (r *ApplicationRunner) ScheduleApproval(ctx context.Context, pipeline *api.
return
}
- if err := r.createExternalApproval(ctx, issue, stage, &settingValue); err != nil {
+ if err := r.createExternalApproval(ctx, issue, stage, settingValue); err != nil {
log.Error("failed to create external approval", zap.Error(err))
return
}
diff --git a/server/issue.go b/server/issue.go
index 5693d68165..e7a1346d07 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -34,6 +34,9 @@ func (s *Server) registerIssueRoutes(g *echo.Group) {
}
issueCreate.CreatorID = c.Get(getPrincipalIDContextKey()).(int)
+ // TODO(p0ny): remove this line when manually sending external approval is ready. This is to temporarily keep our auto sending behaviour.
+ issueCreate.AssigneeNeedAttention = true
+
issue, err := s.createIssue(ctx, issueCreate)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create issue").SetInternal(err)
diff --git a/server/task_scheduler.go b/server/task_scheduler.go
index bd37f24e05..db22f0b45e 100644
--- a/server/task_scheduler.go
+++ b/server/task_scheduler.go
@@ -83,14 +83,13 @@ func (s *TaskScheduler) Run(ctx context.Context, wg *sync.WaitGroup) {
log.Error("Failed to retrieve open pipelines", zap.Error(err))
return
}
- for i, pipeline := range pipelineList {
+ for _, pipeline := range pipelineList {
if err := s.server.ScheduleActiveStage(ctx, pipeline); err != nil {
log.Error("Failed to schedule tasks in the active stage",
zap.Int("pipeline_id", pipeline.ID),
zap.Error(err),
)
}
- s.server.ApplicationRunner.ScheduleApproval(ctx, pipelineList[i])
}
// Inspect all running tasks
diff --git a/server/webhook.go b/server/webhook.go
index 9e94672e12..40b1434182 100644
--- a/server/webhook.go
+++ b/server/webhook.go
@@ -823,13 +823,14 @@ func (s *Server) createIssueFromMigrationDetailList(ctx context.Context, issueNa
}
}
issueCreate := &api.IssueCreate{
- CreatorID: creatorID,
- ProjectID: projectID,
- Name: issueName,
- Type: issueType,
- Description: issueDescription,
- AssigneeID: api.SystemBotID,
- CreateContext: string(createContext),
+ CreatorID: creatorID,
+ ProjectID: projectID,
+ Name: issueName,
+ Type: issueType,
+ Description: issueDescription,
+ AssigneeID: api.SystemBotID,
+ AssigneeNeedAttention: true,
+ CreateContext: string(createContext),
}
issue, err := s.createIssue(ctx, issueCreate)
if err != nil {
diff --git a/store/issue.go b/store/issue.go
index e9274c86e5..44d22f8caf 100644
--- a/store/issue.go
+++ b/store/issue.go
@@ -115,6 +115,24 @@ func (s *Store) GetIssueByPipelineID(ctx context.Context, id int) (*api.Issue, e
return issue, nil
}
+// FindIssue finds a list of issues.
+func (s *Store) FindIssue(ctx context.Context, find *api.IssueFind) ([]*api.Issue, error) {
+ issueRawList, err := s.findIssueRaw(ctx, find)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to find Issue list with IssueFind[%+v]", find)
+ }
+ var issueList []*api.Issue
+ for _, raw := range issueRawList {
+ issue, err := s.composeIssue(ctx, raw)
+ if err != nil {
+ return nil, errors.Wrapf(err, "failed to compose Issue with issueRaw[%+v]", raw)
+ }
+ issueList = append(issueList, issue)
+ }
+
+ return issueList, nil
+}
+
// FindIssueStripped finds a list of issues in stripped format.
// We do not load the pipeline in order to reduce the size of the response payload and the complexity of composing the issue list.
func (s *Store) FindIssueStripped(ctx context.Context, find *api.IssueFind) ([]*api.Issue, error) {
@@ -574,9 +592,10 @@ func (*Store) createIssueImpl(ctx context.Context, tx *Tx, create *api.IssueCrea
type,
description,
assignee_id,
+ assignee_need_attention,
payload
)
- VALUES ($1, $2, $3, $4, $5, 'OPEN', $6, $7, $8, $9)
+ VALUES ($1, $2, $3, $4, $5, 'OPEN', $6, $7, $8, $9, $10)
RETURNING id, creator_id, created_ts, updater_id, updated_ts, project_id, pipeline_id, name, status, type, description, assignee_id, assignee_need_attention, payload
`
var issueRaw issueRaw
@@ -589,6 +608,7 @@ func (*Store) createIssueImpl(ctx context.Context, tx *Tx, create *api.IssueCrea
create.Type,
create.Description,
create.AssigneeID,
+ create.AssigneeNeedAttention,
create.Payload,
).Scan(
&issueRaw.ID,
@@ -641,6 +661,9 @@ func (*Store) findIssueImpl(ctx context.Context, tx *Tx, find *api.IssueFind) ([
if v := find.AssigneeID; v != nil {
where, args = append(where, fmt.Sprintf("assignee_id = $%d", len(args)+1)), append(args, *v)
}
+ if v := find.AssigneeNeedAttention; v != nil {
+ where, args = append(where, fmt.Sprintf("assignee_need_attention = $%d", len(args)+1)), append(args, *v)
+ }
if v := find.SubscriberID; v != nil {
where, args = append(where, fmt.Sprintf("EXISTS (SELECT 1 FROM issue_subscriber WHERE issue_id = issue.id AND subscriber_id = $%d)", len(args)+1)), append(args, *v)
}
diff --git a/tests/external_approval_test.go b/tests/external_approval_test.go
index accf2b0bb6..4b140b5e23 100644
--- a/tests/external_approval_test.go
+++ b/tests/external_approval_test.go
@@ -144,8 +144,8 @@ func TestExternalApprovalFeishu_AllUserCanBeFound(t *testing.T) {
})
a.NoError(err)
- // Sleep for 5 seconds, giving time to ApplicationRunner to create external approvals.
- time.Sleep(5 * time.Second)
+ // Sleep for 65 seconds, giving time to ApplicationRunner to create external approvals.
+ time.Sleep(65 * time.Second)
issue, err = ctl.getIssue(issue.ID)
a.NoError(err)
taskStatus, err := getNextTaskStatus(issue)
@@ -294,8 +294,8 @@ func TestExternalApprovalFeishu_AssigneeCanBeFound(t *testing.T) {
})
a.NoError(err)
- // Sleep for 5 seconds, giving time to ApplicationRunner to create external approvals.
- time.Sleep(5 * time.Second)
+ // Sleep for 65 seconds, giving time to ApplicationRunner to create external approvals.
+ time.Sleep(65 * time.Second)
issue, err = ctl.getIssue(issue.ID)
a.NoError(err)
taskStatus, err := getNextTaskStatus(issue)