Skip to content

Commit 634250b

Browse files
committed
Handled sentinel policy scenario
1 parent 87b93e5 commit 634250b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

addlicense/main.go

+24
Original file line numberDiff line numberDiff line change
@@ -414,8 +414,31 @@ var head = []string{
414414
"/** @jest-environment", // Jest Environment string https://jestjs.io/docs/configuration#testenvironment-string
415415
}
416416

417+
var headPatterns = []string{
418+
`# This policy requires.*\s*(#.*\s*)*`,
419+
}
420+
421+
// matches regex patterns to extract headings to skip
422+
func matchPattern(b []byte) []byte {
423+
424+
for _, v := range headPatterns {
425+
re := regexp.MustCompile(v)
426+
match := re.Find(b)
427+
if len(match) > 0 {
428+
return match
429+
}
430+
}
431+
return []byte{}
432+
}
433+
417434
func hashBang(b []byte) []byte {
418435
var line []byte
436+
437+
line = matchPattern(b)
438+
if len(line) > 0 {
439+
return line
440+
}
441+
419442
for _, c := range b {
420443
line = append(line, c)
421444
if c == '\n' {
@@ -428,6 +451,7 @@ func hashBang(b []byte) []byte {
428451
return line
429452
}
430453
}
454+
431455
return nil
432456
}
433457

addlicense/main_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,17 @@ func TestAddLicense(t *testing.T) {
245245
{"# escape: `\ncontent", "# escape: `\n// HYS\n\ncontent", true},
246246
{"# syntax: docker/dockerfile:1.3\ncontent", "# syntax: docker/dockerfile:1.3\n// HYS\n\ncontent", true},
247247
{"/** @jest-environment jsdom */\ncontent", "/** @jest-environment jsdom */\n// HYS\n\ncontent", true},
248+
{
249+
"# This policy requires immediate action.\n# This is a follow-up comment.\n# Another line of policy.\nSome text that should not match.",
250+
"# This policy requires immediate action.\n# This is a follow-up comment.\n# Another line of policy.\n// HYS\n\nSome text that should not match.",
251+
true,
252+
},
253+
{
254+
`# This policy requires that the max_password_age attribute of the aws_iam_account_password_policy
255+
# resource is according to CIS standards.`,
256+
"# This policy requires that the max_password_age attribute of the aws_iam_account_password_policy\n\t\t\t# resource is according to CIS standards.\n// HYS\n\n",
257+
true,
258+
},
248259

249260
// ensure files with existing license or generated files are
250261
// skipped. No need to test all permutations of these, since

0 commit comments

Comments
 (0)