Skip to content

Commit

Permalink
Merge pull request #24 from bradgarropy/copy
Browse files Browse the repository at this point in the history
🧬 copy
  • Loading branch information
bradgarropy authored Dec 24, 2019
2 parents 0d183b5 + ae0b131 commit 9217210
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 119 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "labman",
"version": "0.3.0",
"version": "0.4.0",
"description": "👨🏼‍🔬 github label manager cli",
"keywords": [
"github",
Expand All @@ -22,14 +22,14 @@
"url": "https://bradgarropy.com"
},
"bin": {
"labman": "src/cli.js"
"labman": "src/cli/index.js"
},
"repository": {
"type": "git",
"url": "https://github.com/bradgarropy/labman-cli"
},
"scripts": {
"start": "node src/cli.js"
"start": "node src/cli/index.js"
},
"dependencies": {
"@octokit/rest": "^16.35.2",
Expand Down
8 changes: 4 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,24 @@ If you have `labman` installed globally you can run it as shown below.

```
labman login <username> <token>
labman clone <source> <destination>
labman <source> <destination>
```

Where `token` is your GitHub [personal access token][token].
Where `token` is a GitHub [personal access token][token] with `repo` scope.
Where `source` and `destination` are GitHub repositories in the form of `owner/repo`.

Here is an example.

```
labman login bradgarropy 1234abcd
labman clone bradgarropy/label-source bradgarropy/label-destination
labman bradgarropy/label-source bradgarropy/label-destination
```

Alternatively, you can run it with [`npx`][npx].

```
npx labman login <username> <token>
npx labman clone <source> <destination>
npx labman <source> <destination>
```

## ❔ Questions
Expand Down
31 changes: 0 additions & 31 deletions src/cli.js

This file was deleted.

54 changes: 54 additions & 0 deletions src/cli/default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const conf = require("conf")
const chalk = require("chalk")
const copy = require("../copy")
const {createOctokit} = require("../octokit")

const config = new conf()

const command = "* <source> <destination> [labels...]"
const description = "Copy issue labels from one repo to another"

const builder = {
clobber: {
alias: "c",
type: "boolean",
default: false,
description: "Clobber destination labels",
},
}

const handler = async argv => {
const {source, destination, labels, clobber} = argv
const token = config.get("token")

if (!token) {
console.log(
`\nYou are not logged in, please run the ${chalk.cyanBright(
"login",
)} command.\n`,
)

console.log(chalk.cyanBright("labman login <username> <token>\n"))
return
}

createOctokit(token)

try {
await copy(source, destination, labels, clobber)
} catch (error) {
console.log(
`\n${chalk.redBright(
"Invalid token!",
)} Please run the ${chalk.cyanBright("login")} command again.\n`,
)
console.log(chalk.cyanBright("labman login <username> <token>\n"))
}
}

module.exports = {
command,
description,
builder,
handler,
}
13 changes: 13 additions & 0 deletions src/cli/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env node

const yargs = require("yargs")
const {name} = require("../../package.json")

yargs
.scriptName(name)
.command(require("./login"))
.command(require("./logout"))
.command(require("./default"))
.help()
.alias("help", "h")
.alias("version", "v").argv
46 changes: 46 additions & 0 deletions src/cli/login.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const conf = require("conf")
const chalk = require("chalk")
const {validToken} = require("../github")

const config = new conf()

const command = "login <username> <token>"
const description = "Persist GitHub credentials"

const builder = {
force: {
alias: "f",
type: "boolean",
default: false,
description: "Force login",
},
}

const handler = async argv => {
const {username, token, force} = argv
const storedToken = config.get("token")

if (!force && storedToken) {
console.log("\nYou are already logged in!\n")
return
}

const valid = await validToken(token)

if (!valid) {
console.log(`\n${chalk.redBright("Login failed!")} Please try again.\n`)
return
}

config.set({username, token})
console.log(chalk.greenBright("\nLogin successful!\n"))

return
}

module.exports = {
command,
description,
builder,
handler,
}
15 changes: 15 additions & 0 deletions src/cli/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const conf = require("conf")

const config = new conf()

const command = "logout"
const description = "Remove GitHub credentials"
const builder = {}
const handler = () => config.clear()

module.exports = {
command,
description,
builder,
handler,
}
16 changes: 0 additions & 16 deletions src/clone.js

This file was deleted.

23 changes: 23 additions & 0 deletions src/copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const {getLabels, deleteLabels, createLabels} = require("./github")

const copy = async (source, destination, labels = [], clobber = false) => {
const [sourceOwner, sourceRepo] = source.split("/")
const [destinationOwner, destinationRepo] = destination.split("/")

// delete existing labels
if (clobber) {
const oldLabels = await getLabels(destinationOwner, destinationRepo)
await deleteLabels(oldLabels, destinationOwner, destinationRepo)
}

// create new labels
const sourceLabels = await getLabels(sourceOwner, sourceRepo)

const newLabels = labels.length
? sourceLabels.filter(label => labels.includes(label.name))
: sourceLabels

await createLabels(newLabels, destinationOwner, destinationRepo)
}

module.exports = copy
64 changes: 0 additions & 64 deletions src/handlers.js

This file was deleted.

0 comments on commit 9217210

Please sign in to comment.