fix(webui): link tasks to their repository connection's forge
This commit is contained in:
4 files changed
+122
-9
No files matched your search
@@ -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
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user