feat(prdescribe): add configurable base branch flag
Some checks failed
Some checks failed
Introduce a new --base flag (default: "master") to specify the base branch for diff comparison in the PR describe command. Update logic to use this base in git diff commands and fallback to origin/base. Adjust no-changes message accordingly. Add tests for custom base and default behavior.
This commit is contained in:
parent
11faf95573
commit
b1d3a445ec
@ -14,17 +14,23 @@ var prDescribeCmd = &cobra.Command{
|
|||||||
Run: runPRDescribe,
|
Run: runPRDescribe,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
prDescribeCmd.Flags().StringP("base", "b", "master", "Base branch to compare against")
|
||||||
|
}
|
||||||
|
|
||||||
func runPRDescribe(cmd *cobra.Command, args []string) {
|
func runPRDescribe(cmd *cobra.Command, args []string) {
|
||||||
diff, err := gitRun([]string{"diff", "main..HEAD", "--no-color"})
|
base, _ := cmd.Flags().GetString("base")
|
||||||
|
|
||||||
|
diff, err := gitRun([]string{"diff", fmt.Sprintf("%s..HEAD", base), "--no-color"})
|
||||||
if err != nil || diff == "" {
|
if err != nil || diff == "" {
|
||||||
diff, err = gitRun([]string{"diff", "origin/main..HEAD", "--no-color"})
|
diff, err = gitRun([]string{"diff", fmt.Sprintf("origin/%s..HEAD", base), "--no-color"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Red("Failed to get branch diff: %v", err)
|
color.Red("Failed to get branch diff: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if diff == "" {
|
if diff == "" {
|
||||||
color.Yellow("No changes on this branch compared to main/origin/main.")
|
color.Yellow("No changes on this branch compared to %s/origin/%s.", base, base)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
modelFlag, _ := cmd.Flags().GetString("model")
|
modelFlag, _ := cmd.Flags().GetString("model")
|
||||||
|
|||||||
@ -47,10 +47,11 @@ func withMockGit(fn func([]string) (string, error)) func() {
|
|||||||
return func() { gitRun = orig }
|
return func() { gitRun = orig }
|
||||||
}
|
}
|
||||||
|
|
||||||
// testCmd returns a minimal cobra command with the model flag registered.
|
// testCmd returns a minimal cobra command with common flags registered.
|
||||||
func testCmd() *cobra.Command {
|
func testCmd() *cobra.Command {
|
||||||
c := &cobra.Command{}
|
c := &cobra.Command{}
|
||||||
c.Flags().String("model", "", "")
|
c.Flags().String("model", "", "")
|
||||||
|
c.Flags().String("base", "master", "")
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,22 +309,62 @@ func TestRunPRDescribe(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("second diff error — skips AI", func(t *testing.T) {
|
t.Run("uses custom base branch", func(t *testing.T) {
|
||||||
mock := &mockStreamer{}
|
mock := &mockStreamer{response: "PR description"}
|
||||||
defer withMockClient(mock)()
|
defer withMockClient(mock)()
|
||||||
callCount := 0
|
var capturedArgs []string
|
||||||
defer withMockGit(func(args []string) (string, error) {
|
defer withMockGit(func(args []string) (string, error) {
|
||||||
callCount++
|
capturedArgs = args
|
||||||
if callCount == 2 {
|
return "diff content", nil
|
||||||
return "", errors.New("no remote")
|
})()
|
||||||
|
|
||||||
|
cmd := testCmd()
|
||||||
|
if err := cmd.Flags().Set("base", "develop"); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
runPRDescribe(cmd, nil)
|
||||||
|
|
||||||
|
if mock.calls != 1 {
|
||||||
|
t.Errorf("expected 1 AI call, got %d", mock.calls)
|
||||||
|
}
|
||||||
|
// Expect "diff", "develop..HEAD", "--no-color"
|
||||||
|
expectedArg := "develop..HEAD"
|
||||||
|
found := false
|
||||||
|
for _, arg := range capturedArgs {
|
||||||
|
if arg == expectedArg {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
}
|
}
|
||||||
return "", nil
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("expected arg %q not found in %v", expectedArg, capturedArgs)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("defaults to master", func(t *testing.T) {
|
||||||
|
mock := &mockStreamer{response: "PR description"}
|
||||||
|
defer withMockClient(mock)()
|
||||||
|
var capturedArgs []string
|
||||||
|
defer withMockGit(func(args []string) (string, error) {
|
||||||
|
capturedArgs = args
|
||||||
|
return "diff content", nil
|
||||||
})()
|
})()
|
||||||
|
|
||||||
runPRDescribe(testCmd(), nil)
|
runPRDescribe(testCmd(), nil)
|
||||||
|
|
||||||
if mock.calls != 0 {
|
if mock.calls != 1 {
|
||||||
t.Errorf("expected 0 AI calls, got %d", mock.calls)
|
t.Errorf("expected 1 AI call, got %d", mock.calls)
|
||||||
|
}
|
||||||
|
expectedArg := "master..HEAD"
|
||||||
|
found := false
|
||||||
|
for _, arg := range capturedArgs {
|
||||||
|
if arg == expectedArg {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !found {
|
||||||
|
t.Errorf("expected arg %q not found in %v", expectedArg, capturedArgs)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user