Skip to content

Commit

Permalink
feat: use the current branch name as the base for wiby testing branch…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
dominykas committed Mar 18, 2021
1 parent d7168f5 commit 722e4d6
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 66 deletions.
3 changes: 2 additions & 1 deletion lib/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 14 additions & 3 deletions lib/context.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand All @@ -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',
Expand Down
5 changes: 3 additions & 2 deletions lib/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
59 changes: 24 additions & 35 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,58 +14,47 @@ 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')
}
dependentPkgJSON[dependencyType][module] = patch
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`)
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
6 changes: 3 additions & 3 deletions test/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
18 changes: 11 additions & 7 deletions test/cli/clean.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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')
})
})
4 changes: 4 additions & 0 deletions test/cli/result.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
})
Expand All @@ -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`
}
})
Expand All @@ -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`
}
})
Expand All @@ -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`
}
})
Expand Down
10 changes: 6 additions & 4 deletions test/cli/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})
2 changes: 1 addition & 1 deletion test/fixtures/http/clean-command-dry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/http/clean-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/http/result-command-empty-branch-checks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/http/result-command-positive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Expand Down

0 comments on commit 722e4d6

Please sign in to comment.