spectral icon indicating copy to clipboard operation
spectral copied to clipboard

Unable to use a spectral file located in Enterprise Github with a token parameter

Open jasonwilczak opened this issue 1 year ago • 4 comments

Describe the bug When trying to use a spectral lint file that requires a token param, Spectral throws an unexpected error

Error running Spectral!
Error #1: Unexpected token (Note that you need plugins to import files that are not JavaScript)
          at error            …hared/rollup.js:198  base = Object.assig…
          at error            …red/rollup.js:12555  return error(props);
          at tryParse         …red/rollup.js:12932  return this.error({ 
          at setSource        …red/rollup.js:12837  ast = this.tryParse…
          at addModuleSource  …red/rollup.js:22311  module.setSource(aw…

To Reproduce

  1. Create a GitHub repository and place a spectral file there, like this:
    • style.yaml
extends: spectral:oas
rules:
  oas3-api-costcenter:
    description: There must be x-cost-center.
    given: $
    severity: error
    then:
      field: x-cost-center
      function: truthy

  oas3-api-businessid:
    description: There must be x-business-id.
    given: $
    severity: error
    then:
      field: x-business-id
      function: truthy
  1. Go to the raw view of that spectral file and copy the url, it should have a token in it
  2. Create a local folder with a "src" folder and a basic api-schema.json file that has an oas schema
  3. Run this CLI command 'spectral lint "./src/api-schema.json" --ruleset {url with token from step 2} --verbose'
  4. See error
Error running Spectral!
Error #1: Unexpected token (Note that you need plugins to import files that are not JavaScript)
          at error            …hared/rollup.js:198  base = Object.assig…
          at error            …red/rollup.js:12555  return error(props);
          at tryParse         …red/rollup.js:12932  return this.error({ 
          at setSource        …red/rollup.js:12837  ast = this.tryParse…
          at addModuleSource  …red/rollup.js:22311  module.setSource(aw…

Expected behavior I would expect that Spectral would be able to ingest this file as a standard curl command shows the file as it should be. Additionally, if I pull the file down locally and put it in the repo, it runs fine.

Environment (remove any that are not applicable):

  • Library version: Spectral v6.5.1
  • OS: Windows 10
  • Browser: n/a

Additional context Add any other context about the problem here.

jasonwilczak avatar Sep 07 '22 14:09 jasonwilczak

I can confirm that currently the ruleset needs to end with \.(json|ya?ml)/ in order to be loaded properly. As a somewhat hacky workaround, one could do append &.yml to the end, like so https://raw.githubusercontent.com/<org>/<project-name>/<branch>/<file-path>?token=<token>&.yml

P0lip avatar Sep 07 '22 15:09 P0lip

I can confirm that currently the ruleset needs to end with \.(json|ya?ml)/ in order to be loaded properly. As a somewhat hacky workaround, one could do append &.yml to the end, like so https://raw.githubusercontent.com/<org>/<project-name>/<branch>/<file-path>?token=<token>&.yml

I'll have to check this out in a bit. I was able to get it to work by doing:

https://{username}:{dev-token}@{enterprisegithub-domain}/raw/{org}/{project}/{filename}.yaml

This works for now, but I wonder if it would make sense to to look at like location.pathname or something similar so that the query stuff isn't considered?

jasonwilczak avatar Sep 08 '22 19:09 jasonwilczak

This works for now, but I wonder if it would make sense to to look at like location.pathname or something similar so that the query stuff isn't considered?

It totally would. I'll keep that ticket open. I just wanted to offer a quick workaround to unblock you till we sort this case out :)

P0lip avatar Sep 08 '22 20:09 P0lip

https://github.com/stoplightio/spectral/pull/2272 it is

P0lip avatar Sep 08 '22 21:09 P0lip

:tada: This issue has been resolved in version @stoplight/spectral-cli-v6.6.0 :tada:

The release is available on npm package (@latest dist-tag)

Your semantic-release bot :package::rocket:

stoplight-bot avatar Oct 24 '22 20:10 stoplight-bot