Skip to content

Commit 77fb98c

Browse files
Copilotfelickz
andcommitted
Extract normalization logic to shared utility function and regenerate dist
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent 4b9ced2 commit 77fb98c

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
lines changed

__tests__/main.test.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {normalizeCweId} from '../src/utils'
2+
13
describe('main', () => {
24
it('placeholder test', () => {
35
// This is a placeholder test to prevent Jest from failing
@@ -6,36 +8,28 @@ describe('main', () => {
68
})
79

810
describe('CWE ID normalization', () => {
9-
const codeQlCweTagPrefix = 'external/cwe/cwe-'
10-
11-
function normalizeCweId(tag: string): string {
12-
const cweId = tag.replace(codeQlCweTagPrefix, '')
13-
const normalizedId = String(parseInt(cweId, 10))
14-
return normalizedId
15-
}
16-
1711
it('should handle CWE IDs with leading zeros', () => {
18-
// Test that cwe-099 maps to 99
19-
const normalizedId = normalizeCweId('external/cwe/cwe-099')
12+
// Test that 099 maps to 99
13+
const normalizedId = normalizeCweId('099')
2014
expect(normalizedId).toBe('99')
2115
})
2216

2317
it('should handle CWE IDs without leading zeros', () => {
24-
// Test that cwe-89 maps to 89
25-
const normalizedId = normalizeCweId('external/cwe/cwe-89')
18+
// Test that 89 maps to 89
19+
const normalizedId = normalizeCweId('89')
2620
expect(normalizedId).toBe('89')
2721
})
2822

2923
it('should handle CWE IDs with multiple leading zeros', () => {
30-
// Test that cwe-020 maps to 20
31-
const normalizedId = normalizeCweId('external/cwe/cwe-020')
24+
// Test that 020 maps to 20
25+
const normalizedId = normalizeCweId('020')
3226
expect(normalizedId).toBe('20')
3327
})
3428

35-
it('should return NaN for non-numeric CWE IDs', () => {
36-
// Test that invalid CWE IDs return NaN
37-
const normalizedId = normalizeCweId('external/cwe/cwe-abc')
38-
expect(normalizedId).toBe('NaN')
29+
it('should return null for non-numeric CWE IDs', () => {
30+
// Test that invalid CWE IDs return null
31+
const normalizedId = normalizeCweId('abc')
32+
expect(normalizedId).toBeNull()
3933
})
4034
})
4135
})

dist/index.js

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as core from '@actions/core'
55
import {DOMParser} from '@xmldom/xmldom'
66
import * as xpath from 'xpath'
77
import {JSONPath} from 'jsonpath-plus'
8-
import {LogLevel, log} from './utils'
8+
import {LogLevel, log, normalizeCweId} from './utils'
99

1010
let sarifFilePath: string
1111
let outputFilePath: string
@@ -104,12 +104,11 @@ JSONPath({
104104
if (tag.startsWith(codeQlCweTagPrefix)) {
105105
const cweId = tag.replace(codeQlCweTagPrefix, '')
106106
// Normalize CWE ID by converting to integer to remove leading zeros
107-
const parsedCweId = parseInt(cweId, 10)
107+
const normalizedCweId = normalizeCweId(cweId)
108108
// Skip if the CWE ID is not a valid number
109-
if (Number.isNaN(parsedCweId)) {
109+
if (normalizedCweId === null) {
110110
continue
111111
}
112-
const normalizedCweId = String(parsedCweId)
113112
if (cweIds.includes(normalizedCweId)) {
114113
tags.push(securityStandardTag)
115114
tags.push(...cweCategories[normalizedCweId])

src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,16 @@ export function log(message: string, level = LogLevel.Info): void {
4141
}
4242
}
4343
}
44+
45+
/**
46+
* Normalize a CWE ID by removing leading zeros
47+
* @param cweId - The CWE ID string (e.g., "099", "020", "89")
48+
* @returns The normalized CWE ID string (e.g., "99", "20", "89") or null if invalid
49+
*/
50+
export function normalizeCweId(cweId: string): string | null {
51+
const parsedCweId = parseInt(cweId, 10)
52+
if (Number.isNaN(parsedCweId)) {
53+
return null
54+
}
55+
return String(parsedCweId)
56+
}

0 commit comments

Comments
 (0)