-
Notifications
You must be signed in to change notification settings - Fork 9
wiby clean
#81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
wiby clean
#81
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
38b9385
style: use strict
dominykas 913a88b
test: disable net connect for cli tests
dominykas eb8ae4a
refactor: extract graphql
dominykas b12c71d
feat: basic `wiby clean` command
dominykas 42ee92c
fix: crash on unhandled rejection
dominykas 7b80990
feat: prevent a crash when default wiby branch missing
dominykas c1195ac
feat: support `wiby clean --all`
dominykas a831c5d
feat: support `wiby clean --dry-run`
dominykas 0108df8
chore: clean up copy/paste remains
dominykas 196d30a
test: document implied assertion
dominykas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
'use strict' | ||
|
||
const wiby = require('../..') | ||
|
||
exports.desc = 'Use this command to clean up branches created by wiby (i.e. branches with the "wiby-" prefix).' | ||
|
||
exports.builder = (yargs) => yargs | ||
.option('dependent', { | ||
desc: 'URL of a dependent', | ||
type: 'string', | ||
conflicts: 'config' | ||
}) | ||
.option('config', { | ||
desc: 'Path to the configuration file. By default it will try to load the configuration from the first file it finds in the current working directory: `.wiby.json`, `.wiby.js`', | ||
type: 'string' | ||
}) | ||
.option('all', { | ||
desc: 'Remove all branches with "wiby-" prefix. By default, `wiby clean` will only remove the branch that would be created if `wiby test` ran in the current repository, on the current branch.' | ||
}) | ||
.option('dry-run', { | ||
desc: 'Print the list of branches to be removed.' | ||
}) | ||
|
||
exports.handler = async (params) => { | ||
const config = params.dependent | ||
? { dependents: [{ repository: params.dependent }] } | ||
: wiby.validate({ config: params.config }) | ||
|
||
return wiby.clean(config, params) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict' | ||
|
||
const github = require('./github') | ||
const gitURLParse = require('git-url-parse') | ||
const logger = require('./logger') | ||
|
||
// setup logger namespace | ||
const cleanCommandNamespace = 'wiby:clean' | ||
const debug = logger(cleanCommandNamespace) | ||
|
||
module.exports = async ({ dependents }, { all, dryRun }) => { | ||
// enable log output for clean command without DEBUG env | ||
logger.enableLogs(cleanCommandNamespace) | ||
|
||
const parentPkgJSON = await require('./test').getLocalPackageJSON() | ||
const parentPkgInfo = gitURLParse(parentPkgJSON.repository.url) | ||
debug(`Parent module: ${parentPkgInfo.owner}/${parentPkgJSON.name}`) | ||
|
||
console.log(dryRun ? 'Branches to be deleted:' : 'Branches deleted:') | ||
|
||
for (const { repository: url } of dependents) { | ||
const dependentPkgInfo = gitURLParse(url) | ||
|
||
let branches | ||
|
||
const branch = await require('./result').getBranchName(parentPkgJSON.name) | ||
|
||
if (all) { | ||
branches = await github.getWibyBranches(dependentPkgInfo.owner, dependentPkgInfo.name) | ||
} else if (await github.getBranch(dependentPkgInfo.owner, dependentPkgInfo.name, branch)) { | ||
branches = [branch] | ||
} else { | ||
branches = [] | ||
} | ||
|
||
if (!dryRun) { | ||
for (const branch of branches) { | ||
await github.deleteBranch(dependentPkgInfo.owner, dependentPkgInfo.name, branch) | ||
} | ||
} | ||
|
||
console.log(`- ${dependentPkgInfo}: ${branches.length ? branches.join(', ') : '(none)'}`) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
query($owner: String!, $repo: String!) { | ||
repository(name: $repo, owner: $owner) { | ||
object(expression: "HEAD:package.json") { | ||
... on Blob { | ||
text | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
query getExistingRepoBranches($owner: String!, $repo: String!) { | ||
organization(login: $owner) { | ||
repository(name: $repo) { | ||
id | ||
name | ||
refs(refPrefix: "refs/heads/", query: "wiby-", first: 100) { | ||
edges { | ||
node { | ||
branchName: name | ||
} | ||
} | ||
pageInfo { | ||
endCursor | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
exports.getPackageJson = fs.readFileSync(path.join(__dirname, 'getPackageJson.graphql')).toString() | ||
|
||
exports.getWibyBranches = fs.readFileSync(path.join(__dirname, 'getWibyBranches.graphql')).toString() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
'use strict' | ||
|
||
const debugPkg = require('debug') | ||
|
||
/** | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict' | ||
|
||
require('dotenv').config() | ||
const tap = require('tap') | ||
const nock = require('nock') | ||
const CONFIG = require('./fixtures/config') | ||
const wiby = require('..') | ||
|
||
tap.beforeEach(async () => { | ||
nock.disableNetConnect() | ||
}) | ||
|
||
tap.afterEach(async () => { | ||
nock.cleanAll() | ||
nock.enableNetConnect() | ||
}) | ||
|
||
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`) | ||
.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 | ||
}) | ||
|
||
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`) | ||
.reply(500) | ||
|
||
await tap.rejects( | ||
wiby.clean({ dependents: [{ repository: `https://www.github.com/${CONFIG.DEP_ORG}/${CONFIG.DEP_REPO}` }] }, {}) | ||
) | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.