From d13bc755d0893d3877f09327e44a70e8cca798ea Mon Sep 17 00:00:00 2001 From: Brendan Kenny Date: Wed, 10 Mar 2021 20:00:52 -0600 Subject: [PATCH] tests(devtools): dynamically fetch chromium version --- .../download-content-shell.js | 20 +++++++++++-------- third-party/download-content-shell/utils.js | 12 +++++++---- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/third-party/download-content-shell/download-content-shell.js b/third-party/download-content-shell/download-content-shell.js index 5a1fb5284063..b01bfa9ec099 100644 --- a/third-party/download-content-shell/download-content-shell.js +++ b/third-party/download-content-shell/download-content-shell.js @@ -23,7 +23,8 @@ function main() { fs.mkdirSync(CACHE_PATH, {recursive: true}); deleteOldContentShells(); - findPreviousUploadedPosition(findMostRecentChromiumCommit()) + findMostRecentChromiumCommit() + .then(findPreviousUploadedPosition) .then(onUploadedCommitPosition) .catch(onError); @@ -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() { diff --git a/third-party/download-content-shell/utils.js b/third-party/download-content-shell/utils.js index 1956851d3cb6..ca6058d0d1e8 100644 --- a/third-party/download-content-shell/utils.js +++ b/third-party/download-content-shell/utils.js @@ -11,7 +11,11 @@ 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) { @@ -19,9 +23,9 @@ function fetch(url) { 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; @@ -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();