Skip to content

Commit 5d1e50a

Browse files
committed
refactor: remove form scrapper/text the parser
1 parent 5fc2b85 commit 5d1e50a

File tree

4 files changed

+57
-43
lines changed

4 files changed

+57
-43
lines changed

src/domain/parseText.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { expect, describe, test } from 'bun:test'
2+
import { parseText } from './parseText'
3+
4+
describe('getIssueLabels function', () => {
5+
test('should return just scoped body', () => {
6+
const body =
7+
'Body with labels <!-- AUTO-LABEL:START --> Label1 Label2 <!-- AUTO-LABEL:END -->'
8+
const result = parseText(body, '', false, false)
9+
10+
expect(result).toEqual(' --> Label1 Label2 <!-- ')
11+
})
12+
13+
test('should return just scoped body', () => {
14+
const body =
15+
'Body with labels <!-- Label3 --> Label1 Label2'
16+
const result = parseText(body, '', true, false)
17+
18+
expect(result).toEqual('Body with labels Label1 Label2')
19+
})
20+
21+
test('should return just scoped body', () => {
22+
const body =
23+
'Body with labels <!-- Label3 --> Label1 Label2'
24+
const result = parseText(body, 'Title', true, true)
25+
26+
expect(result).toEqual('Title Body with labels Label1 Label2')
27+
})
28+
})

src/domain/parseText.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const parseText = (body: string, title: string, ignoreComments?: boolean, includeTitle?: boolean): string => {
2+
let parsedBody = body
3+
if (parsedBody.includes('AUTO-LABEL:START')) {
4+
const [_ignore, ...bodySplit] = body.split('AUTO-LABEL:START')
5+
parsedBody = bodySplit.map(elem => elem.split('AUTO-LABEL:END')[0]).join(' ')
6+
}
7+
8+
9+
if (ignoreComments && parsedBody.includes('<!--')) {
10+
parsedBody = parsedBody.replace(/\<!--(.|\n)*?-->/g, '')
11+
}
12+
13+
const response = [parsedBody]
14+
15+
if (includeTitle) {
16+
response.unshift(title)
17+
}
18+
19+
return response.join(' ')
20+
}

src/scraper/text.spec.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ describe('getIssueLabels function', () => {
77
'Body with labels <!-- AUTO-LABEL:START --> Label1 Label2 <!-- AUTO-LABEL:END -->'
88
const labels = ['Label1', 'Label2']
99

10-
const result = getIssueLabels(body, labels, false, [], {})
10+
const result = getIssueLabels(body, labels, [], {})
1111

1212
expect(result).toEqual(['Label1', 'Label2'])
1313
})
1414

1515
test('should handle no labels in body', () => {
1616
const body = 'No labels in this body'
1717
const labels = ['Label1', 'Label2']
18-
const result = getIssueLabels(body, labels, false, [], {})
18+
const result = getIssueLabels(body, labels, [], {})
1919

2020
expect(result).toEqual([])
2121
})
@@ -25,28 +25,18 @@ describe('getIssueLabels function', () => {
2525
const labels = ['Label1', 'Label2']
2626
const defaultLabels = ['DefaultLabel1', 'DefaultLabel2']
2727

28-
const result = getIssueLabels(body, labels, false, defaultLabels, {})
28+
const result = getIssueLabels(body, labels, defaultLabels, {})
2929

3030
expect(result).toEqual(['DefaultLabel1', 'DefaultLabel2'])
3131
})
3232

33-
test('should not add labels inside the comments section', () => {
34-
const body =
35-
'Body with labels: Label1 <!-- the label Label2 is mentioned in the body, but its commented and shouldn`t be tracked -->'
36-
const labels = ['Label1', 'Label2']
37-
38-
const result = getIssueLabels(body, labels, true, [], {})
39-
40-
expect(result).toEqual(['Label1'])
41-
})
42-
4333
test('should check if there is any synonym for the labels available', () => {
4434
const body =
45-
'Body with labels: Synonym1 <!-- the label Synonym2 is mentioned in the body, but its commented and shouldn`t be tracked -->'
35+
'Body with labels: Synonym1'
4636
const labels = ['Label1', 'Label2']
4737
const labelsSynonyms = { Label1: ['Synonym1'], Label2: ['Synonym2'] }
4838

49-
const result = getIssueLabels(body, labels, true, [], labelsSynonyms)
39+
const result = getIssueLabels(body, labels, [], labelsSynonyms)
5040

5141
expect(result).toEqual(['Label1'])
5242
})

src/scraper/text.ts

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,42 +39,18 @@ const compareLabels = (
3939
return hasLabels
4040
}
4141

42-
const parseAutoLabel = (body: string): string => {
43-
const autoLabelRegex = new RegExp(
44-
/<!-- AUTO-LABEL:START -->(?<label>(\s*\w.+|\n)*?)\s*<!-- AUTO-LABEL:END -->/,
45-
'gm'
46-
)
47-
const autoLabels = body.match(autoLabelRegex)
48-
49-
if (!autoLabels) return body
50-
51-
const replaceAutoLabelByLabelValue = (autoLabel: string) =>
52-
autoLabel.replace(autoLabelRegex, '$1').trim()
53-
54-
return autoLabels.map(replaceAutoLabelByLabelValue).join(' ')
55-
}
56-
5742
export const getIssueLabels = (
58-
body: string,
43+
text: string,
5944
labels: string[],
60-
ignoreComments: boolean,
6145
defaultLabels: string[],
6246
labelsSynonyms: Record<string, string[]>
6347
): string[] => {
6448
const selectedLabels: string[] = []
6549
const hasLabels = compareLabels(labels, labelsSynonyms)
6650

67-
const parsedBody = parseAutoLabel(body)
51+
hasLabels(text).forEach((elem) => {
52+
selectedLabels.push(elem)
53+
})
6854

69-
if (ignoreComments) {
70-
const noCommentaryBody = parsedBody.replace(/\<!--(.|\n)*?-->/g, '')
71-
hasLabels(noCommentaryBody).map((elem) => {
72-
selectedLabels.push(elem)
73-
})
74-
} else {
75-
hasLabels(parsedBody).map((elem) => {
76-
selectedLabels.push(elem)
77-
})
78-
}
7955
return [...new Set([...selectedLabels, ...defaultLabels])]
8056
}

0 commit comments

Comments
 (0)