Skip to content

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

fix: cancel external approval on issue cancellation (#3410)

p0ny00e01ee3cc5b
diff --git a/server/application_runner.go b/server/application_runner.go
index 048c16b98e..2f8ddecc3d 100644
--- a/server/application_runner.go
+++ b/server/application_runner.go
@@ -205,6 +205,36 @@ 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 {
+ settingName := api.SettingAppIM
+ setting, err := r.store.GetSetting(ctx, &api.SettingFind{Name: &settingName})
+ if err != nil {
+ return errors.Wrapf(err, "failed to get IM setting by settingName %s", string(settingName))
+ }
+ if setting == nil {
+ return errors.New("cannot find IM setting")
+ }
+ if setting.Value == "" {
+ return nil
+ }
+ var value api.SettingAppIMValue
+ if err := json.Unmarshal([]byte(setting.Value), &value); err != nil {
+ return errors.Wrapf(err, "failed to unmarshal IM setting, settingName %s", string(settingName))
+ }
+ if !value.ExternalApproval.Enabled {
+ return nil
+ }
+ stage := getActiveStage(issue.Pipeline.StageList)
+ if stage == nil {
+ stage = issue.Pipeline.StageList[len(issue.Pipeline.StageList)-1]
+ }
+ if _, err := r.cancelOldExternalApprovalIfNeeded(ctx, issue, stage, &value); err != nil {
+ return errors.Wrap(err, "failed to cancelOldExternalApprovalIfNeeded")
+ }
+ return nil
+}
+
func (*ApplicationRunner) shouldCreateExternalApproval(issue *api.Issue, stage *api.Stage, oldApproval *api.ExternalApproval) (bool, error) {
if oldApproval != nil {
var oldPayload api.ExternalApprovalPayloadFeishu
diff --git a/server/issue.go b/server/issue.go
index 345b973eff..9a5dcf1985 100644
--- a/server/issue.go
+++ b/server/issue.go
@@ -15,9 +15,11 @@ import (
"github.com/google/jsonapi"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
+ "go.uber.org/zap"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/common/log"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/db/util"
"github.com/bytebase/bytebase/plugin/vcs"
@@ -1298,6 +1300,11 @@ 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 {
+ log.Error("failed to cancel external appoval if needed on issue cancellation", zap.Error(err))
+ }
}
pipelinePatch := &api.PipelinePatch{