query builds based on branch, closes #1495
This commit is contained in:
parent
b950e28dd3
commit
12eeb7f15e
2 changed files with 45 additions and 3 deletions
|
@ -15,6 +15,7 @@
|
|||
package builds
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
|
@ -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")
|
||||
)
|
||||
|
@ -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).
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
@ -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()
|
||||
|
|
Loading…
Reference in a new issue