-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusers.js
executable file
·99 lines (83 loc) · 3.28 KB
/
users.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
#!/usr/bin/env node
var https = require('https');
var async = require('async');
var _ = require('lodash');
var childProcess = require('child_process');
var handleError = require('./lib/handle-error');
var gitConfig = require('./lib/git-config');
// User types to set in git config
var types = ['user', 'author'];
// Parse command line input
var inputs = process.argv.slice(2);
// Gets the installed scope for git-pair (global or local)
gitConfig.get('git-pair.scope', function(err, scope) {
if (scope !== 'global' && scope !== 'local') {
return console.error('Error: git-pair not installed correctly, please reinstall');
}
// If any users have been specified, set git users
if (inputs.length) {
if (inputs.length > 2) return console.error('Error: Cannot use more than 2 users');
// Get usernames from github
async.map(inputs, function(input, next) {
var parts = input.split(':');
if (parts.length == 1) {
return requestGitUsername(input, next);
}
if (parts.length == 2) {
return next(null, parts[0]);
}
next('Error: ' + input + ' should use the format username:email');
}, function(err, usernames) {
if (err) return console.error(err);
var users = inputs.map(function(input, i) {
return { name: usernames[i], email: _.last(input.split(':')), type: types[i] };
});
// Detect invalid users
if (_.any(users, function(user) { return !user.name || !user.email; })) {
return console.error('Error: invalid user ' + user.name + ':' + user.email);
}
gitConfig.clearUsers(scope, types, function(err) {
if (err) return console.error(err);
gitConfig.setUsers(scope, users, function(err) {
if (err) return console.error(err);
console.log('Set users to: ' + usernames.join(' and '));
});
});
});
}
// Otherwise get git users
else {
gitConfig.getUsers(scope, types, function(err, users) {
if (err) return console.error('Error: Couldn\'t read git config');
console.log('Configured users:');
users.forEach(function(user) {
console.log(user.name + ' <' + user.email + '>');
});
});
}
});
// Get usernames from github
function requestGitUsername(email, next) {
var options = {
hostname: 'api.github.com',
path: '/search/users?q=' + email + '+in:email',
headers: { 'User-Agent': 'Node' }
};
var req = https.request(options, function(res) {
var json = '';
res.on('data', function(chunk) {
json += chunk;
});
res.on('end', function() {
var data = JSON.parse(json);
if (data.message && data.message.match(/^API rate limit exceeded/)) {
return next('Error: Github API rate limit exceeded');
}
if (data.items.length === 0) {
return next('Error: Email account is not a public email account for a Github user');
}
next(null, data.items[0].login);
})
res.on('error', next);
}).end();
};