Skip to content
Snippets Groups Projects
Select Git revision
  • f334c568a5039f88f72fb99ee25410a19ffbe501
  • master default
2 results

index.js

Blame
  • index.js 4.99 KiB
    import ghGot from '../../platform/github/gh-got-wrapper';
    
    const { logger } = require('../../logger');
    const got = require('../../util/got');
    
    module.exports = {
      getPreset,
      getDigest,
      getPkgReleases,
    };
    
    async function fetchJSONFile(repo, fileName) {
      const url = `https://api.github.com/repos/${repo}/contents/${fileName}`;
      const opts = {
        headers: {
          accept: global.appMode
            ? 'application/vnd.github.machine-man-preview+json'
            : 'application/vnd.github.v3+json',
        },
        json: true,
        hostType: 'github',
      };
      let res;
      try {
        res = await got(url, opts);
      } catch (err) {
        logger.debug(
          { statusCode: err.statusCodef },
          `Failed to retrieve ${fileName} from repo`
        );
        throw new Error('dep not found');
      }
      try {
        const content = Buffer.from(res.body.content, 'base64').toString();
        const parsed = JSON.parse(content);
        return parsed;
      } catch (err) {
        throw new Error('invalid preset JSON');
      }
    }
    
    async function getPreset(pkgName, presetName = 'default') {
      if (presetName === 'default') {
        try {
          const defaultJson = await fetchJSONFile(pkgName, 'default.json');
          return defaultJson;
        } catch (err) {
          if (err.message === 'dep not found') {
            logger.info('default.json preset not found - trying renovate.json');
            return fetchJSONFile(pkgName, 'renovate.json');
          }
          throw err;
        }
      }
      return fetchJSONFile(pkgName, `${presetName}.json`);
    }
    
    const cacheNamespace = 'datasource-github';
    function getCacheKey(repo, type) {
      return `${repo}:${type}`;
    }
    
    /*
     * github.getDigest
     *
     * The `newValue` supplied here should be a valid tag for the docker image.
     *
     * This function will simply return the latest commit hash for the configured repository.
     */