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

allow custom rules #104

Closed
cdbkr opened this issue Feb 13, 2024 · 2 comments
Closed

allow custom rules #104

cdbkr opened this issue Feb 13, 2024 · 2 comments

Comments

@cdbkr
Copy link

cdbkr commented Feb 13, 2024

Is your feature request related to a problem? Please describe.
At the moment the tool supports only some pre-defined rules defined here https://github.com/IBM/tekton-lint/blob/main/src/rule-loader.ts
It would be great if users could provide (and leave them to maintain) their custom rule implementations and specify how to treat the findings (e.g. error, warning, etc..).

An example would be like the following:

const forbidPipelinesWithoutRequiredAnnotation = (docs, tekton, report) => {
    for (const pipeline of Object.values(tekton.pipelines)) {

        const pipelineAnnotations = Object.keys(pipeline.metadata.annotations);

        if (!pipelineAnnotations.includes('my/required-annotation')) {
            report(
                `Pipeline '${pipeline.metadata.name}' does not specify the 'my/required-annotation' annotation`,
            );
        }
    }
};

module.exports = {
    rules: {
        'no-required-annotations': 'error'
    },
    customRules: {
        'no-required-annotations': forbidPipelinesWithoutRequiredAnnotation
    }
}

Describe the solution you'd like
Support (on top of the yaml configuration) a JS (or TS) configuration file as well, e.g. .tektonlintrc.js where it is possible to define a set of custom rules or provide a specific rule as a function (instead of providing a string 'error' or 'warning', and leave the implementation to handle it).

Describe alternatives you've considered
At the moment there is no way to provide custom rules since they are specified in the mentioned file above

Additional context
Add JS File loader in the ./src/config.ts main constructor. An example below:

if (fs.lstatSync(user_tektonlintrc).isDirectory()) {
    user_tektonlintrc = path.join(user_tektonlintrc, '.tektonlintrc.yaml');
}

if (fs.lstatSync(user_tektonlintjs).isDirectory()) {
    user_tektonlintjs = path.join(user_tektonlintjs, '.tektonlintrc.js');
}

let customRcConfig = {
    ...defaultConfig
};

if (fs.existsSync(user_tektonlintrc)) {
    const customRcFile = fs.readFileSync(user_tektonlintrc, 'utf8');
    customRcConfig = yaml.parse(customRcFile);

    logger.info('Using .tektonlintrc.yaml at %s', user_tektonlintrc);
    logger.info('customConfig %o', customRcConfig);
} else if (fs.existsSync(user_tektonlintjs)) {
    customRcConfig = require(user_tektonlintjs);

    logger.info('Using .tektonlintrc.js at %s', user_tektonlintrc);
    logger.info('customConfig %o', customRcConfig);
} else {
    logger.warn('Unable to find configuration - using defaults');
}

this._rulesConfig = { ...customRcConfig };
this._rulesConfig.rules = { ...customRcConfig.rules };
this._rulesConfig.customRules = { ...customRcConfig.customRules };
this._globs = [...cliConfig['_'], ...(customRcConfig.globs ? customRcConfig.globs : ['_'])];
@mbwhite
Copy link
Collaborator

mbwhite commented Mar 1, 2024

Work is underway in PR #106

@mbwhite
Copy link
Collaborator

mbwhite commented Mar 8, 2024

I've merged the PR, and updated the documentation; there is a sample custom rule in the repo as well.

The implementation is very similar to that originally suggested.

@mbwhite mbwhite closed this as completed Mar 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants