Skip to content

Commit

Permalink
Add tag filter when call build list endpoint (harness#3173)
Browse files Browse the repository at this point in the history
* Add tag filter when call build list endpoint

* Add unit test
  • Loading branch information
michelangelomo authored and ivanvc committed Dec 14, 2023
1 parent 2fe39cf commit 7e3833d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions handler/api/repos/builds/list.go
Expand Up @@ -37,6 +37,7 @@ func HandleList(
namespace = chi.URLParam(r, "owner")
name = chi.URLParam(r, "name")
branch = r.FormValue("branch")
tag = r.FormValue("tag")
page = r.FormValue("page")
perPage = r.FormValue("per_page")
)
Expand Down Expand Up @@ -66,6 +67,9 @@ func HandleList(
if branch != "" {
ref := fmt.Sprintf("refs/heads/%s", branch)
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 {
results, err = builds.List(r.Context(), repo.ID, limit, offset)
}
Expand Down
32 changes: 32 additions & 0 deletions handler/api/repos/builds/list_test.go
Expand Up @@ -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) {
controller := gomock.NewController(t)
defer controller.Finish()
Expand Down

0 comments on commit 7e3833d

Please sign in to comment.