<commit_before>import GitHub from 'github-api';
import trim from 'lodash/trim';
import isEmpty from 'lodash/isEmpty';
import promiseRetry from 'promise-retry';
const anonymousGithub = new GitHub({});

export function EmptyGistError(message) {
  this.name = 'EmptyGistError';
  this.message = message;
  this.stack = new Error().stack;
}
EmptyGistError.prototype = Object.create(Error.prototype);

function createGistFromProject(project) {
  const files = {};
  if (trim(project.sources.html)) {
    files['index.html'] = {
      content: project.sources.html,
      language: 'HTML',
    };
  }
  if (trim(project.sources.css)) {
    files['styles.css'] = {
      content: project.sources.css,
      language: 'CSS',
    };
  }
  if (trim(project.sources.javascript)) {
    files['script.js'] = {
      content: project.sources.javascript,
      language: 'JavaScript',
    };
  }
  if (project.enabledLibraries.length) {
    files['popcode.json'] = {
      content: createPopcodeJson(project),
      language: 'JSON',
    };
  }

  return {
    description: 'Exported from Popcode.',
    'public': true,
    files,
  };
}

function clientForUser(user) {
  if (user.authenticated && user.provider === 'github') {
    return new GitHub({
      token: user.info.accessToken,
      auth: 'oauth',
    });
  }

  return anonymousGithub;
}

function canUpdateGist(user) {
  return user.authenticated && user.provider === 'github';
}

function updateGistWithImportUrl(github, gistData) {
  const gist = github.getGist(gistData.id);
  const uri = document.createElement('a');
  uri.setAttribute('href', '/');
  uri.search = `gist=${gistData.id}`;

  return gist.update({
    description: `${gistData.description} Click to import: ${uri.href}`,
  }).then((response) => response.data);
}

function createPopcodeJson(project) {
  const json = {
    enabledLibraries: project.enabledLibraries,
  };
  return JSON.stringify(json);
}

const Gists = {
  createFromProject(project, user) {
    const github = clientForUser(user);

    const gist = createGistFromProject(project);
    if (isEmpty(gist.files)) {
      return Promise.reject(new EmptyGistError());
    }

    return github.getGist().create(gist).
      then((response) => {
        const gistData = response.data;
        if (canUpdateGist(user)) {
          return updateGistWithImportUrl(github, gistData);
        }
        return gistData;
      }).then((gistData) => promiseRetry(
        (retry) => this.loadFromId(gistData.id, user).catch(retry),
        {
          retries: 5,
          factor: 2,
          minTimeout: 1000,
          maxTimeout: 10000,
        }
      ));
  },

  loadFromId(gistId, user) {
    const github = clientForUser(user);
    const gist = github.getGist(gistId);
    return gist.read().then((response) => response.data);
  },
};

export default Gists;<commit_msg>Retry loading gist if request fails<commit_after>import GitHub from 'github-api';
import trim from 'lodash/trim';
import isEmpty from 'lodash/isEmpty';
import promiseRetry from 'promise-retry';
const anonymousGithub = new GitHub({});

export function EmptyGistError(message) {
  this.name = 'EmptyGistError';
  this.message = message;
  this.stack = new Error().stack;
}
EmptyGistError.prototype = Object.create(Error.prototype);

function createGistFromProject(project) {
  const files = {};
  if (trim(project.sources.html)) {
    files['index.html'] = {
      content: project.sources.html,
      language: 'HTML',
    };
  }
  if (trim(project.sources.css)) {
    files['styles.css'] = {
      content: project.sources.css,
      language: 'CSS',
    };
  }
  if (trim(project.sources.javascript)) {
    files['script.js'] = {
      content: project.sources.javascript,
      language: 'JavaScript',
    };
  }
  if (project.enabledLibraries.length) {
    files['popcode.json'] = {
      content: createPopcodeJson(project),
      language: 'JSON',
    };
  }

  return {
    description: 'Exported from Popcode.',
    'public': true,
    files,
  };
}

function clientForUser(user) {
  if (user.authenticated && user.provider === 'github') {
    return new GitHub({
      token: user.info.accessToken,
      auth: 'oauth',
    });
  }

  return anonymousGithub;
}

function canUpdateGist(user) {
  return user.authenticated && user.provider === 'github';
}

function updateGistWithImportUrl(github, gistData) {
  const gist = github.getGist(gistData.id);
  const uri = document.createElement('a');
  uri.setAttribute('href', '/');
  uri.search = `gist=${gistData.id}`;

  return gist.update({
    description: `${gistData.description} Click to import: ${uri.href}`,
  }).then((response) => response.data);
}

function createPopcodeJson(project) {
  const json = {
    enabledLibraries: project.enabledLibraries,
  };
  return JSON.stringify(json);
}

const Gists = {
  createFromProject(project, user) {
    const github = clientForUser(user);

    const gist = createGistFromProject(project);
    if (isEmpty(gist.files)) {
      return Promise.reject(new EmptyGistError());
    }

    return github.getGist().create(gist).
      then((response) => {
        const gistData = response.data;
        if (canUpdateGist(user)) {
          return updateGistWithImportUrl(github, gistData);
        }
        return gistData;
      }).then((gistData) => promiseRetry(
        (retry) => this.loadFromId(gistData.id, user).catch(retry),
        {
          retries: 5,
          factor: 2,
          minTimeout: 1000,
          maxTimeout: 10000,
        }
      ));
  },

  loadFromId(gistId, user) {
    const github = clientForUser(user);
    const gist = github.getGist(gistId);
    return promiseRetry((retry) => gist.read().catch(retry), {retries: 3}).
      then((response) => response.data);
  },
};

export default Gists;
{'fraction_non_alphanumeric': 0.14587298840629867, 'fraction_numerical': 0.0039799273230662745, 'mean_word_length': 3.2005813953488373, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5964801', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1833, 'n_words': 364}