From 4bf337bf9c5d94200625a0ff326b7b68cac80830 Mon Sep 17 00:00:00 2001 From: Glenn Hinks Date: Sat, 22 May 2021 13:25:18 -0400 Subject: [PATCH] feat: raise PR test that the parent branch exists or not in the dependent. If the dependency parent branch exists in the dependent use that branch and raise a PR of wiby-feature-x into feature-x rather than into the default branch. --- lib/test.js | 16 ++++- test/cli/closePR.js | 4 +- test/cli/test.js | 4 +- test/fixtures/http/test-command-positive.js | 6 ++ test/test.js | 76 ++++++++++++++------- 5 files changed, 77 insertions(+), 29 deletions(-) diff --git a/lib/test.js b/lib/test.js index 06c864b..8d320cd 100644 --- a/lib/test.js +++ b/lib/test.js @@ -69,8 +69,20 @@ const createPR = module.exports.createPR = async function createPR (dependentOwn ` const head = context.getTestingBranchName(parentBranchName) const draft = true - const defaultBranch = await github.getDefaultBranch(dependentOwner, dependentRepo) - const result = await github.createPR(dependentOwner, dependentRepo, title, head, defaultBranch, draft, body) + /* + from the conversation on wiby PR 93 https://github.com/pkgjs/wiby/pull/93#discussion_r615362448 + it was seen that the raising of a PR from head to main was in general ok but in the case where the + dependency feature branch does exist in the dependent then maybe detection and handle would offer a + better experience. + */ + const preExistingOnDependent = await github.getBranch(dependentOwner, dependentRepo, parentBranchName) + let result + if (preExistingOnDependent) { + result = await github.createPR(dependentOwner, dependentRepo, title, head, parentBranchName, draft, body) + } else { + const defaultBranch = await github.getDefaultBranch(dependentOwner, dependentRepo) + result = await github.createPR(dependentOwner, dependentRepo, title, head, defaultBranch, draft, body) + } debug(`PR raised upon ${result.data.html_url}`) return result } diff --git a/test/cli/closePR.js b/test/cli/closePR.js index 3c9c989..02ce01b 100644 --- a/test/cli/closePR.js +++ b/test/cli/closePR.js @@ -8,7 +8,7 @@ const path = require('path') const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby') const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures')) -tap.only('closePRs command', async (t) => { +tap.test('closePRs command', async (t) => { t.beforeEach(async () => { nock.disableNetConnect() gitFixture.init() @@ -31,7 +31,7 @@ tap.only('closePRs command', async (t) => { }).toString() t.match(result, '1 PRs closed\n') }) - t.only('close-pr should call and close the PR on command using wiby.json settings', async (t) => { + t.test('close-pr should call and close the PR on command using wiby.json settings', async (t) => { const result = childProcess.execSync(`${wibyCommand} close-pr`, { env: { ...process.env, diff --git a/test/cli/test.js b/test/cli/test.js index 493b82b..13b4e8d 100644 --- a/test/cli/test.js +++ b/test/cli/test.js @@ -9,7 +9,7 @@ const gitFixture = require('../fixtures/git') const wibyCommand = path.join(__dirname, '..', '..', 'bin', 'wiby') const fixturesPath = path.resolve(path.join(__dirname, '..', 'fixtures')) -tap.only('test command', async (tap) => { +tap.test('test command', async (tap) => { tap.test('test command should fail when config and dependent provided', async (tap) => { gitFixture.init() @@ -34,7 +34,7 @@ tap.only('test command', async (tap) => { tap.match(result, 'Changes pushed to https://github.com/wiby-test/fakeRepo/blob/wiby-running-unit-tests/package.json') }) - tap.only('test command should call test module with all deps from .wiby.json', async (tap) => { + tap.test('test command should call test module with all deps from .wiby.json', async (tap) => { gitFixture.init() const result = childProcess.execSync(`${wibyCommand} test`, { diff --git a/test/fixtures/http/test-command-positive.js b/test/fixtures/http/test-command-positive.js index 0f7a63e..6ca4f01 100644 --- a/test/fixtures/http/test-command-positive.js +++ b/test/fixtures/http/test-command-positive.js @@ -48,6 +48,8 @@ function nockPkgjsWiby (nockInstance) { } } ]) + .get('/repos/wiby-test/pass/branches/wiby-running-unit-tests') + .reply(404, {}) } function nockRepo (nockInstance, repo) { @@ -87,6 +89,10 @@ function nockRepo (nockInstance, repo) { .reply(201, { html_url: 'https://github.com/pkgjs/wiby/pull/1' }) + .get('/repos/wiby-test/pass/branches/running-unit-tests') + .reply(200, { + name: 'running-unit-tests' + }) } function buildNock () { diff --git a/test/test.js b/test/test.js index 27afeea..1972805 100644 --- a/test/test.js +++ b/test/test.js @@ -9,7 +9,7 @@ const gitFixture = require('./fixtures/git') const wiby = require('..') -tap.only('wiby.test()', async (tap) => { +tap.test('wiby.test()', async (tap) => { tap.beforeEach(async () => { nock.disableNetConnect() gitFixture.init() @@ -97,31 +97,61 @@ tap.only('wiby.test()', async (tap) => { ) tap.end() }) - tap.only('test create PR', (t) => { + tap.test('Create PR', (tap) => { + tap.plan(2) const htmlURL = `https://github.com/${CONFIG.PKG_ORG}/${CONFIG.DEP_REPO}/pull/1` - nock('https://api.github.com') - .post(/graphql/) - .reply(200, { - data: { - repository: { - defaultBranchRef: { - name: 'main' - } - } - } - }) - nock('https://api.github.com') - .post('/repos/pkgjs/wiby/pulls') - .reply(201, { - html_url: htmlURL - }) const dependentOwner = 'pkgjs' const dependentRepo = 'wiby' const parentBranchName = 'main' - wiby.test.createPR(dependentOwner, dependentRepo, parentBranchName) - .then((result) => { - t.equal(result.data.html_url, htmlURL) - t.end() - }) + tap.test('test create PR when dependency feature branch does not exist in dependent repo', (t) => { + nock('https://api.github.com') + .post('/repos/pkgjs/wiby/pulls') + .reply(201, { + html_url: htmlURL + }) + .post(/graphql/) + .reply(200, { + data: { + repository: { + defaultBranchRef: { + name: 'main' + } + } + } + }) + .get(/repos/) + .reply(404, {}) + wiby.test.createPR(dependentOwner, dependentRepo, parentBranchName) + .then((result) => { + t.equal(result.data.html_url, htmlURL) + t.end() + }) + }) + tap.test('test create PR when dependency feature branch exists in dependent repo', (t) => { + nock('https://api.github.com') + .post('/repos/pkgjs/wiby/pulls') + .reply(201, { + html_url: htmlURL + }) + .post(/graphql/) + .reply(200, { + data: { + repository: { + defaultBranchRef: { + name: 'main' + } + } + } + }) + .get(/repos/) + .reply(200, { + name: parentBranchName + }) + wiby.test.createPR(dependentOwner, dependentRepo, parentBranchName) + .then((result) => { + t.equal(result.data.html_url, htmlURL) + t.end() + }) + }) }) })