Skip to content

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

fix: force close external approval (#3421)

p0ny5688088c80c0
diff --git a/api/setting.go b/api/setting.go
index dd2d7a95dd..67f1517588 100644
--- a/api/setting.go
+++ b/api/setting.go
@@ -83,7 +83,7 @@ type SettingAppIMValue struct {
AppID string `json:"appId"`
AppSecret string `json:"appSecret"`
ExternalApproval struct {
- Enabled bool `json:"enabled"`
- ApprovalCode string
+ Enabled bool `json:"enabled"`
+ ApprovalDefinitionID string `json:"approvalDefinitionID"`
} `json:"externalApproval"`
}
diff --git a/server/application_runner.go b/server/application_runner.go
index 2234abccaa..5db761bd35 100644
--- a/server/application_runner.go
+++ b/server/application_runner.go
@@ -186,8 +186,7 @@ func (r *ApplicationRunner) cancelOldExternalApprovalIfNeeded(ctx context.Contex
}()
if cancelOld {
- _, err := r.store.PatchExternalApproval(ctx, &api.ExternalApprovalPatch{ID: approval.ID, RowStatus: api.Archived})
- if err != nil {
+ if _, err := r.store.PatchExternalApproval(ctx, &api.ExternalApprovalPatch{ID: approval.ID, RowStatus: api.Archived}); err != nil {
return nil, err
}
if err := r.p.CancelExternalApproval(ctx,
@@ -195,7 +194,7 @@ func (r *ApplicationRunner) cancelOldExternalApprovalIfNeeded(ctx context.Contex
AppID: settingValue.AppID,
AppSecret: settingValue.AppSecret,
},
- settingValue.ExternalApproval.ApprovalCode,
+ settingValue.ExternalApproval.ApprovalDefinitionID,
payload.InstanceCode,
payload.RequesterID,
); err != nil {
@@ -205,8 +204,8 @@ func (r *ApplicationRunner) cancelOldExternalApprovalIfNeeded(ctx context.Contex
return approval, nil
}
-// CancelExternalApprovalIfNeeded trys to cancel the active external approval of an issue.
-func (r *ApplicationRunner) CancelExternalApprovalIfNeeded(ctx context.Context, issue *api.Issue) error {
+// CancelExternalApproval cancels the active external approval of an issue.
+func (r *ApplicationRunner) CancelExternalApproval(ctx context.Context, issue *api.Issue) error {
settingName := api.SettingAppIM
setting, err := r.store.GetSetting(ctx, &api.SettingFind{Name: &settingName})
if err != nil {
@@ -225,14 +224,29 @@ func (r *ApplicationRunner) CancelExternalApprovalIfNeeded(ctx context.Context,
if !value.ExternalApproval.Enabled {
return nil
}
- stage := getActiveStage(issue.Pipeline.StageList)
- if stage == nil {
- stage = issue.Pipeline.StageList[len(issue.Pipeline.StageList)-1]
+ approval, err := r.store.GetExternalApprovalByIssueID(ctx, issue.ID)
+ if err != nil {
+ return err
}
- if _, err := r.cancelOldExternalApprovalIfNeeded(ctx, issue, stage, &value); err != nil {
- return errors.Wrap(err, "failed to cancelOldExternalApprovalIfNeeded")
+ if approval == nil {
+ return nil
}
- return nil
+ var payload api.ExternalApprovalPayloadFeishu
+ if err := json.Unmarshal([]byte(approval.Payload), &payload); err != nil {
+ return err
+ }
+ if _, err := r.store.PatchExternalApproval(ctx, &api.ExternalApprovalPatch{ID: approval.ID, RowStatus: api.Archived}); err != nil {
+ return err
+ }
+ return r.p.CancelExternalApproval(ctx,
+ feishu.TokenCtx{
+ AppID: value.AppID,
+ AppSecret: value.AppSecret,
+ },
+ value.ExternalApproval.ApprovalDefinitionID,
+ payload.InstanceCode,
+ payload.RequesterID,
+ )
}
func (r *ApplicationRunner) shouldCreateExternalApproval(ctx context.Context, issue *api.Issue, stage *api.Stage, oldApproval *api.ExternalApproval) (bool, error) {
@@ -322,7 +336,7 @@ func (r *ApplicationRunner) createExternalApproval(ctx context.Context, issue *a
Link: fmt.Sprintf("%s/issue/%s", r.activityManager.s.profile.ExternalURL, api.IssueSlug(issue)),
TaskList: taskList,
},
- settingValue.ExternalApproval.ApprovalCode,
+ settingValue.ExternalApproval.ApprovalDefinitionID,
users[issue.Creator.Email],
users[issue.Assignee.Email])
if err != nil {
@@ -375,7 +389,7 @@ func (r *ApplicationRunner) ScheduleApproval(ctx context.Context, pipeline *api.
return
}
- if settingValue.ExternalApproval.ApprovalCode == "" {
+ if settingValue.ExternalApproval.ApprovalDefinitionID == "" {
log.Error("no approval code", zap.Any("settingValue", settingValue))
return
}
diff --git a/server/issue.go b/server/issue.go
index b8a59ee497..b8c50e5545 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -1301,8 +1301,8 @@ func (s *Server) changeIssueStatus(ctx context.Context, issue *api.Issue, newSta
}
pipelineStatus = api.PipelineCanceled
- // Try to cancel external approval, it's ok if we failed.
- if err := s.ApplicationRunner.CancelExternalApprovalIfNeeded(ctx, issue); err != nil {
+ // Cancel external approval, it's ok if we failed.
+ if err := s.ApplicationRunner.CancelExternalApproval(ctx, issue); err != nil {
log.Error("failed to cancel external appoval if needed on issue cancellation", zap.Error(err))
}
}
diff --git a/server/setting.go b/server/setting.go
index aeb761704b..6303d21a2d 100644
--- a/server/setting.go
+++ b/server/setting.go
@@ -75,14 +75,14 @@ func (s *Server) registerSettingRoutes(g *echo.Group) {
p := s.ApplicationRunner.p
// clear token cache so that we won't use the previous token.
p.ClearTokenCache()
- approvalCode, err := p.CreateApprovalDefinition(ctx, feishu.TokenCtx{
+ approvalDefinitionID, err := p.CreateApprovalDefinition(ctx, feishu.TokenCtx{
AppID: value.AppID,
AppSecret: value.AppSecret,
}, "")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create approval definition").SetInternal(err)
}
- value.ExternalApproval.ApprovalCode = approvalCode
+ value.ExternalApproval.ApprovalDefinitionID = approvalDefinitionID
b, err := json.Marshal(value)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal updated setting value").SetInternal(err)