From 745d83e393028517c28961037feddedf33ccc39a Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Wed, 13 Dec 2017 23:28:01 +0800 Subject: [PATCH 1/2] Read image author from label 'maintainer' if author is null --- src/ui/api/repository.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/ui/api/repository.go b/src/ui/api/repository.go index 7e1d0277b..f59f464d1 100644 --- a/src/ui/api/repository.go +++ b/src/ui/api/repository.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "net/http" "strconv" + "strings" "time" "github.com/docker/distribution/manifest/schema1" @@ -63,6 +64,11 @@ type tagDetail struct { DockerVersion string `json:"docker_version"` Author string `json:"author"` Created time.Time `json:"created"` + Config *cfg `json:"config"` +} + +type cfg struct { + Labels map[string]string `json:"labels"` } type tagResp struct { @@ -460,6 +466,14 @@ func getTagDetail(client *registry.Repository, tag string) (*tagDetail, error) { return detail, err } + if len(detail.Author) == 0 && detail.Config != nil { + for k, v := range detail.Config.Labels { + if strings.ToLower(k) == "maintainer" { + detail.Author = v + } + } + } + return detail, nil } From d9b0f54c5ead1cb80e0867629b0472428cf98a83 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Fri, 15 Dec 2017 10:40:24 +0800 Subject: [PATCH 2/2] Split populating author as a method and add unit test --- src/ui/api/repository.go | 17 ++++++++++++++--- src/ui/api/repository_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/ui/api/repository.go b/src/ui/api/repository.go index f59f464d1..3bf54bf66 100644 --- a/src/ui/api/repository.go +++ b/src/ui/api/repository.go @@ -466,15 +466,26 @@ func getTagDetail(client *registry.Repository, tag string) (*tagDetail, error) { return detail, err } - if len(detail.Author) == 0 && detail.Config != nil { + populateAuthor(detail) + + return detail, nil +} + +func populateAuthor(detail *tagDetail) { + // has author info already + if len(detail.Author) > 0 { + return + } + + // try to set author with the value of label "maintainer" + if detail.Config != nil { for k, v := range detail.Config.Labels { if strings.ToLower(k) == "maintainer" { detail.Author = v + return } } } - - return detail, nil } // GetManifests returns the manifest of a tag diff --git a/src/ui/api/repository_test.go b/src/ui/api/repository_test.go index 42f96ec55..62ebe61bc 100644 --- a/src/ui/api/repository_test.go +++ b/src/ui/api/repository_test.go @@ -199,3 +199,27 @@ func TestGetReposTop(t *testing.T) { fmt.Printf("\n") } + +func TestPopulateAuthor(t *testing.T) { + author := "author" + detail := &tagDetail{ + Author: author, + } + populateAuthor(detail) + assert.Equal(t, author, detail.Author) + + detail = &tagDetail{} + populateAuthor(detail) + assert.Equal(t, "", detail.Author) + + maintainer := "maintainer" + detail = &tagDetail{ + Config: &cfg{ + Labels: map[string]string{ + "Maintainer": maintainer, + }, + }, + } + populateAuthor(detail) + assert.Equal(t, maintainer, detail.Author) +}