Skip to content

Commit

Permalink
fix: do not add weight twice when two nodes are linked multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 6, 2018
1 parent 6827155 commit a2f6fd7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
8 changes: 2 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,13 @@ func dbExists(opts *dbOptions) bool {

func dbLoad(opts *dbOptions) (Issues, error) {
logger().Debug("dbLoad", zap.Stringer("opts", *opts))
var issues []*Issue
var issues IssueSlice
content, err := ioutil.ReadFile(opts.Path)
if err != nil {
return nil, errors.Wrap(err, "failed to open db file")
}
if err := json.Unmarshal(content, &issues); err != nil {
return nil, errors.Wrap(err, "failed to parse db file")
}
m := make(Issues)
for _, issue := range issues {
m[issue.NodeName()] = issue
}
return m, nil
return issues.ToMap(), nil
}
30 changes: 26 additions & 4 deletions issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

type Issue struct {
github.Issue
DependsOn []*Issue
Blocks []*Issue
DependsOn IssueSlice
Blocks IssueSlice
weightMultiplier int
BaseWeight int
IsOrphan bool
Expand All @@ -25,8 +25,30 @@ type Issue struct {
Errors []error
}

type IssueSlice []*Issue

func (s IssueSlice) Unique() IssueSlice {
return s.ToMap().ToSlice()
}

type Issues map[string]*Issue

func (m Issues) ToSlice() IssueSlice {
slice := IssueSlice{}
for _, issue := range m {
slice = append(slice, issue)
}
return slice
}

func (s IssueSlice) ToMap() Issues {
m := Issues{}
for _, issue := range s {
m[issue.NodeName()] = issue
}
return m
}

func (i Issue) IsEpic() bool {
for _, label := range i.Labels {
if *label.Name == viper.GetString("epic-label") {
Expand Down Expand Up @@ -119,15 +141,15 @@ func (i Issue) DependsOnAnEpic() bool {

func (i Issue) Weight() int {
weight := i.BaseWeight
for _, dep := range i.Blocks {
for _, dep := range i.Blocks.Unique() {
weight += dep.Weight()
}
return weight * i.WeightMultiplier()
}

func (i Issue) WeightMultiplier() int {
multiplier := i.weightMultiplier
for _, dep := range i.Blocks {
for _, dep := range i.Blocks.Unique() {
multiplier *= dep.WeightMultiplier()
}
return multiplier
Expand Down

0 comments on commit a2f6fd7

Please sign in to comment.