From d9b0f54c5ead1cb80e0867629b0472428cf98a83 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Fri, 15 Dec 2017 10:40:24 +0800 Subject: [PATCH] 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) +}