feat: add shared platform buildkite/githubactions BuildRunner clients#423
feat: add shared platform buildkite/githubactions BuildRunner clients#423roychying wants to merge 18 commits into
Conversation
| // Already terminal — no-op per BuildRunner.Cancel contract. | ||
| return nil | ||
| default: | ||
| return fmt.Errorf("unexpected status %d from cancel", resp.StatusCode) |
There was a problem hiding this comment.
would be nice to also include the message into the error, typically in a body of a response; can be truncated if needed.
There was a problem hiding this comment.
done, already included the body in the cancel error
| } | ||
| defer resp.Body.Close() | ||
|
|
||
| respBody, err := io.ReadAll(resp.Body) |
There was a problem hiding this comment.
this may not properly respond to context cancellation. AI analysis:
Behavior: Vulnerable to blocking/hanging.
Why: io.ReadAll reads from resp.Body until it hits io.EOF. However, once httpClient.Do() successfully receives response headers, the HTTP transport hands the raw stream body to you. Standard response bodies (net/http.bodyEOFSignal or TCP socket readers) do not automatically monitor the request ctx for subsequent reads.
If the server stalls, sends data infinitely slowly, or hangs while streaming the payload after headers have been sent, calling io.ReadAll(resp.Body) will block the goroutine until the connection times out at the network level—even if your ctx was canceled long ago.
The poor man's alternative is buffered read with cancellation checks:
https://github.com/uber/tango/blob/1ed2b86888fc4afc59494d9d97fdd6dc23c2638e/core/storage/ctxreader.go#L25
At least it cancels somewhere in between for long transfers.
There was a problem hiding this comment.
good call. dug a bit on this one. Per the stdlib docs, Request's context "controls the entire lifetime of a request and its response: obtaining a connection, sending the request, and reading the response headers and body" , and Client.Timeout (implemented as context cancellation internally) is documented to "interrupt reading of the Response.Body" . So I believe a canceled ctx does unblock an in-progress io.ReadAll(resp.Body) here, not wait for a network-level timeout.
one thing is that nothing sets a deadline on ctx or a Timeout on the http.Client for this path. platform/http.NewClient's doc already calls that out as the caller's job, but no actual service wiring exists yet to enforce it (Buildkite isn't wired into any main.go today).
I prefer to leave that to whichever change wires up a real client, but happy to follow tango's implementation since it's a cheap safe guard. lmk if anything is wrong on my understanding
| } | ||
|
|
||
| if resp.StatusCode == http.StatusNotFound { | ||
| return fmt.Errorf("build not found") |
There was a problem hiding this comment.
http helper function do() is not necessarily about build. You may want to define sentinel errors to transform http codes into.
There was a problem hiding this comment.
Done, do() now returns ErrNotFound instead of a hardcoded string.
| if !ok || raw == "" { | ||
| return meta | ||
| } | ||
| _ = json.Unmarshal([]byte(raw), &meta) |
There was a problem hiding this comment.
let's not swallow errors without proper visibility
because this is an exported function, probably better design is to expose an error and let caller handle it as they see fit
| } | ||
|
|
||
| func buildJSONWithEnv(number int, state, webURL string, env map[string]string) []byte { | ||
| b, _ := json.Marshal(BuildResponse{Number: number, State: state, WebURL: webURL, Env: env}) |
There was a problem hiding this comment.
func buildJSONWithEnv(t *testing.T, number int, state, webURL string, env map[string]string) []byte {
t.Helper()
b, err := json.Marshal(BuildResponse{Number: number, State: state, WebURL: webURL, Env: env})
require.NoError(t, err)
return b
}
| capturedMethod = req.Method | ||
| capturedBody, _ = io.ReadAll(req.Body) | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write(buildJSON(42, "scheduled", "https://buildkite.com/test-org/my-pipeline/builds/42")) |
| ) | ||
| } | ||
|
|
||
| func (c *Client) do(ctx context.Context, method, rawURL string, body []byte, out any) error { |
There was a problem hiding this comment.
duplicated code with buildkite impl, can deduce?
There was a problem hiding this comment.
sure, now both use extracted platform/http.SendRequest
| } | ||
| if params.Logger == nil { | ||
| return nil, fmt.Errorf("logger is required") | ||
| } |
There was a problem hiding this comment.
Submitqueue design assumes all wiring to be non-nil, so there is no need to explicitly check it. Panic will do just fine.
There was a problem hiding this comment.
I see, removed those unnecessary check
There was a problem hiding this comment.
Also applied the same fix to submitqueue's buildkite and githubaction's NewBuildRunners, which had identical checks.
| EnvKeyQueue: r.cfg.QueueName, | ||
| } | ||
| if len(metadata) > 0 { | ||
| metaJSON, _ := json.Marshal(metadata) |
There was a problem hiding this comment.
plz do not swallow errors
There was a problem hiding this comment.
the metadata marshal error is now propagated. Also fixed the same swallowed-marshal-error pattern in submitqueue's buildkite Trigger
|
|
||
| _, err := r.Trigger(context.Background(), "", "github://repo/head/aaa", nil) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "response missing workflow_run_id") |
There was a problem hiding this comment.
Do not assume on error messages - messages are not part of application logic and it makes test unnecessary fragile. AGENTS.md has an instruction to avoid it, surprising it was not picked up.
There was a problem hiding this comment.
dropped the message assertion, kept require.Error.
…dupe and clean up the buildkite/githubactions HTTP clients.
…wing it, logging a warning at each call site.
…succeeds instead of discarding its error.
…validating it in NewBuildRunner.
… instead of discarding it.
…ite Trigger instead of discarding them.
…f validating it in NewBuildRunner.
…ead of validating it in NewBuildRunner.
…ad of hardcoded not-found strings, matching buildkite.
Summary
platform/extension/buildrunner/and refactor the existing submitqueue adapters to use them.BuildRunneradapters on top of the shared clients.Test plan
go build ./...go test ./platform/extension/buildrunner/... ./submitqueue/extension/buildrunner/... ./stovepipe/extension/buildrunner/...make gazelle(no drift)