Skip to content

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

feat: lgtm task checker (#2762)

p0ny66590fd27b8a
diff --git a/api/task_check_run.go b/api/task_check_run.go
index 098871673a..e54e6eab6a 100644
--- a/api/task_check_run.go
+++ b/api/task_check_run.go
@@ -77,6 +77,8 @@ const (
TaskCheckGhostSync TaskCheckType = "bb.task-check.database.ghost.sync"
// TaskCheckGeneralEarliestAllowedTime is the task check type for earliest allowed time.
TaskCheckGeneralEarliestAllowedTime TaskCheckType = "bb.task-check.general.earliest-allowed-time"
+ // TaskCheckIssueLGTM is the task check type for LGTM comments.
+ TaskCheckIssueLGTM TaskCheckType = "bb.task-check.issue.lgtm"
)
// TaskCheckEarliestAllowedTimePayload is the task check payload for earliest allowed time.
diff --git a/server/server.go b/server/server.go
index 57a10bf238..034adf6869 100644
--- a/server/server.go
+++ b/server/server.go
@@ -269,6 +269,9 @@ func NewServer(ctx context.Context, prof Profile) (*Server, error) {
timingExecutor := NewTaskCheckTimingExecutor()
taskCheckScheduler.Register(api.TaskCheckGeneralEarliestAllowedTime, timingExecutor)
+ checkLGTMExecutor := NewTaskCheckLGTMExecutor()
+ taskCheckScheduler.Register(api.TaskCheckIssueLGTM, checkLGTMExecutor)
+
s.TaskCheckScheduler = taskCheckScheduler
// Schema syncer
diff --git a/server/task_check_executor_lgtm.go b/server/task_check_executor_lgtm.go
new file mode 100644
index 0000000000..6c77462b09
--- /dev/null
+++ b/server/task_check_executor_lgtm.go
@@ -0,0 +1,137 @@
+package server
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/api"
+ "github.com/bytebase/bytebase/common"
+ "github.com/bytebase/bytebase/store"
+)
+
+// NewTaskCheckLGTMExecutor creates a task check LGTM executor.
+func NewTaskCheckLGTMExecutor() TaskCheckExecutor {
+ return &TaskCheckLGTMExecutor{}
+}
+
+// TaskCheckLGTMExecutor is the task check LGTM executor. It checks if "LGTM" comments are present.
+type TaskCheckLGTMExecutor struct {
+}
+
+// Run will run the task check LGTM executor once.
+func (*TaskCheckLGTMExecutor) Run(ctx context.Context, server *Server, taskCheckRun *api.TaskCheckRun) (result []api.TaskCheckResult, err error) {
+ task, err := server.store.GetTaskByID(ctx, taskCheckRun.TaskID)
+ if err != nil {
+ return nil, common.Wrap(err, common.Internal)
+ }
+ if task == nil {
+ return []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.Internal.Int(),
+ Title: fmt.Sprintf("Failed to find task %d", taskCheckRun.TaskID),
+ },
+ }, nil
+ }
+
+ issue, err := server.store.GetIssueByPipelineID(ctx, task.PipelineID)
+ if err != nil {
+ return nil, common.Wrap(err, common.Internal)
+ }
+ if issue == nil {
+ return []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.Internal.Int(),
+ Title: fmt.Sprintf("Failed to find issue by pipelineID %d", task.PipelineID),
+ },
+ }, nil
+ }
+
+ if issue.Project.LGTMCheckSetting.Value == api.LGTMValueDisabled {
+ return []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusSuccess,
+ Namespace: api.BBNamespace,
+ Code: common.Ok.Int(),
+ Title: "Skip check",
+ Content: "This check is disabled.",
+ },
+ }, nil
+ }
+
+ activityType := string(api.ActivityIssueCommentCreate)
+ activityList, err := server.store.FindActivity(ctx, &api.ActivityFind{
+ TypePrefix: &activityType,
+ ContainerID: &issue.ID,
+ })
+ if err != nil {
+ return nil, common.Wrap(err, common.Internal)
+ }
+
+ ok, err := checkLGTMcomments(ctx, server.store, activityList, issue)
+ if err != nil {
+ return nil, errors.Wrap(err, "failed to check LGTM comments")
+ }
+ if ok {
+ return []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusSuccess,
+ Namespace: api.BBNamespace,
+ Code: common.Ok.Int(),
+ Title: "OK",
+ Content: "Valid LGTM found",
+ },
+ }, nil
+ }
+
+ return []api.TaskCheckResult{
+ {
+ Status: api.TaskCheckStatusError,
+ Namespace: api.BBNamespace,
+ Code: common.NotFound.Int(),
+ Title: "Not Found",
+ Content: "Valid LGTM NOT found",
+ },
+ }, nil
+}
+
+func checkLGTMcomments(ctx context.Context, store *store.Store, activityList []*api.Activity, issue *api.Issue) (bool, error) {
+ for _, activity := range activityList {
+ ok, err := isCommentLGTM(ctx, store, activity, issue)
+ if err != nil {
+ return false, err
+ }
+ if ok {
+ return true, nil
+ }
+ }
+ return false, nil
+}
+
+func isCommentLGTM(ctx context.Context, store *store.Store, activity *api.Activity, issue *api.Issue) (bool, error) {
+ if activity.Comment != "LGTM" {
+ return false, nil
+ }
+ member, err := store.GetProjectMember(ctx, &api.ProjectMemberFind{
+ PrincipalID: &activity.CreatorID,
+ ProjectID: &issue.ProjectID,
+ })
+ if err != nil {
+ return false, err
+ }
+ if member == nil {
+ return false, nil
+ }
+ switch issue.Project.LGTMCheckSetting.Value {
+ case api.LGTMValueProjectMember:
+ return true, nil
+ case api.LGTMValueProjectOwner:
+ return member.Role == string(api.Owner), nil
+ }
+ return false, errors.Errorf("unexpected LGTM setting value: %s", issue.Project.LGTMCheckSetting.Value)
+}
diff --git a/server/task_check_scheduler.go b/server/task_check_scheduler.go
index 969bca5f9a..97a8e16e7e 100644
--- a/server/task_check_scheduler.go
+++ b/server/task_check_scheduler.go
@@ -225,6 +225,9 @@ func (s *TaskCheckScheduler) ScheduleCheckIfNeeded(ctx context.Context, task *ap
if err := s.scheduleTimingTaskCheck(ctx, task, creatorID, skipIfAlreadyTerminated); err != nil {
return nil, errors.Wrap(err, "failed to schedule timing task check")
}
+ if err := s.scheduleLGTMTaskCheck(ctx, task, creatorID, skipIfAlreadyTerminated); err != nil {
+ return nil, errors.Wrap(err, "failed to schedule LGTM task check")
+ }
if task.Type != api.TaskDatabaseSchemaUpdate && task.Type != api.TaskDatabaseDataUpdate && task.Type != api.TaskDatabaseSchemaUpdateGhostSync {
return task, nil
@@ -440,6 +443,30 @@ func (s *TaskCheckScheduler) scheduleTimingTaskCheck(ctx context.Context, task *
return nil
}
+func (s *TaskCheckScheduler) scheduleLGTMTaskCheck(ctx context.Context, task *api.Task, creatorID int, skipIfAlreadyTerminated bool) error {
+ issue, err := s.server.store.GetIssueByPipelineID(ctx, task.PipelineID)
+ if err != nil {
+ return err
+ }
+ if issue == nil {
+ // skip if no containing issue.
+ return nil
+ }
+ if issue.Project.LGTMCheckSetting.Value == api.LGTMValueDisabled {
+ // don't schedule LGTM check if it's disabled.
+ return nil
+ }
+ if _, err := s.server.store.CreateTaskCheckRunIfNeeded(ctx, &api.TaskCheckRunCreate{
+ CreatorID: creatorID,
+ TaskID: task.ID,
+ Type: api.TaskCheckIssueLGTM,
+ SkipIfAlreadyTerminated: skipIfAlreadyTerminated,
+ }); err != nil {
+ return err
+ }
+ return nil
+}
+
// Returns true only if the task check run result is at least the minimum required level.
// For PendingApproval->Pending transitions, the minimum level is SUCCESS.
// For Pending->Running transitions, the minimum level is WARN.