diff --git a/cmd/survey/main.go b/cmd/survey/main.go index 850a620..7191805 100644 --- a/cmd/survey/main.go +++ b/cmd/survey/main.go @@ -40,5 +40,8 @@ func init() { task.RegisterTask("coding_task", func(app *service.App) task.Tasker { return tasks.NewCodingTask(app) }) + task.RegisterTask("git-task", func(app *service.App) task.Tasker { + return tasks.NewGitTask(app) + }) } diff --git a/cmd/survey/tasks/gitTask.go b/cmd/survey/tasks/gitTask.go new file mode 100644 index 0000000..a64e5cb --- /dev/null +++ b/cmd/survey/tasks/gitTask.go @@ -0,0 +1,103 @@ +package tasks + +import ( + "context" + "errors" + "fmt" + "os" + "time" + + "github.com/LazyBachelor/LazyPM/internal/models" + "github.com/LazyBachelor/LazyPM/internal/service" + "github.com/LazyBachelor/LazyPM/pkg/task" + taskui "github.com/LazyBachelor/LazyPM/pkg/task/ui" + "github.com/go-git/go-git/v6" +) + +var ( + gitTaskDescription = `You are tasked with performing a Git operation. +This task will test your ability to use Git effectively.` +) + +type GitTask struct { + setupIssue *models.Issue + repo *git.Repository + done bool + + app *service.App +} + +func NewGitTask(app *service.App) *GitTask { + return &GitTask{app: app, done: false} +} + +func (t *GitTask) Config() task.Config { + return BaseConfig().WithStatisticsStoragePath("./.pm/git-task-stats.json") +} + +func (t *GitTask) Details() taskui.TaskDetails { + return BaseDetails().WithTitle("Git Task").WithDescription(gitTaskDescription) +} + +func (t *GitTask) Questions(interfaceType task.InterfaceType) taskui.Questions { + return BaseQuestions(interfaceType) +} + +func (t *GitTask) Setup(ctx context.Context) error { + if err := ClearIssues(t.app); err != nil { + return err + } + + var err error + t.repo, err = t.initRepo() + if err != nil { + return err + } + + os.WriteFile("./task/README.md", + []byte("This is a Git task. Please perform a Git operation here."), + os.FileMode(os.O_WRONLY|os.O_CREATE)) + + t.setupIssue = models.NewBaseIssue(). + WithTitle("Git Task Setup Issue"). + WithDescription(gitTaskDescription).Build() + + if err := t.app.Issues.CreateIssue(ctx, t.setupIssue, ""); err != nil { + return err + } + + return nil +} + +func (t *GitTask) Validate(ctx context.Context) (bool, error) { + + if t.setupIssue == nil { + return false, errors.New("setup issue not found") + } + + if t.setupIssue.Assignee != "Me" { + return false, fmt.Errorf("issue not assigned to self") + } + + if t.setupIssue.Status != models.StatusInProgress { + return false, fmt.Errorf("issue status is not in progress") + } + + // Git related validation can be added here, such as checking for commits, branches, etc. + + return EndTaskWithTimeout(&t.done, "Task completed!", 5*time.Second) +} + +func (t *GitTask) initRepo() (*git.Repository, error) { + repo, err := git.PlainInit("./task/.git/", true) + if err != nil { + if errors.Is(err, git.ErrTargetDirNotEmpty) { + repo, err = git.PlainOpen("./task/.git/") + if err != nil { + return nil, err + } + } + return nil, err + } + return repo, nil +}