From 4ccc3da99b107085ce30fce56708e1253ba01a00 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Thu, 5 Mar 2020 14:26:53 +0800 Subject: [PATCH] Remove the "x-go-type" for artifact definition in swagger Using "x-go-type" may cause the inconsistence between the swagger definition and the real data model Signed-off-by: Wenkai Yin --- api/v2.0/swagger.yaml | 14 +++---- src/api/artifact/model.go | 9 +++++ src/api/tag/model.go | 16 ++++++++ src/common/models/label.go | 16 ++++++++ src/pkg/artifact/model.go | 22 ++++++++++ .../artifact-list-tab.component.spec.ts | 12 ++---- src/server/v2.0/handler/artifact.go | 15 +++---- src/server/v2.0/handler/model/artifact.go | 40 ++++++++++++++++++- 8 files changed, 121 insertions(+), 23 deletions(-) diff --git a/api/v2.0/swagger.yaml b/api/v2.0/swagger.yaml index 7ec83d786..7b2c59884 100644 --- a/api/v2.0/swagger.yaml +++ b/api/v2.0/swagger.yaml @@ -763,10 +763,6 @@ definitions: description: The update time of the repository Artifact: type: object - x-go-type: - import: - package: "github.com/goharbor/harbor/src/server/v2.0/handler/model" - type: "Artifact" properties: id: type: integer @@ -897,6 +893,13 @@ definitions: description: The digest of the child artifact platform: $ref: '#/definitions/Platform' + annotations: + $ref: '#/definitions/Annotations' + urls: + type: array + description: The download URLs + items: + type: string Platform: type: object properties: @@ -948,9 +951,6 @@ definitions: type: string format: date-time description: The update time of the label - deleted: - type: boolean - description: Whether the label is deleted or not ScanOverview: type: object description: 'The scan overview attached in the metadata of tag' diff --git a/src/api/artifact/model.go b/src/api/artifact/model.go index c91113dd3..2f6d9b5b8 100644 --- a/src/api/artifact/model.go +++ b/src/api/artifact/model.go @@ -16,6 +16,7 @@ package artifact import ( "fmt" + "github.com/goharbor/harbor/src/server/v2.0/models" "github.com/goharbor/harbor/src/api/tag" cmodels "github.com/goharbor/harbor/src/common/models" @@ -49,6 +50,14 @@ type AdditionLink struct { Absolute bool `json:"absolute"` // specify the href is an absolute URL or not } +// ToSwagger converts the addition link to the swagger model +func (a *AdditionLink) ToSwagger() models.AdditionLink { + return models.AdditionLink{ + Absolute: a.Absolute, + Href: a.HREF, + } +} + // Option is used to specify the properties returned when listing/getting artifacts type Option struct { WithTag bool diff --git a/src/api/tag/model.go b/src/api/tag/model.go index cc541f369..646354e72 100644 --- a/src/api/tag/model.go +++ b/src/api/tag/model.go @@ -1,8 +1,10 @@ package tag import ( + "github.com/go-openapi/strfmt" "github.com/goharbor/harbor/src/pkg/signature" "github.com/goharbor/harbor/src/pkg/tag/model/tag" + "github.com/goharbor/harbor/src/server/v2.0/models" ) // Tag is the overall view of tag @@ -12,6 +14,20 @@ type Tag struct { Signed bool `json:"signed"` } +// ToSwagger converts the tag to the swagger model +func (t *Tag) ToSwagger() *models.Tag { + return &models.Tag{ + ArtifactID: t.ArtifactID, + ID: t.ID, + Name: t.Name, + PullTime: strfmt.DateTime(t.PullTime), + PushTime: strfmt.DateTime(t.PushTime), + RepositoryID: t.RepositoryID, + Immutable: t.Immutable, + Signed: t.Signed, + } +} + // Option is used to specify the properties returned when listing/getting tags type Option struct { WithImmutableStatus bool diff --git a/src/common/models/label.go b/src/common/models/label.go index 5be0c4eb5..4923c9065 100644 --- a/src/common/models/label.go +++ b/src/common/models/label.go @@ -16,6 +16,8 @@ package models import ( "fmt" + "github.com/go-openapi/strfmt" + "github.com/goharbor/harbor/src/server/v2.0/models" "time" "github.com/astaxie/beego/validation" @@ -36,6 +38,20 @@ type Label struct { Deleted bool `orm:"column(deleted)" json:"deleted"` } +// ToSwagger converts the label to the swagger model +func (l *Label) ToSwagger() *models.Label { + return &models.Label{ + Color: l.Color, + CreationTime: strfmt.DateTime(l.CreationTime), + Description: l.Description, + ID: l.ID, + Name: l.Name, + ProjectID: l.ProjectID, + Scope: l.Scope, + UpdateTime: strfmt.DateTime(l.UpdateTime), + } +} + // TableName ... func (l *Label) TableName() string { return "harbor_label" diff --git a/src/pkg/artifact/model.go b/src/pkg/artifact/model.go index fba655318..d26fd8e80 100644 --- a/src/pkg/artifact/model.go +++ b/src/pkg/artifact/model.go @@ -16,6 +16,7 @@ package artifact import ( "encoding/json" + "github.com/goharbor/harbor/src/server/v2.0/models" "time" "github.com/docker/distribution/manifest/manifestlist" @@ -177,3 +178,24 @@ func (r *Reference) To() *dao.ArtifactReference { } return ref } + +// ToSwagger converts the reference to the swagger model +func (r *Reference) ToSwagger() *models.Reference { + ref := &models.Reference{ + ChildDigest: r.ChildDigest, + ChildID: r.ChildID, + ParentID: r.ParentID, + Annotations: r.Annotations, + Urls: r.URLs, + } + if r.Platform != nil { + ref.Platform = &models.Platform{ + Architecture: r.Platform.Architecture, + Os: r.Platform.OS, + OsFeatures: r.Platform.OSFeatures, + OsVersion: r.Platform.OSVersion, + Variant: r.Platform.Variant, + } + } + return ref +} diff --git a/src/portal/src/app/project/repository/artifact-list-page/artifact-list/artifact-list-tab/artifact-list-tab.component.spec.ts b/src/portal/src/app/project/repository/artifact-list-page/artifact-list/artifact-list-tab/artifact-list-tab.component.spec.ts index 86cc4f5d1..c0c805bea 100644 --- a/src/portal/src/app/project/repository/artifact-list-page/artifact-list/artifact-list-tab/artifact-list-tab.component.spec.ts +++ b/src/portal/src/app/project/repository/artifact-list-page/artifact-list/artifact-list-tab/artifact-list-tab.component.spec.ts @@ -118,8 +118,7 @@ describe("ArtifactListTabComponent (inline template)", () => { "scope": "g", "project_id": 0, "creation_time": "2020-01-13T05:44:00.580198Z", - "update_time": "2020-01-13T05:44:00.580198Z", - "deleted": false + "update_time": "2020-01-13T05:44:00.580198Z" }, { "id": 6, @@ -129,8 +128,7 @@ describe("ArtifactListTabComponent (inline template)", () => { "scope": "g", "project_id": 0, "creation_time": "2020-01-13T08:27:19.279123Z", - "update_time": "2020-01-13T08:27:19.279123Z", - "deleted": false + "update_time": "2020-01-13T08:27:19.279123Z" } ], "push_time": "2020-01-07T03:33:41.162319Z", @@ -176,8 +174,7 @@ describe("ArtifactListTabComponent (inline template)", () => { "scope": "g", "project_id": 0, "creation_time": "2020-01-13T05:44:00.580198Z", - "update_time": "2020-01-13T05:44:00.580198Z", - "deleted": false + "update_time": "2020-01-13T05:44:00.580198Z" }, { "id": 6, @@ -187,8 +184,7 @@ describe("ArtifactListTabComponent (inline template)", () => { "scope": "g", "project_id": 0, "creation_time": "2020-01-13T08:27:19.279123Z", - "update_time": "2020-01-13T08:27:19.279123Z", - "deleted": false + "update_time": "2020-01-13T08:27:19.279123Z" } ], "push_time": "2020-01-07T03:33:41.162319Z", diff --git a/src/server/v2.0/handler/artifact.go b/src/server/v2.0/handler/artifact.go index f4e1f26c0..0e01b63de 100644 --- a/src/server/v2.0/handler/artifact.go +++ b/src/server/v2.0/handler/artifact.go @@ -17,8 +17,6 @@ package handler import ( "context" "fmt" - "github.com/goharbor/harbor/src/api/event" - evt "github.com/goharbor/harbor/src/pkg/notifier/event" "net/http" "strings" "time" @@ -28,15 +26,18 @@ import ( "github.com/go-openapi/runtime/middleware" "github.com/goharbor/harbor/src/api/artifact" "github.com/goharbor/harbor/src/api/artifact/abstractor/resolver" + "github.com/goharbor/harbor/src/api/event" "github.com/goharbor/harbor/src/api/repository" "github.com/goharbor/harbor/src/api/scan" "github.com/goharbor/harbor/src/api/tag" "github.com/goharbor/harbor/src/common/rbac" "github.com/goharbor/harbor/src/common/utils" ierror "github.com/goharbor/harbor/src/internal/error" + evt "github.com/goharbor/harbor/src/pkg/notifier/event" v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1" "github.com/goharbor/harbor/src/server/v2.0/handler/assembler" "github.com/goharbor/harbor/src/server/v2.0/handler/model" + "github.com/goharbor/harbor/src/server/v2.0/models" operation "github.com/goharbor/harbor/src/server/v2.0/restapi/operations/artifact" "github.com/opencontainers/go-digest" ) @@ -97,15 +98,15 @@ func (a *artifactAPI) ListArtifacts(ctx context.Context, params operation.ListAr return a.SendError(ctx, err) } - var artifacts []*model.Artifact + assembler := assembler.NewVulAssembler(boolValue(params.WithScanOverview)) + var artifacts []*models.Artifact for _, art := range arts { artifact := &model.Artifact{} artifact.Artifact = *art - artifacts = append(artifacts, artifact) + assembler.WithArtifacts(artifact).Assemble(ctx) + artifacts = append(artifacts, artifact.ToSwagger()) } - assembler.NewVulAssembler(boolValue(params.WithScanOverview)).WithArtifacts(artifacts...).Assemble(ctx) - return operation.NewListArtifactsOK(). WithXTotalCount(total). WithLink(a.Links(ctx, params.HTTPRequest.URL, total, query.PageNumber, query.PageSize).String()). @@ -130,7 +131,7 @@ func (a *artifactAPI) GetArtifact(ctx context.Context, params operation.GetArtif assembler.NewVulAssembler(boolValue(params.WithScanOverview)).WithArtifacts(art).Assemble(ctx) - return operation.NewGetArtifactOK().WithPayload(art) + return operation.NewGetArtifactOK().WithPayload(art.ToSwagger()) } func (a *artifactAPI) DeleteArtifact(ctx context.Context, params operation.DeleteArtifactParams) middleware.Responder { diff --git a/src/server/v2.0/handler/model/artifact.go b/src/server/v2.0/handler/model/artifact.go index a04ac0258..136fbf7b5 100644 --- a/src/server/v2.0/handler/model/artifact.go +++ b/src/server/v2.0/handler/model/artifact.go @@ -14,10 +14,48 @@ package model -import "github.com/goharbor/harbor/src/api/artifact" +import ( + "github.com/go-openapi/strfmt" + "github.com/goharbor/harbor/src/api/artifact" + "github.com/goharbor/harbor/src/server/v2.0/models" +) // Artifact model type Artifact struct { artifact.Artifact ScanOverview map[string]interface{} `json:"scan_overview"` } + +// ToSwagger converts the artifact to the swagger model +func (a *Artifact) ToSwagger() *models.Artifact { + art := &models.Artifact{ + ID: a.ID, + Type: a.Type, + MediaType: a.MediaType, + ManifestMediaType: a.ManifestMediaType, + ProjectID: a.ProjectID, + RepositoryID: a.RepositoryID, + Digest: a.Digest, + Size: a.Size, + PullTime: strfmt.DateTime(a.PullTime), + PushTime: strfmt.DateTime(a.PushTime), + ExtraAttrs: a.ExtraAttrs, + Annotations: a.Annotations, + } + for _, reference := range a.References { + art.References = append(art.References, reference.ToSwagger()) + } + for _, tag := range a.Tags { + art.Tags = append(art.Tags, tag.ToSwagger()) + } + for addition, link := range a.AdditionLinks { + if art.AdditionLinks == nil { + art.AdditionLinks = make(map[string]models.AdditionLink) + } + art.AdditionLinks[addition] = link.ToSwagger() + } + for _, label := range a.Labels { + art.Labels = append(art.Labels, label.ToSwagger()) + } + return art +}