feat: implement gt mq reject command for manual MR rejection
- Add gt mq command group with reject subcommand - Add FindMR method to locate MRs by ID or branch - Add RejectMR method to reject MRs with reason - Add notifyWorkerRejected for optional mail notification - Add tests for rejection state transitions Closes gt-svi.5 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
79
internal/cmd/mq.go
Normal file
79
internal/cmd/mq.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/steveyegge/gastown/internal/style"
|
||||
)
|
||||
|
||||
// MQ command flags
|
||||
var (
|
||||
mqRejectReason string
|
||||
mqRejectNotify bool
|
||||
)
|
||||
|
||||
var mqCmd = &cobra.Command{
|
||||
Use: "mq",
|
||||
Short: "Merge queue operations",
|
||||
Long: `Manage the merge queue for a rig.
|
||||
|
||||
The merge queue tracks work branches from polecats waiting to be merged.
|
||||
Use these commands to view, submit, and manage merge requests.`,
|
||||
}
|
||||
|
||||
var mqRejectCmd = &cobra.Command{
|
||||
Use: "reject <rig> <mr-id-or-branch>",
|
||||
Short: "Reject a merge request",
|
||||
Long: `Manually reject a merge request.
|
||||
|
||||
This closes the MR with a 'rejected' status without merging.
|
||||
The source issue is NOT closed (work is not done).
|
||||
|
||||
Examples:
|
||||
gt mq reject gastown polecat/Nux/gt-xyz --reason "Does not meet requirements"
|
||||
gt mq reject gastown mr-Nux-12345 --reason "Superseded by other work" --notify`,
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: runMQReject,
|
||||
}
|
||||
|
||||
func init() {
|
||||
// Reject flags
|
||||
mqRejectCmd.Flags().StringVarP(&mqRejectReason, "reason", "r", "", "Reason for rejection (required)")
|
||||
mqRejectCmd.Flags().BoolVar(&mqRejectNotify, "notify", false, "Send mail notification to worker")
|
||||
mqRejectCmd.MarkFlagRequired("reason")
|
||||
|
||||
// Add subcommands
|
||||
mqCmd.AddCommand(mqRejectCmd)
|
||||
|
||||
rootCmd.AddCommand(mqCmd)
|
||||
}
|
||||
|
||||
func runMQReject(cmd *cobra.Command, args []string) error {
|
||||
rigName := args[0]
|
||||
mrIDOrBranch := args[1]
|
||||
|
||||
mgr, _, err := getRefineryManager(rigName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
result, err := mgr.RejectMR(mrIDOrBranch, mqRejectReason, mqRejectNotify)
|
||||
if err != nil {
|
||||
return fmt.Errorf("rejecting MR: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("%s Rejected: %s\n", style.Bold.Render("✗"), result.Branch)
|
||||
fmt.Printf(" Worker: %s\n", result.Worker)
|
||||
fmt.Printf(" Reason: %s\n", mqRejectReason)
|
||||
|
||||
if result.IssueID != "" {
|
||||
fmt.Printf(" Issue: %s %s\n", result.IssueID, style.Dim.Render("(not closed - work not done)"))
|
||||
}
|
||||
|
||||
if mqRejectNotify {
|
||||
fmt.Printf(" %s\n", style.Dim.Render("Worker notified via mail"))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -551,6 +551,88 @@ Thank you for your contribution!`,
|
||||
router.Send(msg)
|
||||
}
|
||||
|
||||
// Common errors for MR operations
|
||||
var (
|
||||
ErrMRNotFound = errors.New("merge request not found")
|
||||
)
|
||||
|
||||
// FindMR finds a merge request by ID or branch name.
|
||||
// Returns nil if not found.
|
||||
func (m *Manager) FindMR(idOrBranch string) (*MergeRequest, error) {
|
||||
queue, err := m.Queue()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, item := range queue {
|
||||
// Match by ID
|
||||
if item.MR.ID == idOrBranch {
|
||||
return item.MR, nil
|
||||
}
|
||||
// Match by branch name (with or without polecat/ prefix)
|
||||
if item.MR.Branch == idOrBranch {
|
||||
return item.MR, nil
|
||||
}
|
||||
if "polecat/"+idOrBranch == item.MR.Branch {
|
||||
return item.MR, nil
|
||||
}
|
||||
// Match by worker name (partial match for convenience)
|
||||
if strings.Contains(item.MR.ID, idOrBranch) {
|
||||
return item.MR, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, ErrMRNotFound
|
||||
}
|
||||
|
||||
// RejectMR manually rejects a merge request.
|
||||
// It closes the MR with rejected status and optionally notifies the worker.
|
||||
// Returns the rejected MR for display purposes.
|
||||
func (m *Manager) RejectMR(idOrBranch string, reason string, notify bool) (*MergeRequest, error) {
|
||||
mr, err := m.FindMR(idOrBranch)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Verify MR is open or in_progress (can't reject already closed)
|
||||
if mr.IsClosed() {
|
||||
return nil, fmt.Errorf("%w: MR is already closed with reason: %s", ErrClosedImmutable, mr.CloseReason)
|
||||
}
|
||||
|
||||
// Close with rejected reason
|
||||
if err := mr.Close(CloseReasonRejected); err != nil {
|
||||
return nil, fmt.Errorf("failed to close MR: %w", err)
|
||||
}
|
||||
mr.Error = reason
|
||||
|
||||
// Optionally notify worker
|
||||
if notify {
|
||||
m.notifyWorkerRejected(mr, reason)
|
||||
}
|
||||
|
||||
return mr, nil
|
||||
}
|
||||
|
||||
// notifyWorkerRejected sends a rejection notification to a polecat.
|
||||
func (m *Manager) notifyWorkerRejected(mr *MergeRequest, reason string) {
|
||||
router := mail.NewRouter(m.workDir)
|
||||
msg := &mail.Message{
|
||||
From: fmt.Sprintf("%s/refinery", m.rig.Name),
|
||||
To: fmt.Sprintf("%s/%s", m.rig.Name, mr.Worker),
|
||||
Subject: "Merge request rejected",
|
||||
Body: fmt.Sprintf(`Your merge request has been rejected.
|
||||
|
||||
Branch: %s
|
||||
Issue: %s
|
||||
Reason: %s
|
||||
|
||||
Please review the feedback and address the issues before resubmitting.`,
|
||||
mr.Branch, mr.IssueID, reason),
|
||||
Priority: mail.PriorityNormal,
|
||||
}
|
||||
router.Send(msg)
|
||||
}
|
||||
|
||||
// findTownRoot walks up directories to find the town root.
|
||||
func findTownRoot(startPath string) string {
|
||||
path := startPath
|
||||
|
||||
@@ -255,3 +255,48 @@ func TestMergeRequest_StatusChecks(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeRequest_Rejection(t *testing.T) {
|
||||
t.Run("reject from open succeeds", func(t *testing.T) {
|
||||
mr := &MergeRequest{Status: MROpen}
|
||||
err := mr.Close(CloseReasonRejected)
|
||||
if err != nil {
|
||||
t.Errorf("Close(rejected) unexpected error: %v", err)
|
||||
}
|
||||
if mr.Status != MRClosed {
|
||||
t.Errorf("Close(rejected) status = %s, want %s", mr.Status, MRClosed)
|
||||
}
|
||||
if mr.CloseReason != CloseReasonRejected {
|
||||
t.Errorf("Close(rejected) closeReason = %s, want %s", mr.CloseReason, CloseReasonRejected)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("reject from in_progress succeeds", func(t *testing.T) {
|
||||
mr := &MergeRequest{Status: MRInProgress}
|
||||
err := mr.Close(CloseReasonRejected)
|
||||
if err != nil {
|
||||
t.Errorf("Close(rejected) unexpected error: %v", err)
|
||||
}
|
||||
if mr.Status != MRClosed {
|
||||
t.Errorf("Close(rejected) status = %s, want %s", mr.Status, MRClosed)
|
||||
}
|
||||
if mr.CloseReason != CloseReasonRejected {
|
||||
t.Errorf("Close(rejected) closeReason = %s, want %s", mr.CloseReason, CloseReasonRejected)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("reject from closed fails", func(t *testing.T) {
|
||||
mr := &MergeRequest{Status: MRClosed, CloseReason: CloseReasonMerged}
|
||||
err := mr.Close(CloseReasonRejected)
|
||||
if err == nil {
|
||||
t.Error("Close(rejected) expected error, got nil")
|
||||
}
|
||||
if !errors.Is(err, ErrClosedImmutable) {
|
||||
t.Errorf("Close(rejected) error = %v, want %v", err, ErrClosedImmutable)
|
||||
}
|
||||
// CloseReason should not change
|
||||
if mr.CloseReason != CloseReasonMerged {
|
||||
t.Errorf("Close(rejected) closeReason changed from %s to %s", CloseReasonMerged, mr.CloseReason)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user