mirror of
https://github.com/harness/drone.git
synced 2025-05-17 09:30:00 +08:00
query builds based on branch, closes #1495
This commit is contained in:
parent
b950e28dd3
commit
12eeb7f15e
@ -15,6 +15,7 @@
|
|||||||
package builds
|
package builds
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@ -35,6 +36,7 @@ func HandleList(
|
|||||||
var (
|
var (
|
||||||
namespace = chi.URLParam(r, "owner")
|
namespace = chi.URLParam(r, "owner")
|
||||||
name = chi.URLParam(r, "name")
|
name = chi.URLParam(r, "name")
|
||||||
|
branch = r.FormValue("branch")
|
||||||
page = r.FormValue("page")
|
page = r.FormValue("page")
|
||||||
perPage = r.FormValue("per_page")
|
perPage = r.FormValue("per_page")
|
||||||
)
|
)
|
||||||
@ -59,7 +61,15 @@ func HandleList(
|
|||||||
Debugln("api: cannot find repository")
|
Debugln("api: cannot find repository")
|
||||||
return
|
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 {
|
if err != nil {
|
||||||
render.InternalError(w, err)
|
render.InternalError(w, err)
|
||||||
logger.FromRequest(r).
|
logger.FromRequest(r).
|
||||||
@ -68,7 +78,7 @@ func HandleList(
|
|||||||
WithField("name", name).
|
WithField("name", name).
|
||||||
Debugln("api: cannot list builds")
|
Debugln("api: cannot list builds")
|
||||||
} else {
|
} else {
|
||||||
render.JSON(w, builds, 200)
|
render.JSON(w, results, 200)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ import (
|
|||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/drone/drone/core"
|
||||||
"github.com/drone/drone/handler/api/errors"
|
"github.com/drone/drone/handler/api/errors"
|
||||||
"github.com/drone/drone/mock"
|
"github.com/drone/drone/mock"
|
||||||
"github.com/drone/drone/core"
|
|
||||||
|
|
||||||
"github.com/go-chi/chi"
|
"github.com/go-chi/chi"
|
||||||
"github.com/golang/mock/gomock"
|
"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) {
|
func TestList_RepositoryNotFound(t *testing.T) {
|
||||||
controller := gomock.NewController(t)
|
controller := gomock.NewController(t)
|
||||||
defer controller.Finish()
|
defer controller.Finish()
|
||||||
|
Loading…
Reference in New Issue
Block a user