-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathinvitations.js
70 lines (56 loc) · 1.82 KB
/
invitations.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
'use strict';
const fetch = require('../fetch');
const figgy = require('figgy-pudding');
const { whoAmI } = require('../utils');
module.exports = invitations;
// usage: ds invitations namespace
// list all invitations for the given namespace, if the logged-in user has permission
const invitationsOpts = figgy({
argv: true,
registry: { default: 'https://registry.entropic.dev' },
registries: { default: [] },
token: true,
packages: true,
log: { default: require('npmlog') }
});
const getUrl = (packages, registry, invitee) =>
packages
? `${registry}/v1/namespaces/namespace/${invitee}/maintainerships?status=pending`
: `${registry}/v1/users/user/${invitee}/memberships?status=pending`;
async function invitations(opts) {
opts = invitationsOpts(opts);
let invitee = opts.argv[0] || (await whoAmI(opts));
if (!invitee) {
console.log('Usage: ds invitations <namespace|user> [--packages]');
process.exit(1);
}
if (!invitee.includes('@')) {
invitee += '@' + opts.registry.replace(/^https?:\/\//, '');
}
const response = await fetch(getUrl(opts.packages, opts.registry, invitee), {
headers: { authorization: `Bearer ${opts.token}` }
});
const pkg = await response.json();
let result = [];
if (Array.isArray(pkg.objects)) {
result = pkg.objects;
}
const qualifier = opts.packages ? 'package ' : '';
if (result.length === 0) {
console.log(`${invitee} has no ${qualifier}invitations.`);
return 0;
}
console.log(
`${invitee} has ` +
(result.length === 1
? `one ${qualifier}invitation.`
: `${result.length} ${qualifier}invitations.`) +
'\nTo accept:\n'
);
result.forEach(dest => {
console.log(` ds join ${dest.name} --as ${invitee}`);
});
console.log(
`\nTo decline an invitation: ds decline <group|package> --as ${invitee}`
);
}