Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update referrers api #20068

Merged
merged 3 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions src/server/registry/referrers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,8 @@ func (r *referrersHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}

// Get the artifact by reference
art, err := r.artifactManager.GetByDigest(ctx, repository, reference)
if err != nil {
if errors.IsNotFoundErr(err) {
// If artifact not found, return empty json
newListReferrersOK().WithPayload(nil).WriteResponse(w)
return
}
lib_http.SendError(w, err)
return
}

// Query accessories with matching subject artifact digest
query := q.New(q.KeyWords{"SubjectArtifactDigest": art.Digest, "SubjectArtifactRepo": art.RepositoryName})
query := q.New(q.KeyWords{"SubjectArtifactDigest": reference, "SubjectArtifactRepo": repository})
total, err := r.accessoryManager.Count(ctx, query)
if err != nil {
lib_http.SendError(w, err)
Expand Down
9 changes: 4 additions & 5 deletions src/server/registry/referrers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ package registry
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"testing"

beegocontext "github.com/beego/beego/v2/server/web/context"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"

"github.com/goharbor/harbor/src/lib/errors"
accessorymodel "github.com/goharbor/harbor/src/pkg/accessory/model"
basemodel "github.com/goharbor/harbor/src/pkg/accessory/model/base"
"github.com/goharbor/harbor/src/pkg/artifact"
Expand Down Expand Up @@ -93,8 +91,10 @@ func TestReferrersHandlerEmpty(t *testing.T) {
artifactMock := &arttesting.Manager{}
accessoryMock := &accessorytesting.Manager{}

artifactMock.On("GetByDigest", mock.Anything, mock.Anything, mock.Anything).
Return(nil, errors.NotFoundError(nil))
accessoryMock.On("Count", mock.Anything, mock.Anything).
Return(int64(0), nil)
accessoryMock.On("List", mock.Anything, mock.Anything).
Return([]accessorymodel.Accessory{}, nil)

handler := &referrersHandler{
artifactManager: artifactMock,
Expand All @@ -109,7 +109,6 @@ func TestReferrersHandlerEmpty(t *testing.T) {
}
index := &ocispec.Index{}
json.Unmarshal([]byte(rec.Body.String()), index)
fmt.Println(index)
if index.SchemaVersion != 0 && len(index.Manifests) != -0 {
t.Errorf("Expected empty response body, but got %s", rec.Body.String())
}
Expand Down
Loading