From 722e4d6e8aeec073e204894ee4852ab19caae952 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominykas=20Blyz=CC=8Ce=CC=87?= Date: Thu, 18 Mar 2021 14:30:01 +0200 Subject: [PATCH] feat: use the current branch name as the base for wiby testing branch name --- lib/clean.js | 3 +- lib/context.js | 17 +++++- lib/result.js | 5 +- lib/test.js | 59 ++++++++----------- package.json | 4 +- test/clean.js | 6 +- test/cli/clean.js | 18 +++--- test/cli/result.js | 4 ++ test/cli/test.js | 10 ++-- test/fixtures/http/clean-command-dry.js | 2 +- test/fixtures/http/clean-command.js | 4 +- .../result-command-empty-branch-checks.js | 4 +- .../result-command-positive-checks-failed.js | 2 +- test/fixtures/http/result-command-positive.js | 8 +-- 14 files changed, 80 insertions(+), 66 deletions(-) diff --git a/lib/clean.js b/lib/clean.js index 72dbb51..d063276 100644 --- a/lib/clean.js +++ b/lib/clean.js @@ -24,7 +24,8 @@ module.exports = async ({ dependents }, { all, dryRun }) => { let branches - const branch = await context.getTestingBranchName(parentPkgJSON.name) + const parentBranchName = await context.getParentBranchName() + const branch = await context.getTestingBranchName(parentBranchName) if (all) { branches = await github.getWibyBranches(dependentPkgInfo.owner, dependentPkgInfo.name) diff --git a/lib/context.js b/lib/context.js index 59d8a5e..a815dc2 100644 --- a/lib/context.js +++ b/lib/context.js @@ -1,9 +1,20 @@ 'use strict' +const simpleGit = require('simple-git') const fsPromises = require('fs').promises -exports.getTestingBranchName = async function getTestingBranchName (parentPackageName) { - return `wiby-${parentPackageName}` +exports.getParentBranchName = async function getParentBranchName () { + const { current } = await simpleGit().branch() + + return current +} + +exports.getTestingBranchName = function getTestingBranchName (parentBranchName) { + return `wiby-${parentBranchName}` +} + +exports.getDependencyLink = async function getDependencyLink (owner, repo, commitish) { + return `${owner}/${repo}#${commitish}` } exports.getLocalPackageJSON = async function getLocalPackageJSON () { @@ -15,7 +26,7 @@ exports.getLocalPackageJSON = async function getLocalPackageJSON () { The check should search both the dev and peer dependencies to find out if the dependency is regular, dev or peer. */ -exports.checkPackageInPackageJSON = function checkPackageInPackageJSON (dep, packageJSON) { +exports.checkDependentUsesParent = function checkDependentUsesParent (dep, packageJSON) { const dependencyKeyNames = [ 'dependencies', 'devDependencies', diff --git a/lib/result.js b/lib/result.js index 7354793..93ce1d4 100644 --- a/lib/result.js +++ b/lib/result.js @@ -30,11 +30,12 @@ module.exports = async function ({ dependents }) { const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name) debug(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`) - if (!context.checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) { + if (!context.checkDependentUsesParent(parentPkgJSON.name, dependentPkgJSON)) { throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`) } - const branch = await context.getTestingBranchName(parentPkgJSON.name) + const parentBranchName = await context.getParentBranchName() + const branch = await context.getTestingBranchName(parentBranchName) let resp = await github.getChecks(dependentPkgInfo.owner, dependentPkgInfo.name, branch) if (resp.data.check_runs.length === 0) { resp = await github.getCommitStatusesForRef(dependentPkgInfo.owner, dependentPkgInfo.name, branch) diff --git a/lib/test.js b/lib/test.js index c02861b..e00f594 100644 --- a/lib/test.js +++ b/lib/test.js @@ -14,41 +14,30 @@ module.exports = async function ({ dependents }) { logger.enableLogs(testCommandNamespace) const parentPkgJSON = await context.getLocalPackageJSON() - const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url) - debug(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`) + const parentRepositoryInfo = gitURLParse(parentPkgJSON.repository.url) + const parentBranchName = await context.getParentBranchName() + debug(`Parent module: ${parentRepositoryInfo.owner}/${parentPkgJSON.name}`) - const commitURL = await getCommitURL(parentPkgInfo.owner, parentPkgInfo.name) - debug('Commit URL to test:', commitURL) + const parentDependencyLink = await context.getDependencyLink(parentRepositoryInfo.owner, parentRepositoryInfo.name, parentBranchName) + debug('Commit URL to test:', parentDependencyLink) for (const { repository: url } of dependents) { - const dependentPkgInfo = gitURLParse(url) - const dependentPkgJSON = await github.getPackageJson(dependentPkgInfo.owner, dependentPkgInfo.name) - debug(`Dependent module: ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`) + const dependentRepositoryInfo = gitURLParse(url) + const dependentPkgJson = await github.getPackageJson(dependentRepositoryInfo.owner, dependentRepositoryInfo.name) + debug(`Dependent module: ${dependentRepositoryInfo.owner}/${dependentRepositoryInfo.name}`) - if (!context.checkPackageInPackageJSON(parentPkgJSON.name, dependentPkgJSON)) { - throw new Error(`${parentPkgInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentPkgInfo.owner}/${dependentPkgInfo.name}`) + if (!context.checkDependentUsesParent(parentPkgJSON.name, dependentPkgJson)) { + throw new Error(`${parentRepositoryInfo.owner}/${parentPkgJSON.name} not found in the package.json of ${dependentRepositoryInfo.owner}/${dependentRepositoryInfo.name}`) } - const patchedPackageJSON = applyPatch(commitURL, parentPkgJSON.name, dependentPkgJSON) - await pushPatch(patchedPackageJSON, dependentPkgInfo.owner, dependentPkgInfo.name, parentPkgJSON.name) + const patchedPackageJSON = applyPatch(parentDependencyLink, parentPkgJSON.name, dependentPkgJson, parentPkgJSON.name) + await pushPatch(patchedPackageJSON, dependentRepositoryInfo.owner, dependentRepositoryInfo.name, parentPkgJSON.name, parentBranchName) } } -const getCommitHash = async function getCommitHash (owner, repo) { - const [headSha] = await github.getShas(owner, repo) - return headSha -} - -const getCommitURL = async function getCommitURL (owner, repo, hash) { - hash = hash || await getCommitHash(owner, repo) - - const patch = `${owner}/${repo}#${hash}` - return patch -} - const applyPatch = module.exports.applyPatch = function applyPatch (patch, module, dependentPkgJSON) { - const dependencyType = context.checkPackageInPackageJSON(module, dependentPkgJSON) + const dependencyType = context.checkDependentUsesParent(module, dependentPkgJSON) if (!dependencyType) { throw new Error('Dependency not found in package.json') } @@ -56,16 +45,16 @@ function applyPatch (patch, module, dependentPkgJSON) { return dependentPkgJSON } -async function pushPatch (packageJSON, owner, repo, dep) { - const file = JSON.stringify(packageJSON, null, 2) + '\n' // assumes package.json is using two spaces +async function pushPatch (dependentPkgJson, dependentOwner, dependentRepo, parentName, parentBranchName) { + const file = JSON.stringify(dependentPkgJson, null, 2) + '\n' // assumes package.json is using two spaces const encodedFile = Buffer.from(file).toString('base64') - const message = `wiby: update ${dep}` - const branch = await context.getTestingBranchName(dep) - - const [headSha, treeSha] = await github.getShas(owner, repo) - const blobSha = await github.createBlob(owner, repo, encodedFile) - const newTreeSha = await github.createTree(owner, repo, treeSha, blobSha) - const commitSha = await github.createCommit(owner, repo, message, newTreeSha, headSha) - await github.createBranch(owner, repo, commitSha, branch) - debug(`Changes pushed to https://github.com/${owner}/${repo}/blob/${branch}/package.json`) + const message = `wiby: update ${parentName}` + const branch = await context.getTestingBranchName(parentBranchName) + + const [headSha, treeSha] = await github.getShas(dependentOwner, dependentRepo) + const blobSha = await github.createBlob(dependentOwner, dependentRepo, encodedFile) + const newTreeSha = await github.createTree(dependentOwner, dependentRepo, treeSha, blobSha) + const commitSha = await github.createCommit(dependentOwner, dependentRepo, message, newTreeSha, headSha) + await github.createBranch(dependentOwner, dependentRepo, commitSha, branch) + debug(`Changes pushed to https://github.com/${dependentOwner}/${dependentRepo}/blob/${branch}/package.json`) } diff --git a/package.json b/package.json index cfaca88..4b9be58 100644 --- a/package.json +++ b/package.json @@ -37,12 +37,14 @@ "git-url-parse": "^11.1.2", "joi": "^17.2.1", "node-fetch": "^2.6.0", + "simple-git": "^2.35.1", "yargs": "^16.0.0" }, "devDependencies": { "nock": "^13.0.3", "standard": "^16.0.0", - "tap": "^14.10.7" + "tap": "^14.10.7", + "tmp": "^0.2.1" }, "standard": { "ignore": [ diff --git a/test/clean.js b/test/clean.js index 8580023..37b8957 100644 --- a/test/clean.js +++ b/test/clean.js @@ -18,17 +18,17 @@ tap.afterEach(async () => { tap.test('wiby.clean()', async (tap) => { tap.test('should check if the wiby branch exists', async (tap) => { nock('https://api.github.com') - .get(`/repos/wiby-test/${CONFIG.DEP_REPO}/branches/wiby-wiby`) + .get(`/repos/wiby-test/${CONFIG.DEP_REPO}/branches/wiby-branch-naming`) .reply(404) await wiby.clean({ dependents: [{ repository: `https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}` }] }, {}) - // implied assertion - no DELETE requests expected - we don't need to delete the missing `wiby-wiby` branch + // implied assertion - no DELETE requests expected - we don't need to delete the missing `wiby-branch-naming` branch }) tap.test('should rethrow when github API inaccessible during branch check', async (tap) => { nock('https://api.github.com') - .get(`/repos/wiby-test/${CONFIG.DEP_REPO}/branches/wiby-wiby`) + .get(`/repos/wiby-test/${CONFIG.DEP_REPO}/branches/wiby-branch-naming`) .reply(500) await tap.rejects( diff --git a/test/cli/clean.js b/test/cli/clean.js index 2538d80..fbd1852 100644 --- a/test/cli/clean.js +++ b/test/cli/clean.js @@ -13,30 +13,33 @@ tap.test('clean command', async (tap) => { const result = childProcess.execSync(`${wibyCommand} clean`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/clean-command.js` } }).toString() tap.includes(result, 'Branches deleted:') - tap.includes(result, '- https://github.com/wiby-test/partial: wiby-wiby') - tap.includes(result, '- git://github.com/wiby-test/fail: wiby-wiby') - tap.includes(result, '- git+https://github.com/wiby-test/pass: wiby-wiby') + tap.includes(result, '- https://github.com/wiby-test/partial: wiby-branch-naming') + tap.includes(result, '- git://github.com/wiby-test/fail: wiby-branch-naming') + tap.includes(result, '- git+https://github.com/wiby-test/pass: wiby-branch-naming') }) tap.test('should delete test branch in the test module at dependent URI', async (tap) => { const result = childProcess.execSync(`${wibyCommand} clean --dependent="https://github.com/wiby-test/fakeRepo"`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/clean-command.js` } }).toString() tap.includes(result, 'Branches deleted:') - tap.includes(result, '- https://github.com/wiby-test/fakeRepo: wiby-wiby') + tap.includes(result, '- https://github.com/wiby-test/fakeRepo: wiby-branch-naming') }) tap.test('should delete all wiby-* branches in all configured test modules', async (tap) => { const result = childProcess.execSync(`${wibyCommand} clean --all`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/clean-command-all.js` } }).toString() @@ -50,13 +53,14 @@ tap.test('clean command', async (tap) => { const result = childProcess.execSync(`${wibyCommand} clean --dry-run`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/clean-command-dry.js` } }).toString() tap.includes(result, 'Branches to be deleted:') - tap.includes(result, '- https://github.com/wiby-test/partial: wiby-wiby') - tap.includes(result, '- git://github.com/wiby-test/fail: wiby-wiby') - tap.includes(result, '- git+https://github.com/wiby-test/pass: wiby-wiby') + tap.includes(result, '- https://github.com/wiby-test/partial: wiby-branch-naming') + tap.includes(result, '- git://github.com/wiby-test/fail: wiby-branch-naming') + tap.includes(result, '- git+https://github.com/wiby-test/pass: wiby-branch-naming') }) }) diff --git a/test/cli/result.js b/test/cli/result.js index 5ce4ab7..0bc04de 100644 --- a/test/cli/result.js +++ b/test/cli/result.js @@ -32,6 +32,7 @@ tap.test('result command', async (tap) => { childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js` } }) @@ -54,6 +55,7 @@ tap.test('result command', async (tap) => { childProcess.execSync(`${wibyCommand} result`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive.js` } }) @@ -76,6 +78,7 @@ tap.test('result command', async (tap) => { childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-empty-branch-checks.js` } }) @@ -100,6 +103,7 @@ tap.test('result command', async (tap) => { childProcess.execSync(`${wibyCommand} result --dependent="https://github.com/wiby-test/fakeRepo"`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/result-command-positive-checks-failed.js` } }) diff --git a/test/cli/test.js b/test/cli/test.js index 915251f..327b739 100644 --- a/test/cli/test.js +++ b/test/cli/test.js @@ -22,23 +22,25 @@ tap.test('test command', async (tap) => { const result = childProcess.execSync(`${wibyCommand} test --dependent="https://github.com/wiby-test/fakeRepo"`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/test-command-positive.js` } }).toString() - tap.includes(result, 'Changes pushed to https://github.com/wiby-test/fakeRepo/blob/wiby-wiby/package.json') + tap.includes(result, 'Changes pushed to https://github.com/wiby-test/fakeRepo/blob/wiby-branch-naming/package.json') }) tap.test('test command should call test module with all deps from .wiby.json', async (tap) => { const result = childProcess.execSync(`${wibyCommand} test`, { cwd: cwd, env: { + ...process.env, NODE_OPTIONS: `-r ${fixturesPath}/http/test-command-positive.js` } }).toString() - tap.includes(result, 'Changes pushed to https://github.com/wiby-test/pass/blob/wiby-wiby/package.json') - tap.includes(result, 'Changes pushed to https://github.com/wiby-test/fail/blob/wiby-wiby/package.json') - tap.includes(result, 'Changes pushed to https://github.com/wiby-test/partial/blob/wiby-wiby/package.json') + tap.includes(result, 'Changes pushed to https://github.com/wiby-test/pass/blob/wiby-branch-naming/package.json') + tap.includes(result, 'Changes pushed to https://github.com/wiby-test/fail/blob/wiby-branch-naming/package.json') + tap.includes(result, 'Changes pushed to https://github.com/wiby-test/partial/blob/wiby-branch-naming/package.json') }) }) diff --git a/test/fixtures/http/clean-command-dry.js b/test/fixtures/http/clean-command-dry.js index 3e66a37..417dfb0 100644 --- a/test/fixtures/http/clean-command-dry.js +++ b/test/fixtures/http/clean-command-dry.js @@ -9,7 +9,7 @@ nock.disableNetConnect() function nockRepo (nockInstance, repo) { return nockInstance - .get(`/repos/wiby-test/${repo}/branches/wiby-wiby`) + .get(`/repos/wiby-test/${repo}/branches/wiby-branch-naming`) .reply(200) } diff --git a/test/fixtures/http/clean-command.js b/test/fixtures/http/clean-command.js index 59d5a79..9b31cab 100644 --- a/test/fixtures/http/clean-command.js +++ b/test/fixtures/http/clean-command.js @@ -9,9 +9,9 @@ nock.disableNetConnect() function nockRepo (nockInstance, repo) { return nockInstance - .get(`/repos/wiby-test/${repo}/branches/wiby-wiby`) + .get(`/repos/wiby-test/${repo}/branches/wiby-branch-naming`) .reply(200) - .delete(`/repos/wiby-test/${repo}/git/refs/heads%2Fwiby-wiby`) + .delete(`/repos/wiby-test/${repo}/git/refs/heads%2Fwiby-branch-naming`) .reply(200) } diff --git a/test/fixtures/http/result-command-empty-branch-checks.js b/test/fixtures/http/result-command-empty-branch-checks.js index 622686c..28a3645 100644 --- a/test/fixtures/http/result-command-empty-branch-checks.js +++ b/test/fixtures/http/result-command-empty-branch-checks.js @@ -24,14 +24,14 @@ nock('https://api.github.com') } }) // get check results - .get('/repos/wiby-test/fakeRepo/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/fakeRepo/commits/wiby-branch-naming/check-runs') .reply(200, { check_runs: [ // empty checks list ] }) // get checks for ref - .get('/repos/wiby-test/fakeRepo/commits/wiby-wiby/statuses') + .get('/repos/wiby-test/fakeRepo/commits/wiby-branch-naming/statuses') .reply(200, { check_runs: [ { status: 'queued', name: 'fake_run' }, diff --git a/test/fixtures/http/result-command-positive-checks-failed.js b/test/fixtures/http/result-command-positive-checks-failed.js index c205489..4bf3c9f 100644 --- a/test/fixtures/http/result-command-positive-checks-failed.js +++ b/test/fixtures/http/result-command-positive-checks-failed.js @@ -26,7 +26,7 @@ nock('https://api.github.com') } }) // get check results - .get('/repos/wiby-test/fakeRepo/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/fakeRepo/commits/wiby-branch-naming/check-runs') .reply(200, { check_runs: [ { status: 'done', name: 'fake_run', conclusion: 'failure' } diff --git a/test/fixtures/http/result-command-positive.js b/test/fixtures/http/result-command-positive.js index 8e39693..dda2a7d 100644 --- a/test/fixtures/http/result-command-positive.js +++ b/test/fixtures/http/result-command-positive.js @@ -25,28 +25,28 @@ nock('https://api.github.com') } }) // get check results - .get('/repos/wiby-test/fakeRepo/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/fakeRepo/commits/wiby-branch-naming/check-runs') .reply(200, { check_runs: [ { status: 'queued', name: 'fake_run' }, { status: 'done', name: 'fake_run_2', conclusion: 'fake_conclusion' } ] }) - .get('/repos/wiby-test/fail/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/fail/commits/wiby-branch-naming/check-runs') .reply(200, { check_runs: [ { status: 'queued', name: 'fail_run' }, { status: 'done', name: 'fail_run_2', conclusion: 'fake_conclusion' } ] }) - .get('/repos/wiby-test/pass/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/pass/commits/wiby-branch-naming/check-runs') .reply(200, { check_runs: [ { status: 'queued', name: 'pass_run' }, { status: 'done', name: 'pass_run_2', conclusion: 'fake_conclusion' } ] }) - .get('/repos/wiby-test/partial/commits/wiby-wiby/check-runs') + .get('/repos/wiby-test/partial/commits/wiby-branch-naming/check-runs') .reply(200, { check_runs: [ { status: 'queued', name: 'partial_run' },