From aabcd25aaa3ad2ed1e25e159dad38a846d523d91 Mon Sep 17 00:00:00 2001 From: justinwoo Date: Thu, 3 Aug 2023 13:07:15 +0300 Subject: [PATCH 1/5] get git sha on install --- report.js | 34 ++++++++++++++++++++++++++++++++++ test/report.test.js | 10 ++++++++++ 2 files changed, 44 insertions(+) diff --git a/report.js b/report.js index 42a3d76..4ee5864 100644 --- a/report.js +++ b/report.js @@ -193,6 +193,22 @@ function processDependencyTreeOutput (resolve, reject) { } } +function processGitRevParseOutput (resolve, reject) { + return function (error, stdout, stderr) { + if (error && !stdout) { + return reject(new Error(`Scarf received an error from git rev-parse: ${error} | ${stderr}`)) + } + + const output = String(stdout).trim() + + if (output.length > 0) { + return resolve(output) + } else { + return reject(new Error('Scarf did not receive usable output from git rev-parse')) + } + } +} + // packageJSONOverride: a test convenience to set a packageJSON explicitly. // Leave empty to use the actual root package.json. async function getDependencyInfo (packageJSONOverride) { @@ -226,6 +242,18 @@ async function getDependencyInfo (packageJSONOverride) { }) } +async function getGitSha () { + const promise = new Promise((resolve, reject) => { + exec(`cd ${rootPath} && git rev-parse HEAD`, { timeout: execTimeout, maxBuffer: 1024 * 1024 * 1024 }, processGitRevParseOutput(resolve, reject)) + }) + try { + return await promise + } catch (e) { + logIfVerbose(e) + return undefined + } +} + async function reportPostInstall () { const scarfApiToken = process.env.SCARF_API_TOKEN @@ -235,6 +263,10 @@ async function reportPostInstall () { return Promise.reject(new Error('No parent found, nothing to report')) } + const gitSha = await getGitSha() + logIfVerbose(`Injecting sha to parent: ${gitSha}`) + dependencyInfo.parent.gitSha = gitSha + const rootPackage = dependencyInfo.rootPackage if (!userHasOptedIn(rootPackage) && isYarn()) { @@ -512,8 +544,10 @@ module.exports = { tmpFileName, dirName, processDependencyTreeOutput, + processGitRevParseOutput, npmExecPath, getDependencyInfo, + getGitSha, reportPostInstall, hashWithDefault, findScarfInFullDependencyTree diff --git a/test/report.test.js b/test/report.test.js index 575d416..e80680c 100644 --- a/test/report.test.js +++ b/test/report.test.js @@ -147,6 +147,16 @@ describe('Reporting tests', () => { return report.processDependencyTreeOutput(resolve, reject)(null, JSON.stringify(parsedLsOutput), null) })).rejects.toEqual(new Error('The package depending on Scarf is the root package being installed, but Scarf is not configured to run in this case. To enable it, set `scarfSettings.allowTopLevel = true` in your package.json')) }) + + test('Can parse example git rev-parse HEAD output', async () => { + await expect(new Promise((resolve, reject) => { + return report.processGitRevParseOutput(resolve, reject)(null, '9ace16b9e3833ad4e761a49f17fe607723d5bd5e\n', null) + })).resolves.toEqual('9ace16b9e3833ad4e761a49f17fe607723d5bd5e') + }) + + test('getGitSha resolves in test run of cloned repository', async () => { + await expect(report.getGitSha()).resolves.toBeTruthy() + }) }) function dependencyTreeScarfEnabled () { From 7fe93c582ee9efec3eb0cc5ebae824ed7176b839 Mon Sep 17 00:00:00 2001 From: justinwoo Date: Thu, 10 Aug 2023 14:31:03 +0300 Subject: [PATCH 2/5] we should only inject sha if the root package allows top level --- report.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/report.js b/report.js index 4ee5864..f93bb09 100644 --- a/report.js +++ b/report.js @@ -263,9 +263,11 @@ async function reportPostInstall () { return Promise.reject(new Error('No parent found, nothing to report')) } - const gitSha = await getGitSha() - logIfVerbose(`Injecting sha to parent: ${gitSha}`) - dependencyInfo.parent.gitSha = gitSha + if (allowTopLevel(dependencyInfo.rootPackage)) { + const gitSha = await getGitSha() + logIfVerbose(`Injecting sha to parent: ${gitSha}`) + dependencyInfo.parent.gitSha = gitSha + } const rootPackage = dependencyInfo.rootPackage From aa5fe404ae4ee5dfb8c080facdce5e9c62a4fa8f Mon Sep 17 00:00:00 2001 From: justinwoo Date: Thu, 10 Aug 2023 18:43:06 +0300 Subject: [PATCH 3/5] only get the git sha from root when root = parent * fix up parentIsRoot to be safe against optional values --- report.js | 12 ++++++------ test/report.test.js | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/report.js b/report.js index f93bb09..bb459d8 100644 --- a/report.js +++ b/report.js @@ -74,8 +74,8 @@ function skipTraversal (rootPackage) { } function parentIsRoot (dependencyToReport) { - return dependencyToReport.parent.name === dependencyToReport.rootPackage.name && - dependencyToReport.parent.version === dependencyToReport.rootPackage.version + return dependencyToReport?.parent?.name === dependencyToReport?.rootPackage?.name && + dependencyToReport?.parent?.version === dependencyToReport?.rootPackage?.version } function isTopLevel (dependencyToReport) { @@ -242,7 +242,7 @@ async function getDependencyInfo (packageJSONOverride) { }) } -async function getGitSha () { +async function getGitShaFromRootPath () { const promise = new Promise((resolve, reject) => { exec(`cd ${rootPath} && git rev-parse HEAD`, { timeout: execTimeout, maxBuffer: 1024 * 1024 * 1024 }, processGitRevParseOutput(resolve, reject)) }) @@ -263,8 +263,8 @@ async function reportPostInstall () { return Promise.reject(new Error('No parent found, nothing to report')) } - if (allowTopLevel(dependencyInfo.rootPackage)) { - const gitSha = await getGitSha() + if (parentIsRoot(dependencyInfo) && skipTraversal(dependencyInfo.rootPackage) && allowTopLevel(dependencyInfo.rootPackage)) { + const gitSha = await getGitShaFromRootPath() logIfVerbose(`Injecting sha to parent: ${gitSha}`) dependencyInfo.parent.gitSha = gitSha } @@ -549,7 +549,7 @@ module.exports = { processGitRevParseOutput, npmExecPath, getDependencyInfo, - getGitSha, + getGitShaFromRootPath, reportPostInstall, hashWithDefault, findScarfInFullDependencyTree diff --git a/test/report.test.js b/test/report.test.js index e80680c..2d0463b 100644 --- a/test/report.test.js +++ b/test/report.test.js @@ -154,8 +154,8 @@ describe('Reporting tests', () => { })).resolves.toEqual('9ace16b9e3833ad4e761a49f17fe607723d5bd5e') }) - test('getGitSha resolves in test run of cloned repository', async () => { - await expect(report.getGitSha()).resolves.toBeTruthy() + test('getGitShaFromRootPath resolves in test run of cloned repository', async () => { + await expect(report.getGitShaFromRootPath()).resolves.toBeTruthy() }) }) From 2a45dbf210fba2c7125d0290962e6384fd360e99 Mon Sep 17 00:00:00 2001 From: justinwoo Date: Thu, 10 Aug 2023 18:57:42 +0300 Subject: [PATCH 4/5] update runner matrix to supported node.js versions see https://github.com/nodejs/Release/tree/7b8e0ac1b97b4ce4057e64ae024995052408926a#release-schedule --- .github/workflows/main.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 7fec59b..3d1db32 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: - node-version: [10.x, 12.x] + node-version: [18.x, 20.x] steps: - uses: actions/checkout@v2 @@ -30,7 +30,7 @@ jobs: strategy: matrix: - node-version: [10.x, 12.x] + node-version: [18.x, 20.x] steps: - uses: actions/checkout@v2 @@ -51,7 +51,7 @@ jobs: strategy: matrix: - node-version: [10.x, 12.x] + node-version: [18.x, 20.x] steps: - uses: actions/checkout@v2 From 62f8cc89434c929c935cfc59cbbbbb86413b2ee2 Mon Sep 17 00:00:00 2001 From: justinwoo Date: Thu, 10 Aug 2023 18:58:59 +0300 Subject: [PATCH 5/5] update runner matrix to supported node.js versions see https://github.com/nodejs/Release/tree/7b8e0ac1b97b4ce4057e64ae024995052408926a#release-schedule --- report.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/report.js b/report.js index bb459d8..d3b01e0 100644 --- a/report.js +++ b/report.js @@ -263,7 +263,7 @@ async function reportPostInstall () { return Promise.reject(new Error('No parent found, nothing to report')) } - if (parentIsRoot(dependencyInfo) && skipTraversal(dependencyInfo.rootPackage) && allowTopLevel(dependencyInfo.rootPackage)) { + if (parentIsRoot(dependencyInfo) && allowTopLevel(dependencyInfo.rootPackage)) { const gitSha = await getGitShaFromRootPath() logIfVerbose(`Injecting sha to parent: ${gitSha}`) dependencyInfo.parent.gitSha = gitSha