oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package forge
2
3import (
4 "context"
5 "encoding/json"
6 "errors"
7 "io"
8 "net/http"
9 "os"
10 "os/exec"
11 "path/filepath"
12 "strings"
13 "syscall"
14 "time"
15 "unicode/utf8"
16
17 "amadan.net/oficina/equipo/internal/equipo/team"
18)
19
20// Set may return team.ErrNotDispatched only when no command started.
21// Every failure after Start must remain uncertain, even a nonzero exit.
22type DescriptionWriter interface {
23 Set(context.Context, Credential, Description) error
24}
25
26type descriptionTransport struct {
27 client *http.Client
28 writer DescriptionWriter
29}
30
31func NewDescriptionTransport(client *http.Client, writer DescriptionWriter) DescriptionTransport {
32 return &descriptionTransport{client: client, writer: writer}
33}
34func (*descriptionTransport) BeforePush() bool { return false }
35func (t *descriptionTransport) Set(ctx context.Context, c Credential, d Description) error {
36 if t.writer == nil {
37 return team.ErrNotDispatched
38 }
39 return t.writer.Set(ctx, c, d)
40}
41func (t *descriptionTransport) Read(ctx context.Context, c Credential, repo, ref string) (Description, error) {
42 return ReadBranchDescription(ctx, t.client, c, repo, ref)
43}
44
45type commandDescriptionWriter struct{ path, scratch string }
46
47// NewCommandDescriptionWriter scopes the installed CLI to one supplied token.
48// The temporary command state contains no personal login or provider home.
49func NewCommandDescriptionWriter(path, scratch string) DescriptionWriter {
50 return commandDescriptionWriter{path: path, scratch: scratch}
51}
52func (w commandDescriptionWriter) Set(ctx context.Context, c Credential, d Description) (err error) {
53 started := false
54 defer func() {
55 if err != nil && !started {
56 err = errors.Join(team.ErrNotDispatched, err)
57 }
58 }()
59 if !filepath.IsAbs(w.path) || filepath.Clean(w.path) != w.path {
60 return team.ErrInvalid
61 }
62 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) {
63 return team.ErrInvalid
64 }
65 ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
66 defer cancel()
67 dir, err := os.MkdirTemp(w.scratch, "describe-")
68 if err != nil {
69 return team.ErrUncertain
70 }
71 defer os.RemoveAll(dir)
72 credentials, err := json.Marshal(map[string]string{"https://amadan.net": c.Bearer})
73 if err != nil {
74 return team.ErrUncertain
75 }
76 if err = os.WriteFile(filepath.Join(dir, "credentials"), credentials, 0600); err != nil {
77 return team.ErrUncertain
78 }
79 cmd := exec.CommandContext(ctx, w.path, "branch", "describe", strings.TrimPrefix(d.Repository, "amadan.net/"), d.Ref, "-hub", "https://amadan.net", "-body", "-")
80 cmd.Dir = dir
81 cmd.Env = []string{"HOME=/nonexistent", "PATH=/usr/bin:/bin", "AMADAN_HOME=" + dir}
82 cmd.Stdin = strings.NewReader(d.Body)
83 cmd.Stdout, cmd.Stderr = io.Discard, io.Discard
84 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
85 cmd.Cancel = func() error { return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) }
86 cmd.WaitDelay = time.Second
87 if err = cmd.Start(); err != nil {
88 return team.ErrUncertain
89 }
90 started = true
91 if err = cmd.Wait(); err != nil {
92 return team.ErrUncertain
93 }
94 return nil
95}
96
97// SweepDescriptions runs under the runner state lock before new commands start.
98func SweepDescriptions(root string) error {
99 entries, err := os.ReadDir(root)
100 if err != nil {
101 return err
102 }
103 for _, entry := range entries {
104 if strings.HasPrefix(entry.Name(), "describe-") && entry.IsDir() {
105 if err = os.RemoveAll(filepath.Join(root, entry.Name())); err != nil {
106 return err
107 }
108 }
109 }
110 return nil
111}
112