-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
executable file
·231 lines (202 loc) · 5.24 KB
/
cli.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env node
const program = require("commander");
const chalk = require("chalk");
const _ = require("lodash");
var inquirer = require("inquirer");
const Octokit = require("@octokit/rest");
const dotenv = require("dotenv");
const state = {
teamExists: false,
teamId: 0,
teamUrl: null,
teamSlug: null
};
var octokit;
dotenv.config();
program.option(
"-t, --token <PAT>",
"Your GitHub PAT (leave blank for prompt or set $GH_PAT)",
process.env.GH_PAT
);
program.option(
"-u, --user <username>",
"Your GitHub username (leave blank for prompt or set $GH_USER)",
process.env.GH_USER
);
program.option(
"-o, --org <organization>",
"Organization name (leave blank for prompt or set $GH_ORG)",
process.env.GH_ORG
);
program.option("-s, --slug <team-slug>");
program.parse(process.argv);
const die = msg => {
const ui = new inquirer.ui.BottomBar();
ui.log.write(`${chalk.red("[ERROR]")} ${msg}`);
process.exit(1);
};
const findTeam = async ({ owner, org, team }) => {
const ui = new inquirer.ui.BottomBar();
ui.log.write(
`${chalk.dim("[1/3]")} Verifying team ${chalk.green(
`${org}/${team}`
)} exists...`
);
try {
var { data } = await octokit.teams.getByName({ org, team_slug: team });
} catch (e) {
if (e.status === 404) {
state.teamExists = false;
} else {
die(e.message);
}
}
if (
_.get(data, "organization", false) &&
_.get(data, "organization.login", false) === org
) {
state.teamExists = true;
state.teamUrl = data.html_url;
state.teamSlug = data.slug;
}
if (state.teamExists) {
ui.log.write(`${chalk.dim("[2/3]")} Team ${chalk.green(team)} found.`);
}
};
async function createTeam({ owner, org, team }) {
if (state.teamExists) return;
const ui = new inquirer.ui.BottomBar();
ui.log.write(`${chalk.dim("[2/3]")} Team ${chalk.green(team)} not found. `);
await inquirer
.prompt([
{
type: "confirm",
name: "createTeam",
message: `Create a new team now?`
},
{
type: "input",
name: "newTeamName",
message: "Name of the team",
default: () => team,
when: ({ createTeam }) => createTeam
},
{
type: "input",
name: "teamDescription",
message: "Team description",
when: function({ createTeam }) {
return createTeam;
}
}
])
.then(async function({ createTeam, newTeamName, teamDescription }) {
if (!createTeam) {
process.exit();
} else {
try {
var {
data: { id, html_url, slug }
} = await octokit.teams.create({
org,
name: newTeamName,
privacy: "secret",
description: teamDescription
});
state.teamExists = true;
state.teamUrl = html_url;
state.teamSlug = slug;
state.teamId = id;
} catch (e) {
die(e.message);
}
}
});
}
async function inviteMembers({ org, team }) {
const ui = new inquirer.ui.BottomBar();
await inquirer
.prompt([
{
type: "editor",
name: "csv",
message: "Provide a comma separated list of usernames or email"
}
])
.then(async ({ csv }) => {
const invitees = csv.split(",").map(i => i.trim());
ui.log.write(
`${chalk.dim("[3/3]")} Sending invitation to ${chalk.yellow(
invitees.length
)} users:`
);
ui.log.write(chalk.yellow("- " + invitees.join("\n- ")));
const invites = await invitees.reduce(async (promisedRuns, i) => {
const memo = await promisedRuns;
if (i.indexOf("@") > -1) {
// assume valid email
const res = await octokit.orgs.createInvitation({
org,
team_ids: [state.teamId],
email: i
});
} else {
// assume valid username
const res = await octokit.teams.addOrUpdateMembershipInOrg({
org,
team_slug: state.teamSlug,
username: i
});
}
// TODO what to return?
return memo;
}, []);
ui.log.write(`${chalk.dim("[OK]")} Done. Review invitations at:`);
ui.log.write(state.teamUrl);
});
}
inquirer
.prompt([
{
type: "password",
name: "PAT",
message: "What's your GitHub PAT?",
default: () => program.token
},
{
type: "input",
name: "owner",
message: "Your username?",
default: () => program.user
},
{
type: "input",
name: "org",
message: "Which organization?",
default: () => program.org
},
{
type: "input",
name: "team",
message: "Which team?",
suffix:
" (provide the slug of an existing team, or the full name of the team being created)",
validate: function(value) {
return value.length > 3
? true
: "Please provide at least 4 characters.";
},
default: function() {
return program.team;
}
}
])
.then(async function(answers) {
octokit = new Octokit({
auth: answers.PAT
});
await findTeam({ ...answers });
await createTeam({ ...answers });
await inviteMembers({ ...answers });
process.exit();
});