diff --git a/cmd/issues.go b/cmd/issues.go index 05ce2f0..c9ff28e 100644 --- a/cmd/issues.go +++ b/cmd/issues.go @@ -81,6 +81,9 @@ func runIssues(ctx stdctx.Context, cmd *cli.Command) error { func runIssueDetail(_ stdctx.Context, cmd *cli.Command, index string) error { ctx := context.InitCommand(cmd) + if ctx.IsSet("owner") { + ctx.Owner = ctx.String("owner") + } ctx.Ensure(context.CtxRequirement{RemoteRepo: true}) idx, err := utils.ArgToIndex(index) diff --git a/cmd/issues_test.go b/cmd/issues_test.go index 2785624..48c79ff 100644 --- a/cmd/issues_test.go +++ b/cmd/issues_test.go @@ -5,6 +5,7 @@ package cmd import ( "bytes" + stdctx "context" "encoding/json" "fmt" "net/http" @@ -13,6 +14,7 @@ import ( "time" "code.gitea.io/sdk/gitea" + "code.gitea.io/tea/cmd/flags" "code.gitea.io/tea/modules/config" "code.gitea.io/tea/modules/context" "github.com/stretchr/testify/assert" @@ -262,3 +264,78 @@ func TestRunIssueDetailAsJSON(t *testing.T) { }) } } + +func TestRunIssueDetailUsesOwnerFlag(t *testing.T) { + issueIndex := int64(12) + expectedOwner := "overrideOwner" + expectedRepo := "overrideRepo" + issue := gitea.Issue{ + ID: 99, + Index: issueIndex, + Title: "Owner override test", + State: gitea.StateOpen, + Created: time.Date(2025, 11, 1, 10, 0, 0, 0, time.UTC), + Poster: &gitea.User{ + UserName: "tester", + }, + HTMLURL: "https://example.test/issues/12", + } + + handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + switch r.URL.Path { + case fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d", expectedOwner, expectedRepo, issueIndex): + jsonIssue, err := json.Marshal(issue) + require.NoError(t, err, "Testing setup failed: failed to marshal issue") + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, err = w.Write(jsonIssue) + require.NoError(t, err, "Testing setup failed: failed to write issue") + case fmt.Sprintf("/api/v1/repos/%s/%s/issues/%d/reactions", expectedOwner, expectedRepo, issueIndex): + jsonReactions, err := json.Marshal([]gitea.Reaction{}) + require.NoError(t, err, "Testing setup failed: failed to marshal reactions") + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, err = w.Write(jsonReactions) + require.NoError(t, err, "Testing setup failed: failed to write reactions") + default: + http.NotFound(w, r) + } + }) + + server := httptest.NewServer(handler) + defer server.Close() + + config.SetConfigForTesting(config.LocalConfig{ + Logins: []config.Login{{ + Name: "testLogin", + URL: server.URL, + Token: "token", + User: "loginUser", + Default: true, + }}, + }) + + cmd := cli.Command{ + Name: "issues", + Flags: []cli.Flag{ + &flags.LoginFlag, + &flags.RepoFlag, + &flags.RemoteFlag, + &flags.OutputFlag, + &cli.StringFlag{Name: "owner"}, + &cli.BoolFlag{Name: "comments"}, + }, + } + var outBuffer bytes.Buffer + var errBuffer bytes.Buffer + cmd.Writer = &outBuffer + cmd.ErrWriter = &errBuffer + require.NoError(t, cmd.Set("login", "testLogin")) + require.NoError(t, cmd.Set("repo", expectedRepo)) + require.NoError(t, cmd.Set("owner", expectedOwner)) + require.NoError(t, cmd.Set("output", "json")) + require.NoError(t, cmd.Set("comments", "false")) + + err := runIssueDetail(stdctx.Background(), &cmd, fmt.Sprintf("%d", issueIndex)) + require.NoError(t, err, "Expected runIssueDetail to succeed") +} diff --git a/modules/config/config.go b/modules/config/config.go index f958b57..d5be554 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -122,6 +122,13 @@ func reloadConfigFromDisk() error { return nil } +// SetConfigForTesting replaces the in-memory config and marks it as loaded. +// This allows tests to inject config without relying on file-based loading. +func SetConfigForTesting(cfg LocalConfig) { + loadConfigOnce.Do(func() {}) // ensure sync.Once is spent + config = cfg +} + // saveConfigUnsafe saves config to file without acquiring a lock. // Caller must hold the config lock. func saveConfigUnsafe() error {