Skip to content
Snippets Groups Projects
Select Git revision
  • dff1fb137b30657915d922f60e0837efbc4bfd8f
  • main default protected
  • renovate/main-ghcr.io-renovatebot-base-image-10.x
  • renovate/main-ghcr.io-containerbase-devcontainer-13.x
  • next
  • revert-31645-feat/rename-gradle-wrapper-validation-action
  • renovate/main-redis-5.x
  • fix/36615b-branch-reuse-no-cache
  • chore/punycode
  • fix/36615-branch-reuse-bug
  • refactor/pin-new-value
  • feat/36219--git-x509-signing
  • feat/structured-logger
  • hotfix/39.264.1
  • feat/skip-dangling
  • gh-readonly-queue/next/pr-36034-7a061c4ca1024a19e2c295d773d9642625d1c2be
  • hotfix/39.238.3
  • refactor/gitlab-auto-approve
  • feat/template-strings
  • gh-readonly-queue/next/pr-35654-137d934242c784e0c45d4b957362214f0eade1d7
  • fix/32307-global-extends-merging
  • 41.26.1
  • 41.26.0
  • 41.25.1
  • 41.25.0
  • 41.24.0
  • 41.23.5
  • 41.23.4
  • 41.23.3
  • 41.23.2
  • 41.23.1
  • 41.23.0
  • 41.22.0
  • 41.21.4
  • 41.21.3
  • 41.21.2
  • 41.21.1
  • 41.21.0
  • 41.20.2
  • 41.20.1
  • 41.20.0
41 results

gl-got-wrapper.js

Blame
  • gl-got-wrapper.js 1.78 KiB
    const URL = require('url');
    const glGot = require('gl-got');
    const delay = require('delay');
    
    const parseLinkHeader = require('parse-link-header');
    const hostRules = require('../../util/host-rules');
    
    let cache = {};
    
    async function get(path, options, retries = 5) {
      const { host } = URL.parse(path);
      const opts = {
        ...hostRules.find({ platform: 'gitlab', host }),
        ...options,
      };
      opts.baseUrl = opts.endpoint;
      delete opts.endpoint;
      const method = opts.method || 'get';
      const useCache = opts.useCache || true;
      if (method === 'get' && useCache && cache[path]) {
        logger.trace({ path }, 'Returning cached result');
        return cache[path];
      }
      logger.debug({ path }, method.toUpperCase());
      try {
        const res = await glGot(path, opts);
        if (opts.paginate) {
          // Check if result is paginated
          try {
            const linkHeader = parseLinkHeader(res.headers.link);
            if (linkHeader && linkHeader.next) {
              res.body = res.body.concat(
                (await get(linkHeader.next.url, opts, retries)).body
              );
            }
          } catch (err) {
            logger.warn({ err }, 'Pagination error');
          }
        }
        if (method === 'get' && path.startsWith('projects/')) {
          cache[path] = res;
        }
        return res;
      } catch (err) /* istanbul ignore next */ {
        if (retries < 1) {
          throw err;
        }
        if (err.statusCode === 429) {
          logger.info(`Sleeping 1 minute before retrying 429`);
          await delay(60000);
          return get(path, opts, retries - 1);
        }
        throw err;
      }
    }
    
    const helpers = ['get', 'post', 'put', 'patch', 'head', 'delete'];
    
    for (const x of helpers) {
      get[x] = (url, opts) =>
        get(url, Object.assign({}, opts, { method: x.toUpperCase() }));
    }
    
    get.reset = function reset() {
      cache = null;
      cache = {};
    };
    
    module.exports = get;