From 184038a9182e87a7499bab17cf57dd14ddf67c57 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sun, 8 Nov 2026 06:42:38 +1100 Subject: [PATCH] fix(webui): link tasks to their repository connection's forge --- CHANGELOG.md | 4 ++ internal/webui/tasks.go | 3 ++ internal/webui/tasks_handlers.go | 61 ++++++++++++++++++++++++----- internal/webui/tasks_links_test.go | 63 ++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 9 deletions(-) create mode 100644 internal/webui/tasks_links_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f9cfc4ad..2245fb3a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ release's per-component sections are labelled beneath its heading. ## [Unreleased] +### archie-ui + +- Issue, pull request and repository links on tasks open on the forge the repository's connection points at, instead of GitHub. + ## [1.61.2] - 2026-11-07 ### archied diff --git a/internal/webui/tasks.go b/internal/webui/tasks.go index dcec009b0..399c4d3c0 100644 --- a/internal/webui/tasks.go +++ b/internal/webui/tasks.go @@ -4,6 +4,7 @@ import ( "log/slog" "net/http" + controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/workflow/task" ) @@ -22,6 +23,7 @@ type tasksAPI struct { authorize func(http.ResponseWriter, *http.Request) bool configSource func() ConfigViewSource recovery func(*http.Request, *task.Task) *taskRecoveryView + controlPlane controlpb.ControlPlaneServiceClient } func newTasksAPI(s *Server) *tasksAPI { @@ -35,6 +37,7 @@ func newTasksAPI(s *Server) *tasksAPI { authorize: s.authorizeTaskMutation, configSource: s.configSource, recovery: s.taskRecovery, + controlPlane: s.ControlPlane, } } diff --git a/internal/webui/tasks_handlers.go b/internal/webui/tasks_handlers.go index d426ab87b..3e545e63d 100644 --- a/internal/webui/tasks_handlers.go +++ b/internal/webui/tasks_handlers.go @@ -13,6 +13,8 @@ import ( "strings" "time" + "github.com/samcharles93/archie-core/internal/config" + controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/domain/taskactions" "github.com/samcharles93/archie-core/internal/domain/workflow/task" @@ -141,17 +143,58 @@ type forgeCoordinates struct { forgeType string } -// resolveForge returns a function mapping a task to the instance forge. A -// multi-identity install withholds links: its tasks reach forges through -// organisation connections this projection does not carry. +// resolveForge returns a function mapping a task to the forge its links point +// at: the forge connection its repository policy names, else the instance's +// published forge when there is only one identity, else none. func (a *tasksAPI) resolveForge(ctx context.Context) func(task.Task) forgeCoordinates { - unlinked := func(task.Task) forgeCoordinates { return forgeCoordinates{} } - view, ok, err := a.configSource()(ctx) - if err != nil || !ok || view.MultiIdentity { - return unlinked + fallback := forgeCoordinates{} + if view, ok, err := a.configSource()(ctx); err == nil && ok && !view.MultiIdentity { + fallback = forgeCoordinates{host: view.Identity.ForgeHost, forgeType: view.Identity.ForgeType} } - published := forgeCoordinates{host: view.Identity.ForgeHost, forgeType: view.Identity.ForgeType} - return func(task.Task) forgeCoordinates { return published } + byRepo := a.repositoryForges(ctx) + return func(t task.Task) forgeCoordinates { + if forge, ok := byRepo[strings.ToLower(t.Owner+"/"+t.Repo)]; ok { + return forge + } + return fallback + } +} + +// repositoryForges maps each repository policy, by lower-case owner/name, to +// the forge its connection is on. A read failure yields no entries, so links +// fall back rather than fail the page. +func (a *tasksAPI) repositoryForges(ctx context.Context) map[string]forgeCoordinates { + if a.controlPlane == nil { + return nil + } + var repos []config.Repo + var connections []config.ForgeConnection + if !a.readResource(ctx, "repository-policies", &repos) || !a.readResource(ctx, "forge-connections", &connections) { + return nil + } + hosts := make(map[string]forgeCoordinates, len(connections)) + for _, c := range connections { + host := c.Host + if host != "" && !strings.Contains(host, "://") { + host = "https://" + host + } + hosts[c.ID] = forgeCoordinates{host: host, forgeType: c.Type} + } + out := make(map[string]forgeCoordinates, len(repos)) + for _, r := range repos { + if forge, ok := hosts[r.Connection]; ok && forge.host != "" { + out[strings.ToLower(r.Owner+"/"+r.Name)] = forge + } + } + return out +} + +func (a *tasksAPI) readResource(ctx context.Context, kind string, into any) bool { + response, err := a.controlPlane.Query(ctx, &controlpb.QueryRequest{Kind: kind}) + if err != nil || response.Resource == nil { + return false + } + return json.Unmarshal(response.Resource.ValueJson, into) == nil } func taskURLs(task task.Task, forge forgeCoordinates) (repoURL, issueURL, prURL string) { diff --git a/internal/webui/tasks_links_test.go b/internal/webui/tasks_links_test.go new file mode 100644 index 000000000..d9301b2b9 --- /dev/null +++ b/internal/webui/tasks_links_test.go @@ -0,0 +1,63 @@ +package webui + +import ( + "context" + "testing" + + "google.golang.org/grpc" + + controlpb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1" + "github.com/samcharles93/archie-core/internal/domain/workflow/task" +) + +// linkControl serves one repository policy on a Gitea connection. +type linkControl struct { + controlpb.ControlPlaneServiceClient +} + +func (linkControl) Query(_ context.Context, r *controlpb.QueryRequest, _ ...grpc.CallOption) (*controlpb.QueryResponse, error) { + value := map[string]string{ + "repository-policies": `[{"owner":"sam","name":"archie","connection":"home"}]`, + "forge-connections": `[{"id":"home","type":"gitea","host":"git.example.test"}]`, + }[r.Kind] + return &controlpb.QueryResponse{Resource: &controlpb.Resource{ValueJson: []byte(value)}}, nil +} + +// A task links to the forge its repository's connection is on, not the +// instance's default forge, which is GitHub unless configured otherwise. +func TestTaskLinksFollowTheRepositoryConnection(t *testing.T) { + api := &tasksAPI{ + controlPlane: linkControl{}, + configSource: func() ConfigViewSource { + return func(context.Context) (ConfigView, bool, error) { + return ConfigView{Identity: IdentityView{ForgeHost: "https://github.com", ForgeType: "github"}}, true, nil + } + }, + } + tests := []struct { + name string + task task.Task + wantIssue string + wantPR string + }{ + { + "repository on a connection", + task.Task{Owner: "Sam", Repo: "archie", IssueNumber: 5, PRNumber: 6}, + "https://git.example.test/Sam/archie/issues/5", "https://git.example.test/Sam/archie/pulls/6", + }, + { + "repository without a policy", + task.Task{Owner: "acme", Repo: "app", IssueNumber: 1}, + "https://github.com/acme/app/issues/1", "", + }, + } + forge := api.resolveForge(context.Background()) + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, issue, pr := taskURLs(tt.task, forge(tt.task)) + if issue != tt.wantIssue || pr != tt.wantPR { + t.Fatalf("issue %q pr %q, want %q and %q", issue, pr, tt.wantIssue, tt.wantPR) + } + }) + } +}