diff --git a/action.yml b/action.yml index e8f0339..025a371 100644 --- a/action.yml +++ b/action.yml @@ -10,7 +10,10 @@ inputs: required: true ignore-comments: description: 'ignore labels inside issue comments' - default: true + default: 'true' + include-title: + description: 'include title in labels search' + default: 'true' labels-synonyms: description: 'text synonyms for labels' default: '' diff --git a/package.json b/package.json index 8b7fd42..a776128 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "auto-label", - "version": "3.0.0", + "version": "3.1.0", "module": "src/index.ts", "type": "module", "scripts": { diff --git a/src/domain/getConfigFile.ts b/src/domain/getConfigFile.ts index b1ae52c..f71ebd3 100644 --- a/src/domain/getConfigFile.ts +++ b/src/domain/getConfigFile.ts @@ -6,6 +6,7 @@ export type Config = { defaultLabels: string[] labelsSynonyms: Record ignoreComments: boolean + includeTitle: boolean } export const getConfigFile = (): Config => { @@ -20,6 +21,7 @@ export const getConfigFile = (): Config => { {} ) const ignoreComments = getInput('ignore-comments', true) + const includeTitle = getInput('include-title', true) const config = getLabelConfigs(configPath) return { @@ -27,6 +29,7 @@ export const getConfigFile = (): Config => { defaultLabels, labelsSynonyms, ignoreComments, + includeTitle, ...config } } diff --git a/src/domain/getLabelConfigs.ts b/src/domain/getLabelConfigs.ts index 071bb1b..0766258 100644 --- a/src/domain/getLabelConfigs.ts +++ b/src/domain/getLabelConfigs.ts @@ -31,6 +31,12 @@ const getFilePath = (configurationPath: string): string | undefined => { } } +const compareArray = (arr?: string[]) => (Array.isArray(arr) ? arr : undefined) +const compareBoolean = (bool?: boolean) => + typeof bool === 'boolean' ? bool : undefined +const compareObject = (obj?: Record) => + typeof obj === 'object' && !Array.isArray(obj) ? obj : undefined + export const getLabelConfigs = (configurationPath: string): Config | {} => { const filePath = getFilePath(configurationPath) if (!filePath) return {} @@ -42,21 +48,11 @@ export const getLabelConfigs = (configurationPath: string): Config | {} => { try { const config = JSON5.parse(fileContent) const configObject = { - defaultLabels: Array.isArray(config.defaultLabels) - ? config.defaultLabels - : undefined, - labelsNotAllowed: Array.isArray(config.labelsNotAllowed) - ? config.labelsNotAllowed - : undefined, - ignoreComments: - typeof config.ignoreComments === 'boolean' - ? config.ignoreComments - : undefined, - labelsSynonyms: - typeof config.labelsSynonyms === 'object' && - !Array.isArray(config.labelsSynonyms) - ? config.labelsSynonyms - : undefined + defaultLabels: compareArray(config.defaultLabels), + labelsNotAllowed: compareArray(config.labelsNotAllowed), + ignoreComments: compareBoolean(config.ignoreComments), + includeTitle: compareBoolean(config.includeTitle), + labelsSynonyms: compareObject(config.labelsSynonyms) } return Object.fromEntries( Object.entries(configObject).filter( diff --git a/src/domain/parseText.spec.ts b/src/domain/parseText.spec.ts new file mode 100644 index 0000000..5293e81 --- /dev/null +++ b/src/domain/parseText.spec.ts @@ -0,0 +1,26 @@ +import { expect, describe, test } from 'bun:test' +import { parseText } from './parseText' + +describe('getIssueLabels function', () => { + test('should return just scoped body', () => { + const body = + 'Body with labels Label1 Label2 ' + const result = parseText(body, '', false, false) + + expect(result).toEqual(' --> Label1 Label2 Label1 Label2' + const result = parseText(body, '', true, false) + + expect(result).toEqual('Body with labels Label1 Label2') + }) + + test('should return just scoped body', () => { + const body = 'Body with labels Label1 Label2' + const result = parseText(body, 'Title', true, true) + + expect(result).toEqual('Title Body with labels Label1 Label2') + }) +}) diff --git a/src/domain/parseText.ts b/src/domain/parseText.ts new file mode 100644 index 0000000..e1ffe8c --- /dev/null +++ b/src/domain/parseText.ts @@ -0,0 +1,26 @@ +export const parseText = ( + body: string, + title: string, + ignoreComments?: boolean, + includeTitle?: boolean +): string => { + let parsedBody = body + if (parsedBody.includes('AUTO-LABEL:START')) { + const [_ignore, ...bodySplit] = body.split('AUTO-LABEL:START') + parsedBody = bodySplit + .map((elem) => elem.split('AUTO-LABEL:END')[0]) + .join(' ') + } + + if (ignoreComments && parsedBody.includes('/g, '') + } + + const response = [parsedBody] + + if (includeTitle) { + response.unshift(title) + } + + return response.join(' ') +} diff --git a/src/runner.ts b/src/runner.ts index 2a91351..6b3e82e 100644 --- a/src/runner.ts +++ b/src/runner.ts @@ -6,6 +6,7 @@ import { addLabels, getRepoLabels } from './service/github' import { removeLabelsNotAllowed } from './domain/removeLabelsNotAllowed' import { getIssueLabels } from './scraper/text' import { getConfigFile } from './domain/getConfigFile' +import { parseText } from './domain/parseText' export async function run() { try { @@ -16,8 +17,13 @@ export async function run() { const token = core.getInput('repo-token', { required: true }) const octokit = github.getOctokit(token) const issue = github.context.payload.issue! - const { labelsNotAllowed, defaultLabels, labelsSynonyms, ignoreComments } = - getConfigFile() + const { + labelsNotAllowed, + defaultLabels, + labelsSynonyms, + ignoreComments, + includeTitle + } = getConfigFile() core.endGroup() core.startGroup('Getting repository labels') @@ -28,11 +34,19 @@ export async function run() { core.info(`Considered labels: ${filteredLabels.length}`) core.endGroup() + core.startGroup(`Parsing body${includeTitle ? 'and Title' : ''}`) + const parsedBody = parseText( + issue.body || '', + issue.title || '', + ignoreComments, + includeTitle + ) core.startGroup('Getting repository labels') + + core.startGroup('Looking for labels') const issueLabels: string[] = getIssueLabels( - issue.body!, + parsedBody, repoLabels, - ignoreComments, defaultLabels, labelsSynonyms ) diff --git a/src/scraper/text.spec.ts b/src/scraper/text.spec.ts index 0426192..2eeac12 100644 --- a/src/scraper/text.spec.ts +++ b/src/scraper/text.spec.ts @@ -7,7 +7,7 @@ describe('getIssueLabels function', () => { 'Body with labels Label1 Label2 ' const labels = ['Label1', 'Label2'] - const result = getIssueLabels(body, labels, false, [], {}) + const result = getIssueLabels(body, labels, [], {}) expect(result).toEqual(['Label1', 'Label2']) }) @@ -15,7 +15,7 @@ describe('getIssueLabels function', () => { test('should handle no labels in body', () => { const body = 'No labels in this body' const labels = ['Label1', 'Label2'] - const result = getIssueLabels(body, labels, false, [], {}) + const result = getIssueLabels(body, labels, [], {}) expect(result).toEqual([]) }) @@ -25,28 +25,17 @@ describe('getIssueLabels function', () => { const labels = ['Label1', 'Label2'] const defaultLabels = ['DefaultLabel1', 'DefaultLabel2'] - const result = getIssueLabels(body, labels, false, defaultLabels, {}) + const result = getIssueLabels(body, labels, defaultLabels, {}) expect(result).toEqual(['DefaultLabel1', 'DefaultLabel2']) }) - test('should not add labels inside the comments section', () => { - const body = - 'Body with labels: Label1 ' - const labels = ['Label1', 'Label2'] - - const result = getIssueLabels(body, labels, true, [], {}) - - expect(result).toEqual(['Label1']) - }) - test('should check if there is any synonym for the labels available', () => { - const body = - 'Body with labels: Synonym1 ' + const body = 'Body with labels: Synonym1' const labels = ['Label1', 'Label2'] const labelsSynonyms = { Label1: ['Synonym1'], Label2: ['Synonym2'] } - const result = getIssueLabels(body, labels, true, [], labelsSynonyms) + const result = getIssueLabels(body, labels, [], labelsSynonyms) expect(result).toEqual(['Label1']) }) diff --git a/src/scraper/text.ts b/src/scraper/text.ts index 713ffa1..c249f63 100644 --- a/src/scraper/text.ts +++ b/src/scraper/text.ts @@ -39,42 +39,18 @@ const compareLabels = ( return hasLabels } -const parseAutoLabel = (body: string): string => { - const autoLabelRegex = new RegExp( - /(?