Skip to content

Commit

Permalink
feat: update existing branches
Browse files Browse the repository at this point in the history
  • Loading branch information
dominykas committed Jul 13, 2021
1 parent 014f2c8 commit 15cd50a
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 21 deletions.
15 changes: 13 additions & 2 deletions lib/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ module.exports.getShas = async function getShas (owner, repo) {
})
const headSha = resp.data[0].sha
const treeSha = resp.data[0].commit.tree.sha
return [headSha, treeSha]
return { headSha, treeSha }
}

module.exports.createBlob = async function createBlob (owner, repo, file) {
Expand Down Expand Up @@ -117,11 +117,13 @@ module.exports.deleteBranch = async function deleteBranch (owner, repo, branch)

module.exports.getBranch = async function getBranch (owner, repo, branch) {
try {
return await octokit.repos.getBranch({
const { data } = await octokit.repos.getBranch({
owner,
repo,
branch
})

return data
} catch (err) {
if (err.status === 404) {
return undefined
Expand Down Expand Up @@ -166,3 +168,12 @@ module.exports.closePR = async function closePR (owner, repo, pullNumber) {
state: 'closed'
})
}

module.exports.updateRef = async function updateRef (owner, repo, ref, sha) {
return octokit.rest.git.updateRef({
owner,
repo,
ref,
sha
})
}
26 changes: 20 additions & 6 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,28 @@ async function pushPatch (dependentPkgJson, dependentOwner, dependentRepo, paren
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 ${parentName}`
const branch = await context.getTestingBranchName(parentBranchName)
const testingBranchName = await context.getTestingBranchName(parentBranchName)

const [headSha, treeSha] = await github.getShas(dependentOwner, dependentRepo)
const testingBranch = await github.getBranch(dependentOwner, dependentRepo, testingBranchName)
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`)

if (!testingBranch) {
// testing branch does not yet exist - create it
const { headSha, treeSha } = await github.getShas(dependentOwner, dependentRepo)
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, testingBranchName)

debug(`Changes pushed to https://github.com/${dependentOwner}/${dependentRepo}/blob/${testingBranchName}/package.json`)
} else {
// testing branch already exists - ensure we have the package.json patched or at least push an empty commit
const { sha: headSha, commit: { tree: { sha: treeSha } } } = testingBranch.commit
const newTreeSha = await github.createTree(dependentOwner, dependentRepo, treeSha, blobSha)
const commitSha = await github.createCommit(dependentOwner, dependentRepo, message, newTreeSha, headSha)
await github.updateRef(dependentOwner, dependentRepo, `heads/${testingBranchName}`, commitSha)

debug(`Pushed a new commit to https://github.com/${dependentOwner}/${dependentRepo}#${testingBranchName}`)
}
}

const createPR = module.exports.createPR = async function createPR (dependentOwner, dependentRepo, parentBranchName, parentDependencyLink) {
Expand Down
15 changes: 15 additions & 0 deletions test/cli/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ tap.test('test command', async (tap) => {
tap.match(result, 'Changes pushed to https://github.com/wiby-test/partial/blob/wiby-running-unit-tests/package.json')
})

tap.test('test command should update existing wiby test branches', async (tap) => {
gitFixture.init('existing-branch')

const result = childProcess.execSync(`${wibyCommand} test`, {
env: {
...process.env,
NODE_OPTIONS: `-r ${fixturesPath}/http/test-command-positive.js`
}
}).toString()

tap.match(result, 'Pushed a new commit to https://github.com/wiby-test/pass#wiby-existing-branch')
tap.match(result, 'Pushed a new commit to https://github.com/wiby-test/fail#wiby-existing-branch')
tap.match(result, 'Pushed a new commit to https://github.com/wiby-test/partial#wiby-existing-branch')
})

tap.test('test command should not add `wiby-` prefix when branch already has it', async (tap) => {
gitFixture.init('wiby-running-unit-tests')

Expand Down
8 changes: 2 additions & 6 deletions test/closePR.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ tap.test('close PR', (t) => {
nock('https://api.github.com')
// get package json
.patch('/repos/pkgjs/wiby/pulls/1')
.reply(200, {
data: {}
})
.reply(200, {})
const result = await closePR.closeDependencyPR(owner, repo, branch, [{
status: 'completed',
conclusion: 'success',
Expand All @@ -68,9 +66,7 @@ tap.test('close PR', (t) => {
// nock setup
nock('https://api.github.com')
.get(/repos.*check-runs/)
.reply(200, {
data: {}
})
.reply(200, {})
const result = await closePR({
dependents: [
{
Expand Down
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 @@ -10,7 +10,7 @@ nock.disableNetConnect()
function nockRepo (nockInstance, repo) {
return nockInstance
.get(`/repos/wiby-test/${repo}/branches/wiby-running-unit-tests`)
.reply(200)
.reply(200, {})
}

function buildNock () {
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/http/clean-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ nock.disableNetConnect()
function nockRepo (nockInstance, repo) {
return nockInstance
.get(`/repos/wiby-test/${repo}/branches/wiby-running-unit-tests`)
.reply(200)
.reply(200, {})
.delete(`/repos/wiby-test/${repo}/git/refs/heads%2Fwiby-running-unit-tests`)
.reply(200)
}
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/http/result-command-missing-branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ nock('https://api.github.com')
.get('/repos/wiby-test/pass/branches/wiby-running-unit-tests')
.reply(200, {})
.get('/repos/wiby-test/fail/branches/wiby-running-unit-tests')
.reply(404, {})
.reply(404)
// get check results
.get('/repos/wiby-test/fakeRepo/commits/wiby-running-unit-tests/check-runs')
.reply(200, {
Expand Down
32 changes: 29 additions & 3 deletions test/fixtures/http/test-command-positive.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function nockPkgjsWiby (nockInstance) {
}
])
.get('/repos/wiby-test/pass/branches/wiby-running-unit-tests')
.reply(404, {})
.reply(404)
}

function nockRepo (nockInstance, repo) {
Expand Down Expand Up @@ -89,10 +89,36 @@ function nockRepo (nockInstance, repo) {
.reply(201, {
html_url: 'https://github.com/pkgjs/wiby/pull/1'
})
.get('/repos/wiby-test/pass/branches/running-unit-tests')
.get(`/repos/wiby-test/${repo}/branches/running-unit-tests`)
.reply(200, {
name: 'running-unit-tests'
name: 'running-unit-tests',
commit: {
sha: 'head_sha',
commit: {
tree: {
sha: 'tree_sha'
}
}
}
})
.get(`/repos/wiby-test/${repo}/branches/wiby-running-unit-tests`)
.reply(404)
.get(`/repos/wiby-test/${repo}/branches/existing-branch`)
.reply(404)
.get(`/repos/wiby-test/${repo}/branches/wiby-existing-branch`)
.reply(200, {
name: 'wiby-existing-branch',
commit: {
sha: 'head_sha',
commit: {
tree: {
sha: 'tree_sha'
}
}
}
})
.patch(`/repos/wiby-test/${repo}/git/refs/heads%2Fwiby-existing-branch`, { sha: 'fake_sha' })
.reply(204)
}

function buildNock () {
Expand Down
2 changes: 1 addition & 1 deletion test/internals/github-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ tap.test('package.json can be fetched with a valid url', async tap => {
}, { skip: !process.env.GITHUB_TOKEN })

tap.test('Shas returned from getShas', async tap => {
const [headSha, treeSha] = await github.getShas('pkgjs', 'wiby')
const { headSha, treeSha } = await github.getShas('pkgjs', 'wiby')
tap.notEqual(headSha, null)
tap.notEqual(treeSha, null)
}, { skip: !process.env.GITHUB_TOKEN })
Expand Down

0 comments on commit 15cd50a

Please sign in to comment.