Skip to content

Commit

Permalink
tests(devtools): dynamically fetch chromium version
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Mar 11, 2021
1 parent 863f12f commit d13bc75
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
20 changes: 12 additions & 8 deletions third-party/download-content-shell/download-content-shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ function main() {
fs.mkdirSync(CACHE_PATH, {recursive: true});
deleteOldContentShells();

findPreviousUploadedPosition(findMostRecentChromiumCommit())
findMostRecentChromiumCommit()
.then(findPreviousUploadedPosition)
.then(onUploadedCommitPosition)
.catch(onError);

Expand Down Expand Up @@ -64,13 +65,16 @@ function getPlatform() {
throw new Error(`Unrecognized platform detected: ${process.platform}`);
}

function findMostRecentChromiumCommit() {
// TODO: this code works only if there is a full chromium checkout present.
// const commitMessage = shell('git log --max-count=1 --grep="Cr-Commit-Position"').toString().trim();
// const commitPosition = commitMessage.match(/Cr-Commit-Position: refs\/heads\/master@\{#([0-9]+)\}/)[1];
// return commitPosition;
// TODO: make this dynamic.
return '822569';
async function findMostRecentChromiumCommit() {
// Fetch the most recent commit from the GitHub Chromium mirror.
const githubUrl = 'https://api.github.com/repos/chromium/chromium/commits/master';
// https://docs.github.com/en/rest/overview/resources-in-the-rest-api#user-agent-required
const options = {headers: {'User-Agent': 'lighthouse'}};
const latestCommit = await utils.fetch(githubUrl, options);
const commitMessage = JSON.parse(latestCommit).commit.message;
const commitPosition = /Cr-Commit-Position: refs\/heads\/master@\{#([0-9]+)\}/.exec(commitMessage)[1];

return commitPosition;
}

function deleteOldContentShells() {
Expand Down
12 changes: 8 additions & 4 deletions third-party/download-content-shell/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@ const parseURL = require('url').parse;
const shell = require('child_process').execSync;
const Stream = require('stream').Transform;

function fetch(url) {
/**
* @param {string} url
* @param {import('http').RequestOptions} [options]
*/
function fetch(url, options = {}) {
return new Promise(fetchPromise);

function fetchPromise(resolve, reject) {
let request;
const protocol = parseURL(url).protocol;
const handleResponse = getCallback.bind(null, resolve, reject);
if (protocol === 'https:') {
request = https.get(url, handleResponse);
request = https.get(url, options, handleResponse);
} else if (protocol === 'http:') {
request = http.get(url, handleResponse);
request = http.get(url, options, handleResponse);
} else {
reject(new Error(`Invalid protocol for url: ${url}`));
return;
Expand All @@ -31,7 +35,7 @@ function fetch(url) {

function getCallback(resolve, reject, response) {
if (response.statusCode !== 200) {
reject(new Error(`Request error: + ${response.statusCode}`));
reject(new Error(`Request error: ${response.statusCode}`));
return;
}
const body = new Stream();
Expand Down

0 comments on commit d13bc75

Please sign in to comment.