You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ab...@apache.org on 2022/07/22 09:40:07 UTC

[incubator-devlake] 02/13: fix: add commitfileComponent table

This is an automated email from the ASF dual-hosted git repository.

abeizn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit 128279f13c02b60aeafce5b92158cf2f77286ea8
Author: xgdyp <zx...@163.com>
AuthorDate: Fri Jul 15 09:25:29 2022 +0800

    fix: add commitfileComponent table
---
 models/domainlayer/code/commit.go                | 12 ++++++++++++
 models/migrationscripts/updateSchemas20220711.go | 16 ++++++++++++++--
 plugins/gitextractor/models/interface.go         |  1 +
 plugins/gitextractor/parser/repo.go              | 18 +++++++++++++++---
 plugins/gitextractor/store/csv.go                |  3 +++
 plugins/gitextractor/store/database.go           |  8 ++++++++
 6 files changed, 53 insertions(+), 5 deletions(-)

diff --git a/models/domainlayer/code/commit.go b/models/domainlayer/code/commit.go
index 6d1ead84..1d777624 100644
--- a/models/domainlayer/code/commit.go
+++ b/models/domainlayer/code/commit.go
@@ -66,3 +66,15 @@ type FileComponent struct {
 func (FileComponent) TableName() string {
 	return "file_component"
 }
+
+type CommitfileComponent struct {
+	common.NoPKModel
+	RepoId    string `gorm:"primaryKey;type:varchar(255)"`
+	Component string `gorm:"type:varchar(255)"`
+	CommitSha string `gorm:"primaryKey;type:varchar(40)"`
+	FilePath  string `gorm:"primaryKey;type:varchar(255)"`
+}
+
+func (CommitfileComponent) TableName() string {
+	return "commitfile_component"
+}
diff --git a/models/migrationscripts/updateSchemas20220711.go b/models/migrationscripts/updateSchemas20220711.go
index 0268f5d0..7df8df0e 100644
--- a/models/migrationscripts/updateSchemas20220711.go
+++ b/models/migrationscripts/updateSchemas20220711.go
@@ -55,11 +55,23 @@ func (CommitFile) TableName() string {
 	return "commit_files"
 }
 
+type CommitfileComponent struct {
+	common.NoPKModel
+	RepoId    string `gorm:"primaryKey;type:varchar(255)"`
+	Component string `gorm:"type:varchar(255)"`
+	CommitSha string `gorm:"primaryKey;type:varchar(40)"`
+	FilePath  string `gorm:"primaryKey;type:varchar(255)"`
+}
+
+func (CommitfileComponent) TableName() string {
+	return "commitfile_component"
+}
+
 type updateSchemas20220711 struct{}
 
 func (*updateSchemas20220711) Up(ctx context.Context, db *gorm.DB) error {
 
-	err := db.Migrator().AutoMigrate(FileComponent{}, CommitFile{})
+	err := db.Migrator().AutoMigrate(FileComponent{}, CommitFile{}, CommitfileComponent{})
 	if err != nil {
 		return err
 	}
@@ -68,7 +80,7 @@ func (*updateSchemas20220711) Up(ctx context.Context, db *gorm.DB) error {
 }
 
 func (*updateSchemas20220711) Version() uint64 {
-	return 20220711122512
+	return 20220711122544
 }
 
 func (*updateSchemas20220711) Name() string {
diff --git a/plugins/gitextractor/models/interface.go b/plugins/gitextractor/models/interface.go
index 4ed54d7e..86d73bab 100644
--- a/plugins/gitextractor/models/interface.go
+++ b/plugins/gitextractor/models/interface.go
@@ -27,5 +27,6 @@ type Store interface {
 	Refs(ref *code.Ref) error
 	CommitFiles(file *code.CommitFile) error
 	CommitParents(pp []*code.CommitParent) error
+	CommitfileComponent(component *code.CommitfileComponent) error
 	Close() error
 }
diff --git a/plugins/gitextractor/parser/repo.go b/plugins/gitextractor/parser/repo.go
index 84afbf3a..663bbfc5 100644
--- a/plugins/gitextractor/parser/repo.go
+++ b/plugins/gitextractor/parser/repo.go
@@ -312,6 +312,7 @@ func (r *GitRepo) getDiffComparedToParent(commitSha string, commit *git.Commit,
 
 func (r *GitRepo) storeCommitFilesFromDiff(commitSha string, diff *git.Diff, componentMap map[string]*regexp.Regexp) error {
 	var commitFile *code.CommitFile
+	var commitfileComponent *code.CommitfileComponent
 	var err error
 	err = diff.ForEach(func(file git.DiffDelta, progress float64) (
 		git.DiffForEachHunkCallback, error) {
@@ -322,17 +323,22 @@ func (r *GitRepo) storeCommitFilesFromDiff(commitSha string, diff *git.Diff, com
 				return nil, err
 			}
 		}
+
 		commitFile = new(code.CommitFile)
 		commitFile.CommitSha = commitSha
 		commitFile.FilePath = file.NewFile.Path
+		commitfileComponent = new(code.CommitfileComponent)
 		for component, reg := range componentMap {
 			if reg.MatchString(commitFile.FilePath) {
-				commitFile.Component = component
+				commitfileComponent.Component = component
 				break
 			}
 		}
-		if commitFile.Component == "" {
-			commitFile.Component = "Default"
+		commitfileComponent.RepoId = r.id
+		commitfileComponent.FilePath = file.NewFile.Path
+		commitfileComponent.CommitSha = commitSha
+		if commitfileComponent.Component == "" {
+			commitfileComponent.Component = "Default"
 		}
 		return func(hunk git.DiffHunk) (git.DiffForEachLineCallback, error) {
 			return func(line git.DiffLine) error {
@@ -346,6 +352,12 @@ func (r *GitRepo) storeCommitFilesFromDiff(commitSha string, diff *git.Diff, com
 			}, nil
 		}, nil
 	}, git.DiffDetailLines)
+	if commitfileComponent != nil {
+		err = r.store.CommitfileComponent(commitfileComponent)
+		if err != nil {
+			r.logger.Error("CommitfileComponent error:", err)
+		}
+	}
 	if commitFile != nil {
 		err = r.store.CommitFiles(commitFile)
 		if err != nil {
diff --git a/plugins/gitextractor/store/csv.go b/plugins/gitextractor/store/csv.go
index 2eb8edea..8a389116 100644
--- a/plugins/gitextractor/store/csv.go
+++ b/plugins/gitextractor/store/csv.go
@@ -132,6 +132,9 @@ func (c *CsvStore) Refs(ref *code.Ref) error {
 func (c *CsvStore) CommitFiles(file *code.CommitFile) error {
 	return c.commitFileWriter.Write(file)
 }
+func (c *CsvStore) CommitfileComponent(component *code.CommitfileComponent) error {
+	return c.commitFileWriter.Write(component)
+}
 
 func (c *CsvStore) CommitParents(pp []*code.CommitParent) error {
 	var err error
diff --git a/plugins/gitextractor/store/database.go b/plugins/gitextractor/store/database.go
index becb5264..2821b0f5 100644
--- a/plugins/gitextractor/store/database.go
+++ b/plugins/gitextractor/store/database.go
@@ -91,6 +91,14 @@ func (d *Database) CommitFiles(file *code.CommitFile) error {
 	return batch.Add(file)
 }
 
+func (d *Database) CommitfileComponent(commitfile *code.CommitfileComponent) error {
+	batch, err := d.driver.ForType(reflect.TypeOf(commitfile))
+	if err != nil {
+		return err
+	}
+	return batch.Add(commitfile)
+}
+
 func (d *Database) CommitParents(pp []*code.CommitParent) error {
 	if len(pp) == 0 {
 		return nil