Skip to content

Commit 04178d8

Browse files
Fix DeepSource issues in monorepo PR
- Fix retry loop skipping base-repo fallback when all path segments exhausted - Guard against filepath.Rel returning "." which creates invalid repo suffix - Rename blank receiver `_` to `lo` in PAT login flow - Extract loadGoldenFile helper to reduce test complexity
1 parent 5749e3e commit 04178d8

4 files changed

Lines changed: 26 additions & 21 deletions

File tree

command/auth/login/pat_login_flow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/fatih/color"
1010
)
1111

12-
func (_ *LoginOptions) startPATLoginFlow(svc *authsvc.Service, cfg *config.CLIConfig, token string) error {
12+
func (lo *LoginOptions) startPATLoginFlow(svc *authsvc.Service, cfg *config.CLIConfig, token string) error {
1313
cfg.Token = token
1414

1515
viewer, err := svc.GetViewer(context.Background(), cfg)

command/issues/issues.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,16 @@ func (opts *IssuesOptions) resolveIssuesWithRetry(ctx context.Context, client *d
300300
return nil, err
301301
}
302302

303+
baseName := strings.SplitN(remote.RepoName, ":", 2)[0]
303304
parts := strings.Split(remote.SubRepoSuffix, ":")
304-
for len(parts) > 1 {
305+
for len(parts) > 0 {
305306
parts = parts[:len(parts)-1]
306307
remote.SubRepoSuffix = strings.Join(parts, ":")
307-
baseName := strings.SplitN(remote.RepoName, ":", 2)[0]
308-
remote.RepoName = baseName + ":" + remote.SubRepoSuffix
308+
if remote.SubRepoSuffix == "" {
309+
remote.RepoName = baseName
310+
} else {
311+
remote.RepoName = baseName + ":" + remote.SubRepoSuffix
312+
}
309313

310314
issuesList, err = opts.resolveIssues(ctx, client, remote)
311315
if err == nil {

command/issues/tests/issues_test.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -420,30 +420,27 @@ func TestIssuesMultipleFilters(t *testing.T) {
420420
}
421421
}
422422

423+
// loadGoldenFile reads a golden file and fails the test if it cannot be read.
424+
func loadGoldenFile(t *testing.T, name string) []byte {
425+
t.Helper()
426+
data, err := os.ReadFile(goldenPath(name))
427+
if err != nil {
428+
t.Fatalf("failed to read golden file %s: %v", name, err)
429+
}
430+
return data
431+
}
432+
423433
// TestIssuesPRFallbackUsesRunIssues verifies that when auto-branch resolution
424434
// detects a PR AND the current run is in-progress (fallback), the code fetches
425435
// issues via GetRunIssuesFlat (commit-scoped) instead of GetPRIssues.
426436
// Regression test for ticket #6884175.
427437
func TestIssuesPRFallbackUsesRunIssues(t *testing.T) {
428438
cfgMgr := testutil.CreateTestConfigManager(t, "test-token", "deepsource.com", "test@example.com")
429439

430-
// Load golden files for the multi-step mock.
431-
prFoundData, err := os.ReadFile(goldenPath("get_pr_by_branch_found_response.json"))
432-
if err != nil {
433-
t.Fatalf("failed to read PR golden file: %v", err)
434-
}
435-
runsFirstData, err := os.ReadFile(goldenPath("get_analysis_runs_pr_fallback_first_response.json"))
436-
if err != nil {
437-
t.Fatalf("failed to read first runs golden file: %v", err)
438-
}
439-
runsCompletedData, err := os.ReadFile(goldenPath("get_analysis_runs_pr_fallback_completed_response.json"))
440-
if err != nil {
441-
t.Fatalf("failed to read completed runs golden file: %v", err)
442-
}
443-
commitScopeData, err := os.ReadFile(goldenPath("commit_scope_response.json"))
444-
if err != nil {
445-
t.Fatalf("failed to read commit scope golden file: %v", err)
446-
}
440+
prFoundData := loadGoldenFile(t, "get_pr_by_branch_found_response.json")
441+
runsFirstData := loadGoldenFile(t, "get_analysis_runs_pr_fallback_first_response.json")
442+
runsCompletedData := loadGoldenFile(t, "get_analysis_runs_pr_fallback_completed_response.json")
443+
commitScopeData := loadGoldenFile(t, "commit_scope_response.json")
447444

448445
mock := graphqlclient.NewMockClient()
449446
analysisRunsCalls := 0

internal/vcs/remotes.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ func detectSubRepoPath() string {
156156
return ""
157157
}
158158

159+
if rel == "." {
160+
return ""
161+
}
162+
159163
debug.Log("git: sub-repo relative path %q", rel)
160164
return strings.ReplaceAll(rel, string(os.PathSeparator), ":")
161165
}

0 commit comments

Comments
 (0)