Skip to content

Commit e0439ab

Browse files
committed
removed API token, handling ReadAll error
1 parent 127cb56 commit e0439ab

3 files changed

Lines changed: 29 additions & 24 deletions

File tree

submitqueue/extension/changeprovider/phabricator/conduit.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@ type fileChange struct {
3333
}
3434

3535
// buildQueryDiffsRequest builds query parameters for a differential.querydiffs call.
36-
func buildQueryDiffsRequest(diffIDs []int, apiToken string) url.Values {
36+
func buildQueryDiffsRequest(diffIDs []int) url.Values {
3737
form := url.Values{}
3838
for _, id := range diffIDs {
3939
form.Add("ids[]", strconv.Itoa(id))
4040
}
41-
if apiToken != "" {
42-
form.Set("api.token", apiToken)
43-
}
4441
return form
4542
}
4643

@@ -66,7 +63,10 @@ func doConduitRequest(ctx context.Context, httpClient *http.Client, form url.Val
6663
// and extracts the diffResults for the requested diff IDs.
6764
func parseConduitResponse(resp *http.Response, diffIDs []int) (map[int]*diffResult, error) {
6865
if resp.StatusCode != http.StatusOK {
69-
body, _ := io.ReadAll(resp.Body)
66+
body, err := io.ReadAll(resp.Body)
67+
if err != nil {
68+
return nil, fmt.Errorf("Conduit API returned status %d and failed to read body: %w", resp.StatusCode, err)
69+
}
7070
return nil, fmt.Errorf("Conduit API returned status %d: %s", resp.StatusCode, string(body))
7171
}
7272

submitqueue/extension/changeprovider/phabricator/conduit_test.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package phabricator
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io"
67
"net/http"
78
"net/http/httptest"
@@ -14,10 +15,9 @@ import (
1415

1516
func TestBuildQueryDiffsRequest(t *testing.T) {
1617
testCases := []struct {
17-
name string
18-
diffIDs []int
19-
apiToken string
20-
wantAll []string
18+
name string
19+
diffIDs []int
20+
wantAll []string
2121
}{
2222
{
2323
name: "single ID",
@@ -29,17 +29,11 @@ func TestBuildQueryDiffsRequest(t *testing.T) {
2929
diffIDs: []int{42, 43, 44},
3030
wantAll: []string{"ids%5B%5D=42", "ids%5B%5D=43", "ids%5B%5D=44"},
3131
},
32-
{
33-
name: "includes api.token when set",
34-
diffIDs: []int{42},
35-
apiToken: "my-secret",
36-
wantAll: []string{"ids%5B%5D=42", "api.token=my-secret"},
37-
},
3832
}
3933

4034
for _, tc := range testCases {
4135
t.Run(tc.name, func(t *testing.T) {
42-
form := buildQueryDiffsRequest(tc.diffIDs, tc.apiToken)
36+
form := buildQueryDiffsRequest(tc.diffIDs)
4337
encoded := form.Encode()
4438
for _, want := range tc.wantAll {
4539
assert.Contains(t, encoded, want)
@@ -60,7 +54,7 @@ func TestDoConduitRequest(t *testing.T) {
6054
defer server.Close()
6155

6256
client := &http.Client{Transport: &testTransport{baseURL: server.URL}}
63-
form := buildQueryDiffsRequest([]int{100, 200}, "")
57+
form := buildQueryDiffsRequest([]int{100, 200})
6458

6559
resp, err := doConduitRequest(t.Context(), client, form)
6660
require.NoError(t, err)
@@ -73,7 +67,7 @@ func TestDoConduitRequest(t *testing.T) {
7367

7468
func TestDoConduitRequest_ConnectionError(t *testing.T) {
7569
client := &http.Client{Transport: &testTransport{baseURL: "http://127.0.0.1:0"}}
76-
form := buildQueryDiffsRequest([]int{1}, "")
70+
form := buildQueryDiffsRequest([]int{1})
7771

7872
_, err := doConduitRequest(t.Context(), client, form)
7973

@@ -104,6 +98,15 @@ func TestParseConduitResponse(t *testing.T) {
10498
diffIDs: []int{100},
10599
wantErr: "Conduit API returned status 500",
106100
},
101+
{
102+
name: "HTTP error with unreadable body",
103+
resp: &http.Response{
104+
StatusCode: http.StatusBadGateway,
105+
Body: io.NopCloser(&errReader{}),
106+
},
107+
diffIDs: []int{100},
108+
wantErr: "failed to read body",
109+
},
107110
{
108111
name: "conduit error",
109112
resp: newConduitErrorHTTPResponse(t, "ERR-CONDUIT-CORE", "Invalid diff ID"),
@@ -200,3 +203,9 @@ func (t *testTransport) RoundTrip(req *http.Request) (*http.Response, error) {
200203
req.URL.Host = t.baseURL[len("http://"):]
201204
return http.DefaultTransport.RoundTrip(req)
202205
}
206+
207+
type errReader struct{}
208+
209+
func (e *errReader) Read([]byte) (int, error) {
210+
return 0, fmt.Errorf("simulated read error")
211+
}

submitqueue/extension/changeprovider/phabricator/provider.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@ const queryDiffsBatchSize = 10
2222
type Params struct {
2323
// HTTPClient is a pre-configured HTTP client. The caller is responsible for
2424
// configuring the base URL (e.g. via httpclient.NewClient) and authentication
25-
// (e.g. via oauth2.Transport for Bearer tokens) via transport layers.
25+
// (e.g. via a RoundTripper that injects credentials) via transport layers.
2626
HTTPClient *http.Client
27-
// APIToken is the Conduit API token, sent as the api.token form parameter.
28-
APIToken string
2927
// Logger is the structured logger.
3028
Logger *zap.SugaredLogger
3129
// MetricsScope is the metrics scope for instrumentation.
@@ -34,7 +32,6 @@ type Params struct {
3432

3533
type provider struct {
3634
httpClient *http.Client
37-
apiToken string
3835
logger *zap.SugaredLogger
3936
metricsScope tally.Scope
4037
}
@@ -43,7 +40,6 @@ type provider struct {
4340
func NewProvider(params Params) changeprovider.ChangeProvider {
4441
return &provider{
4542
httpClient: params.HTTPClient,
46-
apiToken: params.APIToken,
4743
logger: params.Logger.Named("phabricator_changeprovider"),
4844
metricsScope: params.MetricsScope.SubScope("phabricator_changeprovider"),
4945
}
@@ -134,7 +130,7 @@ func (p *provider) fetchAllDiffs(ctx context.Context, changeIDs []entityphab.Cha
134130

135131
// fetchDiffBatch fetches a single batch of diffs from Phabricator.
136132
func (p *provider) fetchDiffBatch(ctx context.Context, diffIDs []int) (map[int]*diffResult, error) {
137-
form := buildQueryDiffsRequest(diffIDs, p.apiToken)
133+
form := buildQueryDiffsRequest(diffIDs)
138134

139135
resp, err := doConduitRequest(ctx, p.httpClient, form)
140136
if err != nil {

0 commit comments

Comments
 (0)