Skip to content
This repository was archived by the owner on May 24, 2024. It is now read-only.

Commit a5ca0d1

Browse files
Push LOC using cloc (#75)
* push LOC using cloc Signed-off-by: Ayman <[email protected]> * test changes Signed-off-by: Ayman <[email protected]> * code clean up Signed-off-by: Ayman <[email protected]> * include endpoint in input file name Signed-off-by: Ayman <[email protected]> --------- Signed-off-by: Ayman <[email protected]> Co-authored-by: Ayman <[email protected]>
1 parent cc6233a commit a5ca0d1

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

cmd/git/git.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ var (
524524

525525
// Publisher - for streaming data to Kinesis
526526
type Publisher interface {
527-
PushEvents(action, source, eventType, subEventType, env string, data []interface{}) (string, error)
527+
PushEvents(action, source, eventType, subEventType, env string, data []interface{}, endpoint string) (string, error)
528528
}
529529

530530
// RawPLS - programming language summary (all fields as strings)
@@ -946,7 +946,8 @@ func (j *DSGit) EnrichItem(ctx *shared.Ctx, item map[string]interface{}) (rich m
946946
err = fmt.Errorf("cannot parse author date from %v", iAuthorDate)
947947
return
948948
}
949-
949+
clocCount, _ := shared.Dig(commit, []string{"cloc_count"}, true, false)
950+
rich["cloc_count"] = clocCount
950951
authorLocation := time.FixedZone(fmt.Sprintf("UTC%v", authorTz), int(authorTz)*60*60)
951952
authorLocalDate := time.Date(authorDate.Year(), authorDate.Month(), authorDate.Day(), authorDate.Hour(), authorDate.Minute(), authorDate.Second(), authorDate.Nanosecond(), authorLocation)
952953
rich["orphaned"] = false
@@ -1342,6 +1343,10 @@ func (j *DSGit) GetModelData(ctx *shared.Ctx, docs []interface{}) []git.CommitCr
13421343
for _, value := range fileCache {
13431344
commit.Files = append(commit.Files, *value)
13441345
}
1346+
if len(commit.Files) > 0 {
1347+
clocCount, _ := doc["cloc_count"].(int)
1348+
commit.Files[len(commit.Files)-1].ActualLinesOfCode = clocCount
1349+
}
13451350
}
13461351
commit.MergeCommit = len(fileAry) == 0
13471352
// Event
@@ -1803,7 +1808,7 @@ func (j *DSGit) GitEnrichItems(ctx *shared.Ctx, thrN int, items []interface{}, d
18031808
}
18041809
path := ""
18051810
if len(formattedData) > 0 {
1806-
path, err = j.Publisher.PushEvents(CommitCreated, "insights", GitDataSource, "commits", os.Getenv("STAGE"), formattedData)
1811+
path, err = j.Publisher.PushEvents(CommitCreated, "insights", GitDataSource, "commits", os.Getenv("STAGE"), formattedData, j.endpoint)
18071812
if err != nil {
18081813
j.log.WithFields(logrus.Fields{"operation": "GitEnrichItems"}).Errorf("Error: %+v", err)
18091814
return
@@ -2561,6 +2566,20 @@ func (j *DSGit) Sync(ctx *shared.Ctx) (err error) {
25612566
return
25622567
}
25632568
processCommit := func(c chan error, commit map[string]interface{}) (wch chan error, e error) {
2569+
sha, _ := commit["commit"].(string)
2570+
cmdLine := []string{"cloc", "commit", sha, "--json"}
2571+
sout, serr, err := shared.ExecCommand(ctx, cmdLine, j.GitPath, GitDefaultEnv)
2572+
if err != nil {
2573+
j.log.WithFields(logrus.Fields{"operation": "Sync"}).Errorf("error executing command: %v, error: %v, output: %s, output error: %s", cmdLine, err, sout, serr)
2574+
return
2575+
}
2576+
r := make(map[string]clocResult)
2577+
err = jsoniter.Unmarshal([]byte(sout), &r)
2578+
if err != nil {
2579+
j.log.WithFields(logrus.Fields{"operation": "Sync"}).Errorf("error unmarshall: %v, error: %v", sout, err)
2580+
return
2581+
}
2582+
commit["cloc_count"] = r["SUM"].Code
25642583
defer func() {
25652584
if c != nil {
25662585
c <- e
@@ -2924,7 +2943,7 @@ func (j *DSGit) handleDataLakeOrphans() {
29242943
}
29252944

29262945
if len(formattedData) > 0 {
2927-
path, err := j.Publisher.PushEvents(CommitUpdated, "insights", GitDataSource, "commits", os.Getenv("STAGE"), formattedData)
2946+
path, err := j.Publisher.PushEvents(CommitUpdated, "insights", GitDataSource, "commits", os.Getenv("STAGE"), formattedData, j.endpoint)
29282947
if err != nil {
29292948
j.log.WithFields(logrus.Fields{"operation": "handleDataLakeOrphans"}).Errorf("error pushing data lake orphand commits: %+v", err)
29302949
return
@@ -2969,3 +2988,10 @@ type ReportData struct {
29692988
SyncStatus string `json:"sync_status"`
29702989
OrphanedCommits int64 `json:"orphaned_commits"`
29712990
}
2991+
2992+
type clocResult struct {
2993+
Code int `json:"code"`
2994+
Blank int `json:"blank"`
2995+
Comment int `json:"comment"`
2996+
NumberOfFiles int `json:"nFiles"`
2997+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.17
44

55
require (
66
github.com/LF-Engineering/insights-datasource-shared v1.5.21
7-
github.com/LF-Engineering/lfx-event-schema v0.1.35
7+
github.com/LF-Engineering/lfx-event-schema v0.1.37
88
github.com/aws/aws-lambda-go v1.27.1
99
github.com/aws/aws-sdk-go v1.42.25
1010
github.com/json-iterator/go v1.1.11

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ github.com/DataDog/sketches-go v1.2.1/go.mod h1:1xYmPLY1So10AwxV6MJV0J53XVH+WL9A
1010
github.com/LF-Engineering/insights-datasource-shared v1.5.21 h1:cZHytRoA5pZHsQlpeasV5K2b2jZ66ikylvNvzlBSj9s=
1111
github.com/LF-Engineering/insights-datasource-shared v1.5.21/go.mod h1:9DmFQbC8nnm1C7k+/tDo3Rmqzzx7AzmhPBlFouXaBZ8=
1212
github.com/LF-Engineering/lfx-event-schema v0.1.14/go.mod h1:CfFIZ4mwzo88umf5+KxDQEzqlVkPG7Vx8eLK2oDfWIs=
13-
github.com/LF-Engineering/lfx-event-schema v0.1.35 h1:kvhuZxXS6JRclVJJVYkkXbImaResQ+FeeM5k9sQooSE=
14-
github.com/LF-Engineering/lfx-event-schema v0.1.35/go.mod h1:CfFIZ4mwzo88umf5+KxDQEzqlVkPG7Vx8eLK2oDfWIs=
13+
github.com/LF-Engineering/lfx-event-schema v0.1.37 h1:ny46D2NdCXokvJZ01GJcw2RfQM64ousJjaYsrRj5zzg=
14+
github.com/LF-Engineering/lfx-event-schema v0.1.37/go.mod h1:CfFIZ4mwzo88umf5+KxDQEzqlVkPG7Vx8eLK2oDfWIs=
1515
github.com/Microsoft/go-winio v0.5.0/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
1616
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
1717
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=

0 commit comments

Comments
 (0)