浏览代码

Fix the duplication in Pipeline.DeployItem

Vadim Markovtsev 7 年之前
父节点
当前提交
527224b1f3
共有 4 个文件被更改,包括 40 次插入3 次删除
  1. 4 1
      identity.go
  2. 5 0
      identity_test.go
  3. 24 2
      pipeline.go
  4. 7 0
      pipeline_test.go

+ 4 - 1
identity.go

@@ -65,7 +65,10 @@ func (id *IdentityDetector) Configure(facts map[string]interface{}) {
 			id.LoadPeopleDict(peopleDictPath)
 			facts[FactIdentityDetectorPeopleCount] = len(id.ReversedPeopleDict) - 1
 		} else {
-			id.GeneratePeopleDict(facts["commits"].([]*object.Commit))
+			if _, exists := facts[FactPipelineCommits]; !exists {
+				panic("IdentityDetector needs a list of commits to initialize.")
+			}
+			id.GeneratePeopleDict(facts[FactPipelineCommits].([]*object.Commit))
 			facts[FactIdentityDetectorPeopleCount] = len(id.ReversedPeopleDict)
 		}
 	} else {

+ 5 - 0
identity_test.go

@@ -123,6 +123,11 @@ func TestIdentityDetectorRegistration(t *testing.T) {
 	assert.Equal(t, tps[0].Elem().Name(), "IdentityDetector")
 }
 
+func TestIdentityDetectorConfigureEmpty(t *testing.T) {
+	id := IdentityDetector{}
+	assert.Panics(t, func() {id.Configure(map[string]interface{}{})})
+}
+
 func TestIdentityDetectorConsume(t *testing.T) {
 	commit, _ := testRepository.CommitObject(plumbing.NewHash(
 		"5c0e755dd85ac74584d9988cc361eccf02ce1a48"))

+ 24 - 2
pipeline.go

@@ -231,6 +231,8 @@ type Pipeline struct {
 	features map[string]bool
 }
 
+const FactPipelineCommits = "commits"
+
 func NewPipeline(repository *git.Repository) *Pipeline {
 	return &Pipeline{
 		repository: repository,
@@ -267,6 +269,9 @@ func (pipeline *Pipeline) DeployItem(item PipelineItem) PipelineItem {
 	queue := []PipelineItem{}
 	queue = append(queue, item)
 	added := map[string]PipelineItem{}
+	for _, item := range pipeline.items {
+		added[item.Name()] = item
+	}
 	added[item.Name()] = item
 	pipeline.AddItem(item)
 	for len(queue) > 0 {
@@ -382,7 +387,21 @@ func (pipeline *Pipeline) resolve(dumpPath string) {
 			graph.AddNode(key)
 			if graph.AddEdge(name, key) > 1 {
 				if ambiguousMap[key] != nil {
-					panic("Failed to resolve pipeline dependencies.")
+					fmt.Fprintln(os.Stderr, "Pipeline:")
+					for _, item2 := range pipeline.items {
+						if item2 == item {
+							fmt.Fprint(os.Stderr, "> ")
+						}
+						fmt.Fprint(os.Stderr, item2.Name(), " [")
+						for i, key2 := range item2.Provides() {
+							fmt.Fprint(os.Stderr, key2)
+							if i < len(item.Provides()) - 1 {
+								fmt.Fprint(os.Stderr, ", ")
+							}
+						}
+						fmt.Fprintln(os.Stderr, "]")
+					}
+					panic("Failed to resolve pipeline dependencies: ambiguous graph.")
 				}
 				ambiguousMap[key] = graph.FindParents(key)
 			}
@@ -450,7 +469,7 @@ func (pipeline *Pipeline) resolve(dumpPath string) {
 	}
 	strplan, ok := graph.Toposort()
 	if !ok {
-		panic("Failed to resolve pipeline dependencies.")
+		panic("Failed to resolve pipeline dependencies: unable to topologically sort the items.")
 	}
 	pipeline.items = make([]PipelineItem, 0, len(pipeline.items))
 	for _, key := range strplan {
@@ -467,6 +486,9 @@ func (pipeline *Pipeline) Initialize(facts map[string]interface{}) {
 	if facts == nil {
 		facts = map[string]interface{}{}
 	}
+	if _, exists := facts[FactPipelineCommits]; !exists {
+		facts[FactPipelineCommits] = pipeline.Commits()
+	}
 	dumpPath, _ := facts["Pipeline.DumpPath"].(string)
 	pipeline.resolve(dumpPath)
 	if dryRun, _ := facts["Pipeline.DryRun"].(bool); dryRun {

+ 7 - 0
pipeline_test.go

@@ -420,3 +420,10 @@ func init() {
 		URL: "https://github.com/src-d/hercules",
 	})
 }
+
+func TestPipelineResolveIntegration(t *testing.T) {
+	pipeline := NewPipeline(testRepository)
+	pipeline.DeployItem(&BurndownAnalysis{})
+	pipeline.DeployItem(&CouplesAnalysis{})
+	pipeline.Initialize(nil)
+}