import SynkVulnerabilityBase from './snyk-vulnerability-base.js'

export default class SnykVulnerabilityGitHub extends SynkVulnerabilityBase {
  static route = {
    base: 'snyk/vulnerabilities/github',
    pattern: ':user/:repo/:manifestFilePath*',
  }

  static examples = [
    {
      title: 'Snyk Vulnerabilities for GitHub Repo',
      pattern: ':user/:repo',
      namedParams: {
        user: 'badges',
        repo: 'shields',
      },
      staticPreview: this.render({ vulnerabilities: '0' }),
    },
    {
      title: 'Snyk Vulnerabilities for GitHub Repo (Specific Manifest)',
      pattern: ':user/:repo/:manifestFilePath',
      namedParams: {
        user: 'badges',
        repo: 'shields',
        manifestFilePath: 'badge-maker/package.json',
      },
      staticPreview: this.render({ vulnerabilities: '0' }),
      documentation: `
        <p>
          Provide the path to your target manifest file relative to the base of your repository.
          Snyk does not support using a specific branch for this, so do not include "blob" nor a branch name.
        </p>
        `,
    },
  ]

  async handle({ user, repo, manifestFilePath }) {
    const url = `https://snyk.io/test/github/${user}/${repo}/badge.svg`
    const searchParams = { targetFile: manifestFilePath }
    const { vulnerabilities } = await this.fetch({
      url,
      searchParams,
      errorMessages: {
        404: 'repo or manifest not found',
      },
    })
    return this.constructor.render({ vulnerabilities })
  }
}
