Skip to content

Commit

Permalink
feat(code-commit): support getting project scopes (#466)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelfimov authored Dec 23, 2024
1 parent 02aa391 commit 7ee2b0a
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
49 changes: 45 additions & 4 deletions code/code-commit/src/commit.linter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { LintOutcome } from '@commitlint/types'
import type { FormattableReport } from '@commitlint/types'
import type { LintOptions } from '@commitlint/types'

import { RuleConfigSeverity } from '@commitlint/types'
import { LintOutcome } from '@commitlint/types'
import { QualifiedRules } from '@commitlint/types'
import { format } from '@commitlint/format/lib/format.js'
import commitlint from '@commitlint/lint'

Expand All @@ -15,11 +18,29 @@ const defaultParserOpts = {
revertCorrespondence: ['header', 'hash'],
}

const options: LintOptions = {
parserOpts: defaultParserOpts,
}

interface CommitLinterOptions {
scopes?: Array<string>
workspaceNames?: Array<string>
}

export class CommitLinter {
readonly scopes?: Array<string>

readonly workspaceNames?: Array<string>

constructor({ scopes, workspaceNames }: CommitLinterOptions) {
this.scopes = scopes
this.workspaceNames = workspaceNames
}

async lint(message: string): Promise<LintOutcome> {
return commitlint(message, rules, {
parserOpts: defaultParserOpts,
})
const lintRules = this.prepareConfig(rules)

return commitlint(message, lintRules, options)
}

format(
Expand All @@ -30,4 +51,24 @@ export class CommitLinter {
): string {
return format(report, options)
}

/**
* Prepares config, including scopes
*/
private prepareConfig(rules: QualifiedRules) {
const allowedScopes = []

if (this.scopes) {
allowedScopes.push(...this.scopes.filter((scope) => scope && scope !== 'atls'))
}

if (this.workspaceNames) {
allowedScopes.push(...this.workspaceNames.filter((workspaceName) => workspaceName))
}

const possibleScopeValuesArray = ['common', 'github', ...allowedScopes]
rules['scope-enum'] = [RuleConfigSeverity.Error, 'always', possibleScopeValuesArray]

return rules
}
}
25 changes: 19 additions & 6 deletions yarn/plugin-commit/sources/commit-message-lint.command.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { BaseCommand } from '@yarnpkg/cli'
import { BaseCommand } from '@yarnpkg/cli'
import { Configuration } from '@yarnpkg/core'
import { Project } from '@yarnpkg/core'

import { CommitLinter } from '@atls/code-commit'
import { read } from '@atls/code-commit'
import { CommitLinter } from '@atls/code-commit'
import { read } from '@atls/code-commit'

class CommitMessageLintCommand extends BaseCommand {
static paths = [['commit', 'message', 'lint']]
static override paths = [['commit', 'message', 'lint']]

async execute(): Promise<number> {
const linter = new CommitLinter()
const configuration = await Configuration.find(this.context.cwd, this.context.plugins)
const {
project: { workspaces },
} = await Project.find(configuration, this.context.cwd)

const workspaceNames = new Set(workspaces.map(({ manifest }) => manifest.name?.name ?? ''))
const scopes = new Set(workspaces.map(({ manifest }) => manifest.name?.scope ?? ''))

const linter = new CommitLinter({
scopes: Array.from(scopes),
workspaceNames: Array.from(workspaceNames),
})

const messages = await read({ edit: true })
const results = await Promise.all(messages.map(linter.lint))
const results = await Promise.all(messages.map((message) => linter.lint(message)))

const output = linter.format({ results })

Expand Down

0 comments on commit 7ee2b0a

Please sign in to comment.