Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions internal/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,21 @@ func (a *API) ServeHTTP(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(r.URL.String(), "/")
kind := parts[len(parts)-1]

a.outputRequestData(kind, data)

actual, err := decodeWrapper(kind, data)
if err != nil {
a.pushError(err)
}

a.outputRequestData(kind, actual)

if kind == "create_pull_request" && actual != nil {
createPR := actual.Data.(model.CreatePullRequest)
createPR.UpdatedDependencyFiles = replaceBinaryWithHash(createPR.UpdatedDependencyFiles)
} else if kind == "update_pull_request" && actual != nil {
updatePR := actual.Data.(model.UpdatePullRequest)
updatePR.UpdatedDependencyFiles = replaceBinaryWithHash(updatePR.UpdatedDependencyFiles)
}

if actual == nil {
// indicates the kind (endpoint) isn't implemented in decodeWrapper, so return a 501
w.WriteHeader(http.StatusNotImplemented)
Expand Down Expand Up @@ -179,12 +187,12 @@ func (a *API) assertExpectation(kind string, actual *model.UpdateWrapper) {
}
}

func (a *API) outputRequestData(kind string, data []byte) {
func (a *API) outputRequestData(kind string, data *model.UpdateWrapper) {
if a.writer != nil {
// output the data received to stdout
if err := json.NewEncoder(a.writer).Encode(map[string]any{
"type": kind,
"data": string(data),
"data": data.Data,
}); err != nil {
// Fail so the user knows stdout is not working
log.Panicln("Failed to write to stdout: ", err)
Expand Down Expand Up @@ -221,15 +229,9 @@ func decodeWrapper(kind string, data []byte) (actual *model.UpdateWrapper, err e
case "update_dependency_list":
actual.Data, err = decode[model.UpdateDependencyList](data)
case "create_pull_request":
var createPR model.CreatePullRequest
createPR, err = decode[model.CreatePullRequest](data)
createPR.UpdatedDependencyFiles = replaceBinaryWithHash(createPR.UpdatedDependencyFiles)
actual.Data = createPR
actual.Data, err = decode[model.CreatePullRequest](data)
case "update_pull_request":
var updatePR model.UpdatePullRequest
updatePR, err = decode[model.UpdatePullRequest](data)
updatePR.UpdatedDependencyFiles = replaceBinaryWithHash(updatePR.UpdatedDependencyFiles)
actual.Data = updatePR
actual.Data, err = decode[model.UpdatePullRequest](data)
case "close_pull_request":
actual.Data, err = decode[model.ClosePullRequest](data)
case "mark_as_processed":
Expand Down
11 changes: 9 additions & 2 deletions internal/server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/dependabot/cli/internal/model"
"net/http"
"net/http/httptest"
"strings"
"testing"
)

Expand All @@ -37,6 +36,10 @@ func TestAPI_ServeHTTP(t *testing.T) {
})
}

type Wrapper[T any] struct {
Data T `json:"data"`
}

func TestAPI_CreatePullRequest_ReplacesBinaryWithHash(t *testing.T) {
var stdout bytes.Buffer

Expand Down Expand Up @@ -92,7 +95,11 @@ func TestAPI_CreatePullRequest_ReplacesBinaryWithHash(t *testing.T) {
}

// stdout should contain the original content so folks can create PRs
if !strings.Contains(stdout.String(), content) {
var wrapper Wrapper[model.CreatePullRequest]
if err := json.NewDecoder(&stdout).Decode(&wrapper); err != nil {
t.Fatalf("failed to decode stdout: %v", err)
}
if wrapper.Data.UpdatedDependencyFiles[0].Content != content {
t.Errorf("expected stdout to contain the original content, got '%s'", stdout.String())
}
}