diff --git a/cmd/mesh-controller/facts.go b/cmd/mesh-controller/facts.go index 4bf271f..3990026 100644 --- a/cmd/mesh-controller/facts.go +++ b/cmd/mesh-controller/facts.go @@ -406,11 +406,11 @@ func gatherFacts(ctx context.Context, open *stores, busVersion string) (snapshot if err != nil { return snapshot.Facts{}, err } - mod := snapshot.Module{Name: e.Manifest.Module, Repository: e.Source.Repository, Path: e.Source.Path, + mod := snapshot.Module{Name: e.Manifest.Module, Repository: snapshot.RepositoryName(e.Source.Repository), Path: e.Source.Path, Commit: e.Source.BuiltFrom, Provided: e.Provided, RollOut: current[e.Manifest.Module].RollOut, Manifest: raw} for _, r := range read[e.Manifest.Module] { - mod.Reads = append(mod.Reads, r.Repository) + mod.Reads = append(mod.Reads, snapshot.RepositoryName(r.Repository)) } f.Modules = append(f.Modules, mod) if e.Provided || e.Source.Repository == "" { @@ -426,7 +426,8 @@ func gatherFacts(ctx context.Context, open *stores, busVersion string) (snapshot if commit == "" { commit = s.BuiltFrom } - f.Sources = append(f.Sources, snapshot.Source{Repository: repository, Commit: commit, Modules: count[repository]}) + f.Sources = append(f.Sources, snapshot.Source{Repository: snapshot.RepositoryName(repository), Commit: commit, + Modules: count[repository]}) } for _, e := range edges { f.Edges = append(f.Edges, snapshot.Edge{From: e.From, To: e.To, Kind: e.Kind}) diff --git a/internal/facts/facts_test.go b/internal/facts/facts_test.go index 56021a9..bef9959 100644 --- a/internal/facts/facts_test.go +++ b/internal/facts/facts_test.go @@ -172,3 +172,18 @@ func TestAWithheldPathStaysAPath(t *testing.T) { t.Errorf("a key became %q", got) } } + +// **Issue 288**: a repository was kept as the URL it was cloned from, the forge's address with it. +func TestARepositoryIsNamedWithoutTheForge(t *testing.T) { + for in, want := range map[string]string{ + "http://forge.internal:3000/owner/repo.git": "owner/repo", + "ssh://git@forge.internal:222/owner/repo": "owner/repo", + "git@forge.internal:owner/repo.git": "owner/repo", + "owner/repo": "owner/repo", + "": "", + } { + if got := RepositoryName(in); got != want { + t.Errorf("%q is named %q, not %q", in, got, want) + } + } +} diff --git a/internal/facts/scrub.go b/internal/facts/scrub.go index 2f310ed..4615bb3 100644 --- a/internal/facts/scrub.go +++ b/internal/facts/scrub.go @@ -321,3 +321,31 @@ func standIn(run string) string { } return run } + +// RepositoryName is a repository as `owner/repository`, without the forge's address it was cloned from: +// http://forge.internal:3000/owner/repo.git → owner/repo (novox/hq issue 288). What a check matches a +// pull request's repository by is its owner and name, never the forge's address, so nothing is lost. +func RepositoryName(repository string) string { + r := strings.TrimSuffix(strings.TrimSuffix(strings.TrimSpace(repository), "/"), ".git") + if r == "" { + return "" + } + if _, rest, found := strings.Cut(r, "://"); found { + r = rest + if _, path, found := strings.Cut(r, "/"); found { + r = path + } else { + return "" + } + } else if at := strings.Index(r, "@"); at >= 0 { + // scp-like: git@forge:owner/repo + if _, path, found := strings.Cut(r[at+1:], ":"); found { + r = path + } + } + parts := strings.Split(strings.Trim(r, "/"), "/") + if len(parts) >= 2 { + return parts[len(parts)-2] + "/" + parts[len(parts)-1] + } + return parts[len(parts)-1] +}