<commit_before>import path from 'path';

import logging from '../logging';
const logger = logging.getLogger('express');

import {
  internalFindSnap,
  internalGetSnapBuilds
} from './launchpad';
import { getGitHubRepoUrl } from '../../common/helpers/github-url';
import { snapBuildFromAPI } from '../../common/helpers/snap-builds';

const BADGES_PATH = path.join(__dirname, '../../common/images/badges');

export const badge = async (req, res) => {

  const repoUrl = getGitHubRepoUrl(req.params.owner, req.params.name);

  try {
    const snap = await internalFindSnap(repoUrl);
    const builds = await internalGetSnapBuilds(snap);

    let badgeName = 'never_built';

    if (builds.entries && builds.entries.length) {
      const latestBuild = snapBuildFromAPI(builds.entries[0]);

      if (latestBuild.badge) {
        badgeName = latestBuild.badge;
      }
    }
    return res.sendFile(path.join(BADGES_PATH, `${badgeName}.svg`));
  } catch (err) {
    logger.error(`Error generating badge for repo ${repoUrl}`, err);
    res.status(404).send('Not found');
  }

};<commit_msg>Update badge cache headers as per GH expects it (#1119)<commit_after>import path from 'path';

import logging from '../logging';
const logger = logging.getLogger('express');

import {
  internalFindSnap,
  internalGetSnapBuilds
} from './launchpad';
import { getGitHubRepoUrl } from '../../common/helpers/github-url';
import { snapBuildFromAPI } from '../../common/helpers/snap-builds';

const BADGES_PATH = path.join(__dirname, '../../common/images/badges');

export const badge = async (req, res) => {

  const repoUrl = getGitHubRepoUrl(req.params.owner, req.params.name);

  try {
    const snap = await internalFindSnap(repoUrl);
    const builds = await internalGetSnapBuilds(snap);

    let badgeName = 'never_built';

    if (builds.entries && builds.entries.length) {
      const latestBuild = snapBuildFromAPI(builds.entries[0]);

      if (latestBuild.badge) {
        badgeName = latestBuild.badge;
      }
    }

    res.setHeader('Cache-Control', 'no-cache');
    return res.sendFile(path.join(BADGES_PATH, `${badgeName}.svg`));
  } catch (err) {
    logger.error(`Error generating badge for repo ${repoUrl}`, err);
    res.status(404).send('Not found');
  }

};
{'fraction_non_alphanumeric': 0.15126050420168066, 'fraction_numerical': 0.005307386112339673, 'mean_word_length': 3.7322175732217575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3075316', 'n_tokens_mistral': 735, 'n_tokens_neox': 715, 'n_words': 159}