forked from 99x/github-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (127 loc) · 3.72 KB
/
index.js
File metadata and controls
146 lines (127 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const GitHubApi = require("github");
const Promise = require('bluebird');
const inquirer = require('inquirer');
const github = new GitHubApi({
debug: false,
protocol: "https",
host: "api.github.com",
pathPrefix: "",
headers: {
"user-agent": "My-Cool-GitHub-App"
},
Promise: Promise,
followRedirects: false,
timeout: 5000
});
Promise.promisifyAll(github.issues);
Promise.promisifyAll(github.repos);
function getRepositories(user) {
return github.repos.getForUser({
user: user,
type: 'owner'
});
}
function createLabel(repository, owner, name, color) {
return github.issues.createLabel({
owner: owner,
repo: repository.name,
name: name,
color: color
});
}
function getIssues(repository, owner) {
return github.issues.getForRepo({
owner: owner,
repo: repository.name
}).each(issue => issue.repo = repository);
}
function addLabels(repository, issue, owner, labels) {
github.issues.addLabels({
owner: owner,
repo: repository.name,
number: issue.number,
body: labels
});
}
Promise.coroutine(function*() {
const {username, password} = yield inquirer.prompt([
{
type: 'input',
name: 'username',
message: 'Enter your GitHub username:'
},
{
type: 'password',
name: 'password',
message: 'Enter your GitHub password:'
}
]);
// This is synchronous
github.authenticate({
type: "basic",
username: username,
password: password
});
const {owner} = yield inquirer.prompt([
{
type: 'input',
name: 'owner',
message: 'Enter the owner of the repositories:'
}
]);
const repositories = yield getRepositories(owner);
const {selectedRepositories} = yield inquirer.prompt([
{
type: 'checkbox',
name: 'selectedRepositories',
choices: repositories.map(repo => ({name: repo.name, value: repo})),
message: 'Please select the repositories:'
}
]);
if(selectedRepositories.length == 0)
return;
const issues = yield Promise.all(selectedRepositories.map(repo => getIssues(repo, owner)))
.reduce((all, current) => all.concat(current));
const {selectedIssues} = yield inquirer.prompt([
{
type: 'checkbox',
name: 'selectedIssues',
choices: issues.map(issue => ({name: issue.title, value: issue})),
message: 'Please select the issues:'
}
]);
if(selectedIssues.length === 0)
return;
const label = yield inquirer.prompt([
{
type: 'input',
name: 'name',
message: 'Please enter the label name:'
},
{
type: 'input',
name: 'color',
message: 'Please select the color:',
validate: color => /^[0-9a-fA-F]{6}$/.test(color) || 'Input a color as a 6 digit hex code.'
}
]);
yield Promise.resolve(selectedRepositories)
.each(repo => createLabel(repo, owner, label.name, label.color))
.then(() => selectedIssues, () => selectedIssues)
.each(issue => addLabels(issue.repo, issue, owner, [label.name]))
})().catch(e => displayError(e));
function displayError(error) {
if(!error) {
console.log('Unknown error :/');
return;
}
let errorMessage = error;
if(error.message && typeof error.message == 'string') {
const json = JSON.parse(error.message);
if(json && json.message) {
errorMessage = json.message;
} else
errorMessage = error.message;
}
console.log(`Error: ${errorMessage}`);
}