-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
132 lines (115 loc) · 3.75 KB
/
index.ts
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
/**
* Copyright Zendesk, Inc.
*
* Use of this source code is governed under the Apache License, Version 2.0
* found at http://www.apache.org/licenses/LICENSE-2.0.
*/
import { GraphQlQueryResponseData, graphql } from '@octokit/graphql';
import { repository as getRepository, token as getToken } from '../index.js';
import { handleErrorMessage, handleSuccessMessage } from '../../utils/index.js';
import { Command } from 'commander';
import { Octokit } from '@octokit/rest';
import { Ora } from 'ora';
interface IGitHubTeamsArgs {
org?: string;
path?: string;
user?: string | boolean;
token?: string;
spinner?: Ora;
}
/**
* Execute the `github-teams` command.
*
* @param {string} [args.org] GitHub organization name.
* @param {string} [args.user] GitHub user name or `true` for current user.
* @param {string} [args.path] Path to a git directory.
* @param {string} [args.token] GitHub personal access token.
* @param {string} [args.spinner] Terminal spinner.
*
* @returns {Promise<string[]>} A list of GitHub teams.
*/
export const execute = async (args: IGitHubTeamsArgs): Promise<string[] | undefined> => {
let retVal: string[] | undefined;
try {
const auth = args.token || (await getToken(args.spinner));
const github = new Octokit({ auth });
const org = (args.org || (await getRepository(args.path, args.spinner))?.owner)!;
if (args.user) {
let userLogin;
if (args.user === true) {
/* https://docs.github.com/en/rest/users/users#get-the-authenticated-user */
const currentUser = await github.users.getAuthenticated();
userLogin = currentUser.data.login;
} else {
userLogin = args.user;
}
/* https://docs.github.com/en/graphql/reference/objects#teamconnection */
const userTeams: GraphQlQueryResponseData = await graphql(
`
query userTeams($login: String!, $userLogin: String!) {
organization(login: $login) {
teams(first: 100, userLogins: [$userLogin]) {
edges {
node {
name
}
}
}
}
}
`,
{
login: org,
userLogin,
headers: {
authorization: `token ${auth}`
}
}
);
retVal = userTeams.organization.teams.edges
.map((edge: { node: { name: string } }) => edge.node.name)
.sort();
} else {
/* https://octokit.github.io/rest.js/v17#teams-list */
const teams = await github.teams.list({ org });
retVal = teams.data.map(team => team.name).sort();
}
} catch (error: unknown) {
handleErrorMessage(error, 'github-teams', args.spinner);
throw error;
}
return retVal;
};
export default (spinner: Ora): Command => {
const command = new Command('github-teams');
return command
.description('output GitHub organization teams')
.argument('[org]', 'GitHub organization name; defaults to repository owner')
.option('-u, --user [user]', 'user to get team membership for; defaults to current')
.option('-p, --path <path>', 'git directory')
.option('-t, --token <token>', 'access token')
.action(async org => {
try {
spinner.start();
const options = command.opts();
const teams = await execute({
org,
user: options.user,
path: options.path,
token: options.token,
spinner
});
if (teams) {
const message = teams.join(', ');
handleSuccessMessage(message, spinner);
} else {
throw new Error();
}
} catch {
spinner.fail('GitHub teams not found');
process.exitCode = 1;
} finally {
spinner.stop();
}
});
};