Skip to content
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

Fix auth #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.idea/
39 changes: 11 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,28 @@
const fs = require('fs');
const path = require('path');
const co = require('co');
const auth = require('./lib/auth');
const label = require('./lib/label');
const color = require('./lib/colors.json');
const dotfile = getDotFile();
const GitHubApi = require('github');
const { Octokit } = require('@octokit/rest');

module.exports = program => {
const github = new GitHubApi({
version: '3.0.0',
protocol: 'https',
pathPrefix: program.pathPrefix,
host: program.host,
});
co(function* () {
let token = program.token || readToken();
const token = program.token || readToken();

const octokit = new Octokit({
auth: token,
});

const repo = program.args[0].split('/');

const opt = {
config: parse(program.config),
token,
repo: program.args[0],
github,
owner: repo[0],
repo: repo[1],
github: octokit.rest,
};

/*
Github Authorization
*/

token = yield auth(opt);
if (token) {
yield saveToken(token);
}
console.info('>> Authorized');

/*
Force option will delete add existing labels
*/
Expand Down Expand Up @@ -84,12 +73,6 @@ function readToken() {
return null;
}

function saveToken(token) {
return function(callback) {
fs.writeFile(dotfile, token, callback);
};
}

function randomColor() {
const len = color.length;
return color[Math.floor(Math.random() * len)];
Expand Down
81 changes: 0 additions & 81 deletions lib/auth.js

This file was deleted.

24 changes: 12 additions & 12 deletions lib/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
exports.create = function* (opt) {
const github = opt.github;
const config = opt.config;
const user = opt.repo.split('/')[0];
const repo = opt.repo.split('/')[1];
const owner = opt.owner;
const repo = opt.repo;

/*
Fetch all existing labels and transform
*/

const labelsObj = {};
let labels = yield github.issues.getLabels({ user, repo });
labels.forEach(function(label) {
let labels = yield github.issues.listLabelsForRepo({ owner, repo });
labels.data.forEach(function(label) {
labelsObj[label.name] = label;
});

Expand All @@ -28,7 +28,7 @@ exports.create = function* (opt) {
if (label.color !== old.color) {
console.info('>> Update label ' + label.name + ', color ' + label.color);
return github.issues.updateLabel({
user,
owner,
repo,
name: label.name,
color: label.color.replace(/^#/, ''),
Expand All @@ -41,7 +41,7 @@ exports.create = function* (opt) {
} else {
console.info('>> Create label ' + label.name + ', color ' + label.color);
return github.issues.createLabel({
user,
owner,
repo,
name: label.name,
color: label.color.replace(/^#/, ''),
Expand All @@ -58,23 +58,23 @@ exports.create = function* (opt) {

exports.deleteAll = function* (opt) {
const github = opt.github;
const user = opt.repo.split('/')[0];
const repo = opt.repo.split('/')[1];
const owner = opt.owner;
const repo = opt.repo;

/*
Fetch all existing labels
*/

let labels = yield github.issues.getLabels({ user, repo });
let labels = yield github.issues.listLabelsForRepo({ owner, repo });

/*
Delete all existing labels
*/

if (labels.length) {
labels = labels.map(label => {
if (labels.data.length) {
labels = labels.data.map(label => {
return github.issues.deleteLabel({
user,
owner,
repo,
name: label.name,
});
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"dependencies": {
"co": "^4.6.0",
"commander": "^2.9.0",
"github": "^2.4.1",
"github-oauth-prompt": "~1.1.0"
"@octokit/rest": "^18.12.0"
},
"devDependencies": {
"eslint": "^3.3.0",
Expand All @@ -18,7 +17,7 @@
"repository": "[email protected]:popomore/github-labels.git",
"author": "popomore <[email protected]>",
"bin": {
"labels": "./bin/labels"
"labels": "DEBUG=octokit* ./bin/labels"
},
"license": "MIT"
}
Loading