feat(daemon): execute and verify organisation repository connections
This commit is contained in:
1 parent
32642af4a9
commit
05987f04e2
45 files changed
+1600
-284
No files matched your search
@@ -143,6 +143,30 @@ func TestForgeHTTPAuthentication(t *testing.T) {
|
||||
if _, _, err := m.Prepare(t.Context(), "org", "repo", "main", issue, "test", "", "", Target(branch)); err != nil {
|
||||
t.Fatalf("authenticated fetch: %v", err)
|
||||
}
|
||||
if err := m.CheckAccess(t.Context(), "org", "repo", "main"); err != nil {
|
||||
t.Fatalf("read-only access check: %v", err)
|
||||
}
|
||||
if err := m.CheckAccess(t.Context(), "org", "repo", "missing"); err == nil {
|
||||
t.Fatal("missing base branch verified")
|
||||
}
|
||||
cloned, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
changed, err := cloned.Config()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
changed.Remotes["origin"].URLs = []string{target.URL + "/org/repo.git"}
|
||||
if err := cloned.SetConfig(changed); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := m.PushRepository(t.Context(), "org", "repo", issue, branch); err != nil {
|
||||
t.Fatalf("push to authorised connection: %v", err)
|
||||
}
|
||||
if _, _, err := m.Prepare(t.Context(), "org", "repo", "main", issue, "test", "", "", Fresh); err == nil {
|
||||
t.Fatal("prepared worktree from another origin was reused")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import (
|
||||
fdiff "github.com/go-git/go-git/v6/plumbing/format/diff"
|
||||
"github.com/go-git/go-git/v6/plumbing/object"
|
||||
githttp "github.com/go-git/go-git/v6/plumbing/transport/http"
|
||||
"github.com/go-git/go-git/v6/storage/memory"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
|
||||
)
|
||||
@@ -40,6 +41,7 @@ type Manager struct {
|
||||
// and passed to go-git directly, never written to .git/config, an
|
||||
// askpass helper, or a child process environment.
|
||||
Token string
|
||||
AuthUser string
|
||||
BotUser string
|
||||
BotEmail string
|
||||
// BaseURL overrides the forge host. Set from config [forge].host.
|
||||
@@ -57,6 +59,12 @@ func (m *Manager) logger() *slog.Logger {
|
||||
return slog.Default()
|
||||
}
|
||||
|
||||
// OrgWorkDir confines an organisation's worktrees beneath root, including
|
||||
// legacy organisation IDs that contain filesystem separators.
|
||||
func OrgWorkDir(root, org string) string {
|
||||
return filepath.Join(root, "org-"+base64.RawURLEncoding.EncodeToString([]byte(org)))
|
||||
}
|
||||
|
||||
// Dir is the worktree path for a task.
|
||||
func (m *Manager) Dir(owner, repo string, issue int) string {
|
||||
ownerKey := base64.RawURLEncoding.EncodeToString([]byte(owner))
|
||||
@@ -77,7 +85,7 @@ func (m *Manager) auth() []gitclient.Option {
|
||||
if m.Token == "" {
|
||||
return nil
|
||||
}
|
||||
username := m.BotUser
|
||||
username := m.AuthUser
|
||||
if username == "" {
|
||||
// Any non-empty username is accepted alongside a token; this one
|
||||
// makes the origin obvious in a forge's audit log.
|
||||
@@ -130,6 +138,9 @@ func (m *Manager) Prepare(
|
||||
prepared = migrated
|
||||
}
|
||||
|
||||
if prepared && !repositoryUsesURL(dir, m.cloneURL(owner, repo)) {
|
||||
return "", "", fmt.Errorf("the worktree belongs to a different repository connection; archive it before starting again")
|
||||
}
|
||||
if !prepared {
|
||||
if err := m.createClone(ctx, owner, repo, base, dir); err != nil {
|
||||
return "", "", err
|
||||
@@ -466,18 +477,29 @@ func (m *Manager) CommitAll(ctx context.Context, dir, message string) (bool, err
|
||||
|
||||
// Push publishes the branch and sets it to track origin.
|
||||
func (m *Manager) Push(ctx context.Context, dir, branch string) error {
|
||||
return m.push(ctx, dir, branch, "")
|
||||
}
|
||||
|
||||
// PushRepository pins the destination to the authorised connection rather
|
||||
// than trusting origin in a container-writable Git configuration.
|
||||
func (m *Manager) PushRepository(ctx context.Context, owner, repo string, issue int, branch string) error {
|
||||
return m.push(ctx, m.Dir(owner, repo, issue), branch, m.cloneURL(owner, repo))
|
||||
}
|
||||
|
||||
func (m *Manager) push(ctx context.Context, dir, branch, remoteURL string) error {
|
||||
r, err := git.PlainOpen(dir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("open worktree: %w", err)
|
||||
}
|
||||
if m.Token == "" && remoteUsesHTTP(r) {
|
||||
return fmt.Errorf("push %s: no forge credential configured; "+
|
||||
"set [forge].token (or the identity's forge.token) so archied can authenticate", branch)
|
||||
"check the repository connection's credential", branch)
|
||||
}
|
||||
ref := plumbing.NewBranchReferenceName(branch)
|
||||
spec := gitconfig.RefSpec(fmt.Sprintf("%s:%s", ref, ref))
|
||||
if err := r.PushContext(ctx, &git.PushOptions{
|
||||
RemoteName: git.DefaultRemoteName,
|
||||
RemoteURL: remoteURL,
|
||||
RefSpecs: []gitconfig.RefSpec{spec},
|
||||
ClientOptions: m.auth(),
|
||||
}); err != nil && !errors.Is(err, git.NoErrAlreadyUpToDate) {
|
||||
@@ -990,3 +1012,21 @@ func (m *Manager) cloneURL(owner, repo string) string {
|
||||
}
|
||||
return fmt.Sprintf("https://github.com/%s/%s.git", owner, repo)
|
||||
}
|
||||
|
||||
var ErrBaseBranchNotFound = errors.New("base branch not found")
|
||||
|
||||
// CheckAccess checks the same authenticated Git endpoint as Prepare, without
|
||||
// creating a clone, branch, or commit.
|
||||
func (m *Manager) CheckAccess(ctx context.Context, owner, repo, base string) error {
|
||||
remote := git.NewRemote(memory.NewStorage(), &gitconfig.RemoteConfig{Name: "origin", URLs: []string{m.cloneURL(owner, repo)}})
|
||||
refs, err := remote.ListContext(ctx, &git.ListOptions{ClientOptions: m.auth()})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, ref := range refs {
|
||||
if ref.Name() == plumbing.NewBranchReferenceName(base) {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("%w: %s", ErrBaseBranchNotFound, base)
|
||||
}
|
||||
Reference in new issue
Block a user