JulienKode / pull-request-name-linter-action

⚡️ Lint pull request name with commit lint. This is useful if you squash merge your pull requests ⚡️

Home Page:https://github.com/marketplace/actions/pull-request-linter-action

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeError: normalizedName.indexOf is not a function

dominikfoldi opened this issue · comments

When the action runs it throws the following error:

Error: TypeError: normalizedName.indexOf is not a function
Error: normalizedName.indexOf is not a function

image

It seems that it is coming from the bundled eslint. Why devDependencies are bundled into the action's bundle in the first place? Isn't it possible to leave the devDependencies out with ncc?

I would really like to use your action but I don't have any experience with ncc so I cannot provide a PR unfortunately.

Hi @dominikfoldi

To help you and debug on this can you show me your:

  • package.json
  • commitlint.config.js
  • your github action

Then I'll have informations to help you and dive on this

Have a nice day

Sure @JulienKode!

package.json

{
  "name": "my-project",
  "devDependencies": {
    "@commitlint/config-angular": "^11.0.0",
    "lerna": "^3.20.0",
  }
}

commitlint.config.js

const Project = require("@lerna/project");
const fs = require("fs");

const POC_FOLDER_PATH = "poc";

/**
 * Enum for scope requirements.
 */
const scopeRequirements = {
  REQUIRED: "required",
  OPTIONAL: "optional",
  FORBIDDEN: "forbidden"
};

/**
 * Valid types of commits with their scope requirements.
 */
const COMMIT_TYPES = {
  build: {
    name: "build",
    scope: scopeRequirements.OPTIONAL
  },
  release: {
    name: "release",
    scope: scopeRequirements.FORBIDDEN
  },
  docs: {
    name: "docs",
    scope: scopeRequirements.OPTIONAL
  },
  feat: {
    name: "feat",
    scope: scopeRequirements.REQUIRED
  },
  fix: {
    name: "fix",
    scope: scopeRequirements.REQUIRED
  },
  perf: {
    name: "perf",
    scope: scopeRequirements.REQUIRED
  },
  refactor: {
    name: "refactor",
    scope: scopeRequirements.REQUIRED
  },
  style: {
    name: "style",
    scope: scopeRequirements.OPTIONAL
  },
  test: {
    name: "test",
    scope: scopeRequirements.OPTIONAL
  },
  prot: {
    name: "prot",
    scope: scopeRequirements.REQUIRED
  },
  revert: {
    name: "revert",
    scope: scopeRequirements.FORBIDDEN
  },
};

/**
 * Our custom scopes
 */
const CUSTOM_SCOPES = ["packaging", "dev-docs"];

/**
 * Array of valid types.
 */
const types = Object.keys(COMMIT_TYPES);

/**
 * Construct the scopes array from the custom scopes, lerna package names and POC folder names.
 */
async function getScopes() {
  const lernaPackages = (await new Project().getPackages())
    .map(package => package.name)
    .map((name) => (name.charAt(0) === '@' ? name.split('/')[1] : name));
  const customScopes = CUSTOM_SCOPES;
  const pocs = fs.readdirSync(POC_FOLDER_PATH, { withFileTypes: true })
    .filter(file => file.isDirectory()).map(directory => directory.name);

  return [].concat(lernaPackages, customScopes, pocs);
}

/**
 * This rule checks if the scopes were used correctly.
 * @param type type of the commit 
 * @param scope optional scope of the commit message 
 */
function scopeRequirementRule({ type, scope }) {
  const scopeRequirement = COMMIT_TYPES[type].scope;

  if (scopeRequirement === scopeRequirements.FORBIDDEN && scope) {
    return [false, `scopes are forbidden for commits with type '${type}', but a scope of '${scope}' was provided`];
  }

  if (scopeRequirement === scopeRequirements.REQUIRED && !scope) {
    return [false, `scopes are required for commits with type '${type}', but no scope was provided`];
  }

  return [true];
}

module.exports = {
  extends: ["@commitlint/config-angular"],
  rules: {
    'scope-requirement-rule': [2, 'always'],
    'type-enum': [2, 'always', types],
    'scope-enum': getScopes().then(scopes => [2, 'always', scopes])
  },
  plugins: [
    {
      rules: {
        'scope-requirement-rule': scopeRequirementRule
      }
    }
  ]
};

lint-pr.yml

name: Lint PRs

on:
  pull_request:
    types: ['opened', 'edited', 'reopened', 'synchronize']

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Setup Node.js
        uses: actions/setup-node@v1
        with:
          node-version: 10.x

      - name: Install npm dependencies
        run: npm ci

      - name: Lint the PR title to match our convention
        uses: JulienKode/pull-request-name-linter-action@v0.1.2

I left out the private/not relevant information.

Thank you!

@JulienKode I forked your example repository and configured it like our project is configured. The job is failing there too, see: https://github.com/dominikfoldi/pull-request-name-linter-action-example/runs/1198471507?check_suite_focus=true

It does not work because this plugin is currently using @commitlint/lint v8, and it now needs to be updated to the version 11

While updating to v11 I face some issue that seems coming inside: https://github.com/davidtheclark/cosmiconfig

It is not possible to load the commitlint.config file

We need to dive more on this, to find out what is the issue and update this plugin to the v11

(we should version this plugin with the same version of commit lint may be ? )

Maybe we can version the plugin with the versions of commitlint but I have a feeling that it doesn't worth the effort. I think that first we should try to move to the latest commitlint version and if somebody complains that it is creating an issue for them, then consider the multiple version support.

You're right, I think the main priority is to update to the latest version of commitlint then having a versioning strategy

@JulienKode can I help you somehow with this?

I've made a minimal reproduction for this bug on the following branch:

When you go on it you can do:

  • yarn build:pack
  • node lib/main.js (it will work)
  • node dist/index.js (it will not work)

When we are using it with babel it work but when we pack it with ncc we have a issue inside cosmiconfig, the issue looks like this: cosmiconfig/cosmiconfig#235

Maybe we can try to investigate on this as now we have a reproductible issue

The difference with lib/main.js and dist/index.js is that in the dist we pack it with the node_modules like here: https://github.com/actions/typescript-action/blob/main/package.json

Maybe we should throw out ncc and use Webpack, Rollup or other packaging solution. What do you think?

The issue seems related to: https://github.com/sindresorhus/import-fresh, this is the same issue as sindresorhus/import-fresh#18 that seems to block the update to commitlint v11, I do not think if changing to webpack or rollup can change this issue as we will have only one bundle and the error that we have happens when there is no parent module. But we can try to move to webpack too if you want, or rollup

sindresorhus/import-fresh#19 can fix the issue I think

I think this is the last blocker before upgrading this action to v11 of commitlint

Let me know if you still have the issue or not

We can close this issue if you still have it after the update @dominikfoldi, feel free to re-open this issue

Just tested it. It is working like a charm! Thank you @JulienKode! 🌮

Perfect ! Glad that it work for you, thanks for raising this issue