Skip to content

Commit

Permalink
change priority type from id to name (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvatanabe committed Jul 29, 2018
1 parent 42ba773 commit 2258105
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 29 deletions.
9 changes: 9 additions & 0 deletions bbir/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type BacklogAPIClient interface {
GetCategories(ctx context.Context, id ProjectID) ([]*v2.Category, error)
GetVersions(ctx context.Context, id ProjectID) ([]*v2.Version, error)
GetCustomFields(ctx context.Context, id ProjectID) ([]*v2.CustomField, error)
GetPriorities(ctx context.Context) ([]*v2.Priority, error)
}

func NewBacklogAPIClient(cfg *Config) BacklogAPIClient {
Expand Down Expand Up @@ -91,3 +92,11 @@ func (c *client) GetCustomFields(ctx context.Context, id ProjectID) ([]*v2.Custo
}
return v, nil
}

func (c *client) GetPriorities(ctx context.Context) ([]*v2.Priority, error) {
v, _, err := c.Projects.GetPriorities(ctx)
if err != nil {
return nil, err
}
return v, nil
}
5 changes: 5 additions & 0 deletions bbir/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type BacklogAPIClientAsMock struct {
getCategories func(ctx context.Context, id ProjectID) ([]*Category, error)
getVersions func(ctx context.Context, id ProjectID) ([]*Version, error)
getCustomFields func(ctx context.Context, id ProjectID) ([]*CustomField, error)
getPriorities func(ctx context.Context) ([]*Priority, error)
}

func (m *BacklogAPIClientAsMock) AddIssue(ctx context.Context, projectID ProjectID, summary string, issueTypeID IssueTypeID, priorityID PriorityID, opt *AddIssueOptions) (*Issue, error) {
Expand Down Expand Up @@ -48,3 +49,7 @@ func (m *BacklogAPIClientAsMock) GetVersions(ctx context.Context, id ProjectID)
func (m *BacklogAPIClientAsMock) GetCustomFields(ctx context.Context, id ProjectID) ([]*CustomField, error) {
return m.getCustomFields(ctx, id)
}

func (m *BacklogAPIClientAsMock) GetPriorities(ctx context.Context) ([]*Priority, error) {
return m.getPriorities(ctx)
}
11 changes: 5 additions & 6 deletions bbir/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,11 @@ const DefaultPriorityID = 3
func (b *commandBuilder) ensurePriorityID(cmd *Command, line *Line) error {
priorityID := DefaultPriorityID
if line.Priority != "" {
v, _ := strconv.Atoi(line.Priority)
priorityID = v
}
// TODO Get PriorityID via project repository
if !(2 <= priorityID && priorityID <= 4) {
return errors.New(b.msgs.PriorityIsInvalid(line.Priority))
if v := b.project.FindPriorityByName(line.Priority); v != nil {
priorityID = v.ID
} else {
return errors.New(b.msgs.PriorityIsInvalid(line.Priority))
}
}
cmd.PriorityID = PriorityID(priorityID)
return nil
Expand Down
12 changes: 9 additions & 3 deletions bbir/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ func NewInjectorForCommandBuilderTest(t *testing.T) shot.Injector {
}}
return []*CustomField{text, sentence, number, date, singleList, multipleList, checkbox, radio}, nil
},
getPriorities: func(ctx context.Context) ([]*Priority, error) {
priority1 := &Priority{ID: 2, Name: "High"}
priority2 := &Priority{ID: 3, Name: "Normal"}
priority3 := &Priority{ID: 4, Name: "Low"}
return []*Priority{priority1, priority2, priority3}, nil
},
getIssue: func(ctx context.Context, issueKey string) (*Issue, error) {
switch issueKey {
case "EXAMPLE-1":
Expand Down Expand Up @@ -131,7 +137,7 @@ func Test_CommandBuilder(t *testing.T) {
Category: "web",
Version: "sprint1",
Milestone: "sprint1",
Priority: "2",
Priority: "Normal",
Assignee: "ken",
ParentIssue: "*",
}); err != nil {
Expand Down Expand Up @@ -469,7 +475,7 @@ func Test_CommandBuilder_resolveAssigneeID_should_return_error(t *testing.T) {
func Test_CommandBuilder_resolvePriorityID(t *testing.T) {
injector := NewInjectorForCommandBuilderTest(t)
builder := injector.Get(new(CommandBuilder)).(*commandBuilder)
tests := []string{"", "2", "3", "4"}
tests := []string{"", "High", "Normal", "Low"}
for _, v := range tests {
command := &Command{}
line := &Line{
Expand All @@ -484,7 +490,7 @@ func Test_CommandBuilder_resolvePriorityID(t *testing.T) {
func Test_CommandBuilder_resolvePriorityID_should_return_error(t *testing.T) {
injector := NewInjectorForCommandBuilderTest(t)
builder := injector.Get(new(CommandBuilder)).(*commandBuilder)
tests := []string{"1", "5"}
tests := []string{"xxx", "yyy"}
for _, v := range tests {
command := &Command{}
line := &Line{
Expand Down
2 changes: 1 addition & 1 deletion bbir/line_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Test_Line_NewLine(t *testing.T) {
"web",
"sprint1",
"sprint1",
"2",
"Normal",
"ken",
"*",
"1",
Expand Down
10 changes: 5 additions & 5 deletions bbir/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Messages interface {
IssueTypeIsRequired() string
IssueTypeIsNotRegistered(name string) string
PriorityIsRequired() string
PriorityIsInvalid(id string) string
PriorityIsInvalid(name string) string
StartDateIsInvalid(date string) string
DueDateIsInvalid(date string) string
StartDateIsAfterDueDate(start, due string) string
Expand Down Expand Up @@ -65,8 +65,8 @@ func (m *Japanese) PriorityIsRequired() string {
return "課題の優先度は必須です"
}

func (m *Japanese) PriorityIsInvalid(id string) string {
return fmt.Sprintf("課題の優先度 (%v) は不正です", id)
func (m *Japanese) PriorityIsInvalid(name string) string {
return fmt.Sprintf("課題の優先度 (%v) は不正です", name)
}

func (m *Japanese) StartDateIsInvalid(date string) string {
Expand Down Expand Up @@ -177,8 +177,8 @@ func (m *English) PriorityIsRequired() string {
return "The priority is required"
}

func (m *English) PriorityIsInvalid(id string) string {
return fmt.Sprintf("The priority (%v) is invalid", id)
func (m *English) PriorityIsInvalid(name string) string {
return fmt.Sprintf("The priority (%v) is invalid", name)
}

func (m *English) StartDateIsInvalid(date string) string {
Expand Down
21 changes: 21 additions & 0 deletions bbir/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ type ProjectRepository interface {
FindCategoryByName(name string) *v2.Category
FindVersionByName(name string) *v2.Version
FindCustomFieldByName(name string) *v2.CustomField
FindPriorityByName(name string) *v2.Priority
Prefetch(ctx context.Context) error
}

Expand All @@ -69,6 +70,7 @@ func NewProjectHTTPClient(cfg *Config, client BacklogAPIClient) ProjectRepositor
categories: make(map[string]*v2.Category),
versions: make(map[string]*v2.Version),
customFields: make(map[string]*v2.CustomField),
priorities: make(map[string]*v2.Priority),
}
}

Expand All @@ -82,6 +84,7 @@ type ProjectHTTPClient struct {
categories map[string]*v2.Category
versions map[string]*v2.Version
customFields map[string]*v2.CustomField
priorities map[string]*v2.Priority
}

func (s *ProjectHTTPClient) GetProjectID() ProjectID {
Expand Down Expand Up @@ -128,6 +131,14 @@ func (s *ProjectHTTPClient) FindCustomFieldByName(name string) *v2.CustomField {
return v
}

func (s *ProjectHTTPClient) FindPriorityByName(name string) *v2.Priority {
v, ok := s.priorities[name]
if !ok {
return nil
}
return v
}

func (s *ProjectHTTPClient) Prefetch(ctx context.Context) error {
project, err := s.client.GetProject(ctx, s.cfg.ProjectKey)
if err != nil {
Expand Down Expand Up @@ -188,6 +199,16 @@ func (s *ProjectHTTPClient) Prefetch(ctx context.Context) error {
}
return nil
},
func() error {
priorities, err := s.client.GetPriorities(errCtx)
if err != nil {
return err
}
for _, v := range priorities {
s.priorities[v.Name] = v
}
return nil
},
} {
g.Go(f)
}
Expand Down
33 changes: 33 additions & 0 deletions bbir/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type ProjectRepositoryAsMock struct {
findCategoryByName func(name string) *Category
findVersionByName func(name string) *Version
findCustomFieldByName func(name string) *CustomField
findPriorityByName func(name string) *Priority
prefetch func(ctx context.Context) error
}

Expand Down Expand Up @@ -57,6 +58,10 @@ func (r *ProjectRepositoryAsMock) FindCustomFieldByName(name string) *CustomFiel
return r.findCustomFieldByName(name)
}

func (r *ProjectRepositoryAsMock) FindPriorityByName(name string) *Priority {
return r.findPriorityByName(name)
}

func (r *ProjectRepositoryAsMock) Prefetch(ctx context.Context) error {
return r.prefetch(ctx)
}
Expand Down Expand Up @@ -123,6 +128,12 @@ func NewInjectorForRepositoryTest(t *testing.T) shot.Injector {
}}
return []*CustomField{text, sentence, number, date, singleList, multipleList, checkbox, radio}, nil
},
getPriorities: func(ctx context.Context) ([]*Priority, error) {
priority1 := &Priority{ID: 2, Name: "High"}
priority2 := &Priority{ID: 3, Name: "Normal"}
priority3 := &Priority{ID: 4, Name: "Low"}
return []*Priority{priority1, priority2, priority3}, nil
},
getIssue: func(ctx context.Context, issueKey string) (*Issue, error) {
switch issueKey {
case "EXAMPLE-1":
Expand Down Expand Up @@ -275,3 +286,25 @@ func Test_ProjectHTTPClient_FindVersionByName_should_return_nil_if_does_not_matc
t.Error("Result is not nil")
}
}

func Test_ProjectHTTPClient_FindPriorityByName_should_return_priority_that_match_name(t *testing.T) {
injector := NewInjectorForRepositoryTest(t)
project := injector.Get(new(ProjectRepository)).(*ProjectHTTPClient)
want := "Normal"
result := project.FindPriorityByName(want)
if result == nil {
t.Error("Result is nil")
}
if want != result.Name {
t.Errorf("Could not match result. want: %v, result: %v", want, result)
}
}

func Test_ProjectHTTPClient_FindPriorityByName_should_return_nil_if_does_not_match_name(t *testing.T) {
injector := NewInjectorForRepositoryTest(t)
project := injector.Get(new(ProjectRepository)).(*ProjectHTTPClient)
result := project.FindPriorityByName("xxx")
if result != nil {
t.Error("Result is not nil")
}
}
14 changes: 7 additions & 7 deletions testdata/example.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Summary (Required), Description, StartDate, DueDate, EstimatedHours, ActualHours, IssueTypeName (Required), CategoryName, VersionName, MilestoneName, PriorityID, AssigneeName, ParentIssueKey, Text, Sentence, Number, Date, Single List, Multiple List, Checkbox, Radio
Summary1,Description1,2017/01/01,2017/01/02,1.00,2.00,task,web,sprint1,sprint2,2,,,apple,orange,10,2017/01/01,select-1,select-2,select-3,select-3
Summary2,Description2,2017/02/01,2017/02/02,3.00,4.00,task,web,sprint1,sprint2,3,,,,,,,,,,
Summary3,Description3,2017/03/01,2017/03/02,5.00,6.00,task,web,sprint1,sprint2,4,,,,,,,,,,
Summary4,Description4,2017/03/01,2017/03/02,5.00,6.00,task,web,sprint1,sprint2,4,,,,,,,,,,
Summary4-1,Description4-1,,,,,task,,,,4,,*,,,,,,,,
Summary4-2,Description4-2,,,,,task,,,,4,,*,,,,,,,,
Summary4-3,Description4-3,,,,,task,,,,4,,*,,,,,,,,
Summary1,Description1,2017/01/01,2017/01/02,1.00,2.00,task,web,sprint1,sprint2,,,,apple,orange,10,2017/01/01,select-1,select-2,select-3,select-3
Summary2,Description2,2017/02/01,2017/02/02,3.00,4.00,task,web,sprint1,sprint2,,,,,,,,,,,
Summary3,Description3,2017/03/01,2017/03/02,5.00,6.00,task,web,sprint1,sprint2,,,,,,,,,,,
Summary4,Description4,2017/03/01,2017/03/02,5.00,6.00,task,web,sprint1,sprint2,,,,,,,,,,,
Summary4-1,Description4-1,,,,,task,,,,,,*,,,,,,,,
Summary4-2,Description4-2,,,,,task,,,,,,*,,,,,,,,
Summary4-3,Description4-3,,,,,task,,,,,,*,,,,,,,,
14 changes: 7 additions & 7 deletions testdata/example_ja.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
件名(必須),詳細,開始日,期限日,予定時間,実績時間,種別名(必須),カテゴリ名,発生バージョン名,マイルストーン名,優先度ID,担当者ユーザ名,親課題キー,Text,Sentence,Number,Date,Single List,Multiple List,Checkbox,Radio
テスト件名1,テスト詳細1です,2017/01/01,2017/01/02,1.00,2.00,task,web,sprint1,sprint2,2,,,,,,,,,,
テスト件名2,テスト詳細2です,2017/02/01,2017/02/02,3.00,4.00,task,web,sprint1,sprint2,3,,,,,,,,,,
テスト件名3,テスト詳細3です,2017/03/01,2017/03/02,5.00,6.00,task,web,sprint1,sprint2,4,,,,,,,,,,
テスト件名4,テスト詳細4です,2017/03/01,2017/03/02,5.00,6.00,task,web,sprint1,sprint2,4,,,,,,,,,,
テスト件名4-1,テスト詳細4-1です,,,,,task,,,,4,,*,,,,,,,,
テスト件名4-2,テスト詳細4-2です,,,,,task,,,,4,,*,,,,,,,,
テスト件名4-3,テスト詳細4-3です,,,,,task,,,,4,,*,,,,,,,,
テスト件名1,テスト詳細1です,2017/01/01,2017/01/02,1.00,2.00,task,web,sprint1,sprint2,,,,,,,,,,,
テスト件名2,テスト詳細2です,2017/02/01,2017/02/02,3.00,4.00,task,web,sprint1,sprint2,,,,,,,,,,,
テスト件名3,テスト詳細3です,2017/03/01,2017/03/02,5.00,6.00,task,web,sprint1,sprint2,,,,,,,,,,,
テスト件名4,テスト詳細4です,2017/03/01,2017/03/02,5.00,6.00,task,web,sprint1,sprint2,,,,,,,,,,,
テスト件名4-1,テスト詳細4-1です,,,,,task,,,,,,*,,,,,,,,
テスト件名4-2,テスト詳細4-2です,,,,,task,,,,,,*,,,,,,,,
テスト件名4-3,テスト詳細4-3です,,,,,task,,,,,,*,,,,,,,,

0 comments on commit 2258105

Please sign in to comment.