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(rules): add async promise based rules methods into lint #697

Closed
wants to merge 0 commits into from

Conversation

blackswanny
Copy link

Description

Extra feature should allow rule method to be asynchronous in case this method needs to do some asynchronous operations, calls, etc. For asynchronous Promise pattern is used.

Motivation and Context

For more functionality and flexibility commit lint started to support plugins. However, those plugins, or even the core rules with new feature can add some extra checks if they require tests to be requested from some remote source or any asynchronous source.

Usage examples

// commitlint.config.js
const axios = require('axios');
module.exports = {
  rules: {
    'commit-msg': [2, 'always', {
      checkCommit: (match, [valid, message]) => {
        const jiraServer = 'https://jira.attlassian.com/rest/api/latest/issue/';
        const issueUrl = jiraServer + match[1];
        return new Promise(resolve => {
          axios.get(issueUrl).then(() => resolve([valid, message])).catch(({
            response: {
              status,
              data: {
                errorMessages = []
              } = {}
            } = {}
          }) => {
            if (status === 404 && errorMessages.indexOf('Issue Does Not Exist') >= 0) {
              valid = false;
              message = `Seems like issue ${issueUrl} doesn't exist or URL config is wrong.`;
            }
            resolve([valid, message]);
          })
        });
      },
      pattern: /.*\[([A-Z]{1,10}-\d{1,})\]/igm
    }]
  },
  plugins: ['message-regex']
};

//commitlint-plugin-message-regex/index.js
module.exports = {
  rules: {
    'commit-msg': (parsed, when, config) => {
      let valid = true, message = 'All is good';
      if (config.pattern instanceof RegExp) {
        if (when === 'always') {
          const match = config.pattern.exec(parsed.raw);
          if (!match || (match.length < 1)) {
            valid = false;
          } else {
            return config.checkCommit(match, [valid, message]);
          }
        }
      } else {
        valid = false;
        message = `Rule config should contain object { pattern: RegExp, message: 'in case if RegExp doesn't match' }`;
      }
      return Promise.resolve([valid, message]);
    }
  }
};
echo "Some [JIRA-000000] " | commitlint # fails
⧗   input: Some [JIRA-000000]
✖   Seems like issue https://jira.attlassian.com/rest/api/latest/issue/JIRA-000000 doesn't exist or URL config is wrong.
[commit-msg]

✖   found 1 problems, 0 warnings
ⓘ   Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint

echo "Some [JIRA-12345] " | commitlint # passes

How Has This Been Tested?

The changes were tested by applying in real project of the build code produced by npm run build. Also additional unit tests are added to lint tests

Types of changes

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@markcellus
Copy link

markcellus commented Nov 25, 2019

I was very surprised to come to this repo to find that the rule methods can only be synchronous. Any idea when this can be merged? Or is there a workaround for now?

@markcellus
Copy link

I see this was closed but I can't seem to find where this feature was added. Can anyone provide any insight? Thanks

@armano2
Copy link
Contributor

armano2 commented Feb 9, 2020

#976

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants