-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from dominykas/cleanup-command
- Loading branch information
Showing
29 changed files
with
436 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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.