Skip to content

Commit

Permalink
When entity hidden, should also hide branch and dependencies unless s…
Browse files Browse the repository at this point in the history
…pecific patterns set for those
  • Loading branch information
alanhamlett authored and gandarez committed Jan 11, 2025
1 parent 0bc3464 commit 10d8d04
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
22 changes: 15 additions & 7 deletions pkg/heartbeat/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@ func Sanitize(ctx context.Context, h Heartbeat, config SanitizeConfig) Heartbeat
ProjectPathOverride: h.ProjectPathOverride,
}

// project patterns
if h.Project != nil {
check.Patterns = config.ProjectPatterns
if ShouldSanitize(ctx, check) {
h = sanitizeMetaData(h)
}
}

// file patterns
check.Patterns = config.FilePatterns
if ShouldSanitize(ctx, check) {
Expand All @@ -64,15 +72,15 @@ func Sanitize(ctx context.Context, h Heartbeat, config SanitizeConfig) Heartbeat
h.Entity = "HIDDEN"
}

h = sanitizeMetaData(h)
}
if len(config.BranchPatterns) == 0 {
h.Branch = nil
}

// project patterns
if h.Project != nil {
check.Patterns = config.ProjectPatterns
if ShouldSanitize(ctx, check) {
h = sanitizeMetaData(h)
if len(config.DependencyPatterns) == 0 {
h.Dependencies = nil
}

h = sanitizeMetaData(h)
}

// branch patterns
Expand Down
55 changes: 25 additions & 30 deletions pkg/heartbeat/sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ func TestWithSanitization_ObfuscateFile(t *testing.T) {
handle := opt(func(_ context.Context, hh []heartbeat.Heartbeat) ([]heartbeat.Result, error) {
assert.Equal(t, []heartbeat.Heartbeat{
{
Branch: heartbeat.PointerTo("heartbeat"),
Category: heartbeat.CodingCategory,
Dependencies: []string{"dep1", "dep2"},
Entity: "HIDDEN.go",
EntityType: heartbeat.FileType,
IsWrite: heartbeat.PointerTo(true),
Language: heartbeat.PointerTo("Go"),
Project: heartbeat.PointerTo("wakatime"),
Time: 1585598060,
UserAgent: "wakatime/13.0.7",
Category: heartbeat.CodingCategory,
Entity: "HIDDEN.go",
EntityType: heartbeat.FileType,
IsWrite: heartbeat.PointerTo(true),
Language: heartbeat.PointerTo("Go"),
Project: heartbeat.PointerTo("wakatime"),
Time: 1585598060,
UserAgent: "wakatime/13.0.7",
},
}, hh)

Expand Down Expand Up @@ -74,16 +72,14 @@ func TestSanitize_Obfuscate(t *testing.T) {
UserAgent: "wakatime/13.0.7",
},
Expected: heartbeat.Heartbeat{
Branch: heartbeat.PointerTo("heartbeat"),
Category: heartbeat.CodingCategory,
Dependencies: []string{"dep1", "dep2"},
Entity: "HIDDEN.go",
EntityType: heartbeat.FileType,
IsWrite: heartbeat.PointerTo(true),
Language: heartbeat.PointerTo("Go"),
Project: heartbeat.PointerTo("wakatime"),
Time: 1585598060,
UserAgent: "wakatime/13.0.7",
Category: heartbeat.CodingCategory,
Entity: "HIDDEN.go",
EntityType: heartbeat.FileType,
IsWrite: heartbeat.PointerTo(true),
Language: heartbeat.PointerTo("Go"),
Project: heartbeat.PointerTo("wakatime"),
Time: 1585598060,
UserAgent: "wakatime/13.0.7",
},
},
"app": {
Expand Down Expand Up @@ -138,16 +134,15 @@ func TestSanitize_ObfuscateFile_SkipBranchIfNotMatching(t *testing.T) {
})

assert.Equal(t, heartbeat.Heartbeat{
Branch: heartbeat.PointerTo("heartbeat"),
Dependencies: []string{"dep1", "dep2"},
Category: heartbeat.CodingCategory,
Entity: "HIDDEN.go",
EntityType: heartbeat.FileType,
IsWrite: heartbeat.PointerTo(true),
Language: heartbeat.PointerTo("Go"),
Project: heartbeat.PointerTo("wakatime"),
Time: 1585598060,
UserAgent: "wakatime/13.0.7",
Branch: heartbeat.PointerTo("heartbeat"),
Category: heartbeat.CodingCategory,
Entity: "HIDDEN.go",
EntityType: heartbeat.FileType,
IsWrite: heartbeat.PointerTo(true),
Language: heartbeat.PointerTo("Go"),
Project: heartbeat.PointerTo("wakatime"),
Time: 1585598060,
UserAgent: "wakatime/13.0.7",
}, r)
}

Expand Down

0 comments on commit 10d8d04

Please sign in to comment.