Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/json config #80

Merged
merged 12 commits into from
May 10, 2024
26 changes: 26 additions & 0 deletions .github/workflows/auto-label.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"labels-synonyms": {
"bug": [
"error",
"need fix",
"not working"
],
"enhancement": [
"upgrade"
],
"question": [
"help",
"how can i"
]
},
"labels-not-allowed": [
"documentation",
"duplicate",
"good first issue",
"help wanted",
"invalid"
],
"default-labels": [
"triage"
]
}
17 changes: 17 additions & 0 deletions .github/workflows/auto-label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Labeling new issue

on:
issues:
types: ['opened']
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
.github/workflows/auto-label.json5
sparse-checkout-cone-mode: false
- uses: Renato66/auto-label@main
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@actions/core": "1.10.1",
"@actions/github": "6.0.0"
"@actions/github": "6.0.0",
"json5": "^2.2.3"
}
}
16 changes: 14 additions & 2 deletions src/domain/getConfigFile.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { getInput } from './getInput'
import { getLabelConfigs } from './getJsonFile'

export const getConfigFile = () => {
export type Config = {
labelsNotAllowed: string[];
defaultLabels: string[];
labelsSynonyms: Record<string, string[]>;
ignoreComments: boolean;
}

export const getConfigFile = (): Config => {

const configPath = getInput<string>('configuration-file', '.github/workflows/auto-label.json5')
const labelsNotAllowed = getInput<string[]>('labels-not-allowed', [])
const defaultLabels = getInput<string[]>('default-labels', [])
const labelsSynonyms = getInput<Record<string, string[]>>(
'labels-synonyms',
{}
)
const ignoreComments = getInput('ignore-comments', true)
const config = getLabelConfigs(configPath)

return {
labelsNotAllowed,
defaultLabels,
labelsSynonyms,
ignoreComments
ignoreComments,
...config
}
}
25 changes: 25 additions & 0 deletions src/domain/getJsonFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import fs from 'fs'
import type { Config } from './getConfigFile'
import * as core from '@actions/core'
import JSON5 from 'json5'

export const getLabelConfigs = (configurationPath: string): Config | {} => {
if (fs.existsSync(configurationPath)) return {}
Renato66 marked this conversation as resolved.
Show resolved Hide resolved
const fileContent = fs.readFileSync(configurationPath, {
encoding: 'utf8'
})

try {
const config =
JSON5.parse(fileContent)
return {
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
}
} catch {
core.warning('Could not parse configuration file. skipping')
return {}
Renato66 marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading