| 1 | package forge |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "net/http" |
| 10 | "net/url" |
| 11 | "strings" |
| 12 | ) |
| 13 | |
| 14 | // ErrCIRead means the hub's CI answer could not be read: a refused or |
| 15 | // failed request, a redirect, an oversized or unreadable body. Callers |
| 16 | // treat it as "not known", never as a verdict. |
| 17 | var ErrCIRead = errors.New("forge: the hub's CI answer could not be read") |
| 18 | |
| 19 | // CI status logs modes the hub knows: none for a poll, failed for the |
| 20 | // logs of the units that failed. |
| 21 | const ( |
| 22 | CILogsNone = "none" |
| 23 | CILogsFailed = "failed" |
| 24 | ) |
| 25 | |
| 26 | // CIStatus is the hub's verdict on a branch's tip, as GET /api/v1/repos/ |
| 27 | // {ns}/{repo}/ci/status?branch= reports it (amadan discussion #8): the tip |
| 28 | // it judged, the run's state, when the hub recorded it, its one-line |
| 29 | // summary of what failed, and the workflow's steps and shards. |
| 30 | type CIStatus struct { |
| 31 | NS string `json:"ns"` |
| 32 | Repo string `json:"repo"` |
| 33 | Branch string `json:"branch"` |
| 34 | SHA string `json:"sha"` |
| 35 | State string `json:"state"` |
| 36 | Summary string `json:"summary"` |
| 37 | UpdatedAt int64 `json:"updated_at"` |
| 38 | Log string `json:"log"` |
| 39 | Steps []CIStep `json:"steps"` |
| 40 | } |
| 41 | |
| 42 | // CIStep is one step of a run, with its shards when it is sharded. |
| 43 | type CIStep struct { |
| 44 | Name string `json:"name"` |
| 45 | State string `json:"state"` |
| 46 | Log string `json:"log"` |
| 47 | Shards []CIShard `json:"shards"` |
| 48 | } |
| 49 | |
| 50 | // CIShard is one shard of a sharded step. |
| 51 | type CIShard struct { |
| 52 | Name string `json:"name"` |
| 53 | State string `json:"state"` |
| 54 | Log string `json:"log"` |
| 55 | } |
| 56 | |
| 57 | // Read caps: the hosted answer is a handful of fields; a status with the |
| 58 | // failing units' logs is bounded by the hub's own log budget. |
| 59 | const ( |
| 60 | maxCIHostedBody = 64 << 10 |
| 61 | maxCIStatusBody = 4 << 20 |
| 62 | ) |
| 63 | |
| 64 | // HubCIHosted asks the hub whether it runs repo's CI on its own machines |
| 65 | // (GET /api/v1/repos/{ns}/{repo}/ci/hosted, any member). |
| 66 | func HubCIHosted(ctx context.Context, client *http.Client, hub string, cred Credential, repo string) (bool, error) { |
| 67 | var out struct { |
| 68 | On *bool `json:"on"` |
| 69 | } |
| 70 | if err := ciGet(ctx, client, hub, cred, repo, "/ci/hosted", nil, maxCIHostedBody, &out); err != nil { |
| 71 | return false, err |
| 72 | } |
| 73 | if out.On == nil { |
| 74 | return false, ErrCIRead |
| 75 | } |
| 76 | return *out.On, nil |
| 77 | } |
| 78 | |
| 79 | // HubCIStatus reads the hub's CI verdict on branch's tip, with logs |
| 80 | // (CILogsNone or CILogsFailed). |
| 81 | func HubCIStatus(ctx context.Context, client *http.Client, hub string, cred Credential, repo, branch, logs string) (CIStatus, error) { |
| 82 | if !validRef(branch) || logs != CILogsNone && logs != CILogsFailed { |
| 83 | return CIStatus{}, ErrCIRead |
| 84 | } |
| 85 | var out CIStatus |
| 86 | if err := ciGet(ctx, client, hub, cred, repo, "/ci/status", url.Values{"branch": {branch}, "logs": {logs}}, maxCIStatusBody, &out); err != nil { |
| 87 | return CIStatus{}, err |
| 88 | } |
| 89 | // The answer must be about exactly what was asked. |
| 90 | parts := strings.Split(strings.TrimPrefix(repo, "amadan.net/"), "/") |
| 91 | if out.NS != parts[0] || out.Repo != parts[1] || out.Branch != branch || out.State == "" || out.SHA != "" && !object.MatchString(out.SHA) { |
| 92 | return CIStatus{}, ErrCIRead |
| 93 | } |
| 94 | return out, nil |
| 95 | } |
| 96 | |
| 97 | func ciGet(ctx context.Context, client *http.Client, hub string, cred Credential, repo, suffix string, q url.Values, limit int64, out any) error { |
| 98 | if !validRepository(repo) || cred.Bearer == "" || strings.ContainsAny(cred.Bearer, "\r\n") { |
| 99 | return ErrCIRead |
| 100 | } |
| 101 | parts := strings.Split(strings.TrimPrefix(repo, "amadan.net/"), "/") |
| 102 | endpoint := strings.TrimRight(hub, "/") + "/api/v1/repos/" + url.PathEscape(parts[0]) + "/" + url.PathEscape(parts[1]) + suffix |
| 103 | if len(q) > 0 { |
| 104 | endpoint += "?" + q.Encode() |
| 105 | } |
| 106 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil) |
| 107 | if err != nil { |
| 108 | return ErrCIRead |
| 109 | } |
| 110 | req.Header.Set("Authorization", "Bearer "+cred.Bearer) |
| 111 | req.Header.Set("Accept", "application/json") |
| 112 | // The bearer goes to the hub only: a redirect is refused, not followed. |
| 113 | c := *client |
| 114 | c.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse } |
| 115 | res, err := c.Do(req) |
| 116 | if err != nil { |
| 117 | return fmt.Errorf("%w: %v", ErrCIRead, err) |
| 118 | } |
| 119 | defer res.Body.Close() |
| 120 | if res.StatusCode != http.StatusOK { |
| 121 | return fmt.Errorf("%w: status %d", ErrCIRead, res.StatusCode) |
| 122 | } |
| 123 | raw, err := io.ReadAll(io.LimitReader(res.Body, limit+1)) |
| 124 | if err != nil || int64(len(raw)) > limit || json.Unmarshal(raw, out) != nil { |
| 125 | return ErrCIRead |
| 126 | } |
| 127 | return nil |
| 128 | } |
| 129 | |