Skip to content

Commit

Permalink
query builds based on branch, closes #1495
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Sep 19, 2019
1 parent b950e28 commit 12eeb7f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
14 changes: 12 additions & 2 deletions handler/api/repos/builds/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package builds

import (
"fmt"
"net/http"
"strconv"

Expand All @@ -35,6 +36,7 @@ func HandleList(
var (
namespace = chi.URLParam(r, "owner")
name = chi.URLParam(r, "name")
branch = r.FormValue("branch")
page = r.FormValue("page")
perPage = r.FormValue("per_page")
)
Expand All @@ -59,7 +61,15 @@ func HandleList(
Debugln("api: cannot find repository")
return
}
builds, err := builds.List(r.Context(), repo.ID, limit, offset)

var results []*core.Build
if branch != "" {
ref := fmt.Sprintf("refs/heads/%s", branch)
results, err = builds.ListRef(r.Context(), repo.ID, ref, limit, offset)
} else {
results, err = builds.List(r.Context(), repo.ID, limit, offset)
}

if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).
Expand All @@ -68,7 +78,7 @@ func HandleList(
WithField("name", name).
Debugln("api: cannot list builds")
} else {
render.JSON(w, builds, 200)
render.JSON(w, results, 200)
}
}
}
34 changes: 33 additions & 1 deletion handler/api/repos/builds/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"net/http/httptest"
"testing"

"github.com/drone/drone/core"
"github.com/drone/drone/handler/api/errors"
"github.com/drone/drone/mock"
"github.com/drone/drone/core"

"github.com/go-chi/chi"
"github.com/golang/mock/gomock"
Expand Down Expand Up @@ -107,6 +107,38 @@ func TestList(t *testing.T) {
}
}

func TestListBranch(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()

repos := mock.NewMockRepositoryStore(controller)
repos.EXPECT().FindName(gomock.Any(), gomock.Any(), mockRepo.Name).Return(mockRepo, nil)

builds := mock.NewMockBuildStore(controller)
builds.EXPECT().ListRef(gomock.Any(), mockRepo.ID, "refs/heads/develop", 25, 0).Return(mockBuilds, nil)

c := new(chi.Context)
c.URLParams.Add("owner", "octocat")
c.URLParams.Add("name", "hello-world")

w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/?branch=develop", nil)
r = r.WithContext(
context.WithValue(context.Background(), chi.RouteCtxKey, c),
)

HandleList(repos, builds)(w, r)
if got, want := w.Code, 200; want != got {
t.Errorf("Want response code %d, got %d", want, got)
}

got, want := []*core.Build{}, mockBuilds
json.NewDecoder(w.Body).Decode(&got)
if diff := cmp.Diff(got, want); len(diff) != 0 {
t.Errorf(diff)
}
}

func TestList_RepositoryNotFound(t *testing.T) {
controller := gomock.NewController(t)
defer controller.Finish()
Expand Down

0 comments on commit 12eeb7f

Please sign in to comment.