<commit_before>/**
 * Copyright 2018 The AMP HTML Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

const argv = require('minimist')(process.argv.slice(2));
const BBPromise = require('bluebird');
const brotliSize = require('brotli-size');
const colors = require('ansi-colors');
const fs = require('fs-extra');
const log = require('fancy-log');
const Octokit = require('@octokit/rest');
const path = require('path');
const requestPost = BBPromise.promisify(require('request').post);
const url = require('url');
const {
  isTravisPullRequestBuild,
  isTravisPushBuild,
  travisRepoSlug,
} = require('../travis');
const {getStdout} = require('../exec');
const {gitCommitHash, gitTravisMasterBaseline, shortSha} = require('../git');
const {VERSION: internalRuntimeVersion} = require('../internal-version');

const runtimeFile = './dist/v0.js';

const buildArtifactsRepoOptions = {
  owner: 'ampproject',
  repo: 'amphtml-build-artifacts',
};
const expectedGitHubRepoSlug = 'ampproject/amphtml';
const bundleSizeAppBaseUrl = 'https://amp-bundle-size-bot.appspot.com/v0/';

const {red, cyan, yellow} = colors;

/**
 * Get the gzipped bundle size of the current build.
 *
 * @return {string} the bundle size in KB rounded to 2 decimal points.
 */
function getGzippedBundleSize() {
  const cmd = `npx bundlesize -f "${runtimeFile}"`;
  log('Running', cyan(cmd) + '...');
  const output = getStdout(cmd).trim();

  const bundleSizeOutputMatches = output.match(/PASS .*: (\d+.?\d*KB) .*/);
  if (bundleSizeOutputMatches) {
    const bundleSize = parseFloat(bundleSizeOutputMatches[1]);
    log('Bundle size', cyan('(gzipped)'), 'is', cyan(`${bundleSize}KB`));
    return bundleSize;
  }
  throw Error('could not infer bundle size from output.');
}

/**
 * Get the brotli bundle size of the current build.
 *
 * @return {string} the bundle size in KB rounded to 2 decimal points.
 */
function getBrotliBundleSize() {
  const bundleSize = parseFloat(
    (brotliSize.fileSync(runtimeFile) / 1024).toFixed(2)
  );
  log('Bundle size', cyan('(brotli)'), 'is', cyan(`${bundleSize}KB`));
  return bundleSize;
}

/**
 * Store the bundle size of a commit hash in the build artifacts storage
 * repository to the passed value.
 *
 * @return {!Promise}
 */
async function storeBundleSize() {
  if (!isTravisPushBuild()) {
    log(
      yellow('Skipping'),
      cyan('--on_push_build') + ':',
      'this action can only be performed on `push` builds on Travis'
    );<commit_msg>🏗 Normalize RTV number before determining Brotli compressed size (#24472)<commit_after>/**
 * Copyright 2018 The AMP HTML Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
'use strict';

const argv = require('minimist')(process.argv.slice(2));
const BBPromise = require('bluebird');
const brotliSize = require('brotli-size');
const colors = require('ansi-colors');
const fs = require('fs');
const log = require('fancy-log');
const Octokit = require('@octokit/rest');
const path = require('path');
const requestPost = BBPromise.promisify(require('request').post);
const url = require('url');
const {
  isTravisPullRequestBuild,
  isTravisPushBuild,
  travisRepoSlug,
} = require('../travis');
const {getStdout} = require('../exec');
const {gitCommitHash, gitTravisMasterBaseline, shortSha} = require('../git');
const {VERSION: internalRuntimeVersion} = require('../internal-version');

const runtimeFile = './dist/v0.js';
const normalizedRtvNumber = '1234567890123';

const buildArtifactsRepoOptions = {
  owner: 'ampproject',
  repo: 'amphtml-build-artifacts',
};
const expectedGitHubRepoSlug = 'ampproject/amphtml';
const bundleSizeAppBaseUrl = 'https://amp-bundle-size-bot.appspot.com/v0/';

const {red, cyan, yellow} = colors;

/**
 * Get the gzipped bundle size of the current build.
 *
 * @return {string} the bundle size in KB rounded to 2 decimal points.
 */
function getGzippedBundleSize() {
  const cmd = `npx bundlesize -f "${runtimeFile}"`;
  log('Running', cyan(cmd) + '...');
  const output = getStdout(cmd).trim();

  const bundleSizeOutputMatches = output.match(/PASS .*: (\d+.?\d*KB) .*/);
  if (bundleSizeOutputMatches) {
    const bundleSize = parseFloat(bundleSizeOutputMatches[1]);
    log('Bundle size', cyan('(gzipped)'), 'is', cyan(`${bundleSize}KB`));
    return bundleSize;
  }
  throw Error('could not infer bundle size from output.');
}

/**
 * Get the brotli bundle size of the current build.
 *
 * @return {string} the bundle size in KB rounded to 2 decimal points.
 */
function getBrotliBundleSize() {
  // Brotli compressed size fluctuates because of changes in the RTV number, so
  // normalize this across pull requests by replacing that RTV with a constant.
  const normalizedFileContents = fs
    .readFileSync(runtimeFile, 'utf8')
    .replace(new RegExp(internalRuntimeVersion, 'g'), normalizedRtvNumber);
  const bundleSize = parseFloat(
    (brotliSize.sync(normalizedFileContents) / 1024).toFixed(2)
  );
  log('Bundle size', cyan('(brotli)'), 'is', cyan(`${bundleSize}KB`));
  return bundleSize;
}

/**
 * Store the bundle size of a commit hash in the build artifacts storage
 * repository to the passed value.
 *
 * @return {!Promise}
 */
async function storeBundleSize() {
  if (!isTravisPushBuild()) {
    log(
      yellow('Skipping'),
      cyan('--on_push_build') + ':',
      'this action can only be performed on `push` builds on Travis'
    );
{'fraction_non_alphanumeric': 0.13880712625871416, 'fraction_numerical': 0.008830364058869094, 'mean_word_length': 4.39799331103679, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5916249', 'n_tokens_mistral': 2009, 'n_tokens_neox': 1952, 'n_words': 621}