package forge import ( "context" "encoding/json" "errors" "io" "net/http" "os" "os/exec" "path/filepath" "strings" "syscall" "time" "unicode/utf8" "amadan.net/oficina/equipo/internal/equipo/team" ) // Set may return team.ErrNotDispatched only when no command started. // Every failure after Start must remain uncertain, even a nonzero exit. type DescriptionWriter interface { Set(context.Context, Credential, Description) error } type descriptionTransport struct { client *http.Client writer DescriptionWriter } func NewDescriptionTransport(client *http.Client, writer DescriptionWriter) DescriptionTransport { return &descriptionTransport{client: client, writer: writer} } func (*descriptionTransport) BeforePush() bool { return false } func (t *descriptionTransport) Set(ctx context.Context, c Credential, d Description) error { if t.writer == nil { return team.ErrNotDispatched } return t.writer.Set(ctx, c, d) } func (t *descriptionTransport) Read(ctx context.Context, c Credential, repo, ref string) (Description, error) { return ReadBranchDescription(ctx, t.client, c, repo, ref) } type commandDescriptionWriter struct{ path, scratch string } // NewCommandDescriptionWriter scopes the installed CLI to one supplied token. // The temporary command state contains no personal login or provider home. func NewCommandDescriptionWriter(path, scratch string) DescriptionWriter { return commandDescriptionWriter{path: path, scratch: scratch} } func (w commandDescriptionWriter) Set(ctx context.Context, c Credential, d Description) (err error) { started := false defer func() { if err != nil && !started { err = errors.Join(team.ErrNotDispatched, err) } }() if !filepath.IsAbs(w.path) || filepath.Clean(w.path) != w.path { return team.ErrInvalid } if !validDescriptionTarget(c, d.Repository, d.Ref) || d.Agent != c.Agent || d.Body == "" || !utf8.ValidString(d.Body) || len(d.Body) > 1<<20 || d.Digest != Digest([]byte(d.Body)) || !filepath.IsAbs(w.scratch) { return team.ErrInvalid } ctx, cancel := context.WithTimeout(ctx, 15*time.Second) defer cancel() dir, err := os.MkdirTemp(w.scratch, "describe-") if err != nil { return team.ErrUncertain } defer os.RemoveAll(dir) credentials, err := json.Marshal(map[string]string{"https://amadan.net": c.Bearer}) if err != nil { return team.ErrUncertain } if err = os.WriteFile(filepath.Join(dir, "credentials"), credentials, 0600); err != nil { return team.ErrUncertain } cmd := exec.CommandContext(ctx, w.path, "branch", "describe", strings.TrimPrefix(d.Repository, "amadan.net/"), d.Ref, "-hub", "https://amadan.net", "-body", "-") cmd.Dir = dir cmd.Env = []string{"HOME=/nonexistent", "PATH=/usr/bin:/bin", "AMADAN_HOME=" + dir} cmd.Stdin = strings.NewReader(d.Body) cmd.Stdout, cmd.Stderr = io.Discard, io.Discard cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} cmd.Cancel = func() error { return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) } cmd.WaitDelay = time.Second if err = cmd.Start(); err != nil { return team.ErrUncertain } started = true if err = cmd.Wait(); err != nil { return team.ErrUncertain } return nil } // SweepDescriptions runs under the runner state lock before new commands start. func SweepDescriptions(root string) error { entries, err := os.ReadDir(root) if err != nil { return err } for _, entry := range entries { if strings.HasPrefix(entry.Name(), "describe-") && entry.IsDir() { if err = os.RemoveAll(filepath.Join(root, entry.Name())); err != nil { return err } } } return nil }