Add tag filter when call build list endpoint (#3173)

* Add tag filter when call build list endpoint

* Add unit test
This commit is contained in:
Michelangelo 2022-01-11 12:03:41 +01:00 committed by GitHub
parent 0aca433670
commit f889ae8eb0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View file

@ -37,6 +37,7 @@ func HandleList(
namespace = chi.URLParam(r, "owner") namespace = chi.URLParam(r, "owner")
name = chi.URLParam(r, "name") name = chi.URLParam(r, "name")
branch = r.FormValue("branch") branch = r.FormValue("branch")
tag = r.FormValue("tag")
page = r.FormValue("page") page = r.FormValue("page")
perPage = r.FormValue("per_page") perPage = r.FormValue("per_page")
) )
@ -66,6 +67,9 @@ func HandleList(
if branch != "" { if branch != "" {
ref := fmt.Sprintf("refs/heads/%s", branch) ref := fmt.Sprintf("refs/heads/%s", branch)
results, err = builds.ListRef(r.Context(), repo.ID, ref, limit, offset) results, err = builds.ListRef(r.Context(), repo.ID, ref, limit, offset)
} else if tag != "" {
ref := fmt.Sprintf("refs/tags/%s", tag)
results, err = builds.ListRef(r.Context(), repo.ID, ref, limit, offset)
} else { } else {
results, err = builds.List(r.Context(), repo.ID, limit, offset) results, err = builds.List(r.Context(), repo.ID, limit, offset)
} }

View file

@ -139,6 +139,38 @@ func TestListBranch(t *testing.T) {
} }
} }
func TestListTag(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/tags/1.33.7", 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", "/?tag=1.33.7", 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()