-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-blast
executable file
·102 lines (83 loc) · 2.48 KB
/
git-blast
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
#!/usr/bin/env node
const https = require('https');
const { execSync } = require('child_process');
const twitterUsernames = {};
const githubRequest = async (path) => {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.github.com',
port: 443,
path: path,
method: 'GET',
headers: { 'User-Agent': 'git-blast' },
};
https
.get(options, (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
// console.log(data);
resolve(JSON.parse(data));
});
})
.on('error', (err) => {
console.log('Error: ' + err.message);
reject(err);
});
});
};
const getTwitterByEmail = async (email) => {
if (twitterUsernames[email] || twitterUsernames[email] === null) {
return twitterUsernames[email];
} else {
const searchResults = await githubRequest(`/search/users?q=${encodeURIComponent(email)}`);
if (searchResults && searchResults.items && searchResults.items.length > 0) {
const githubLogin = searchResults.items[0].login;
if (githubLogin) {
const user = await githubRequest(`/users/${githubLogin}`);
if (user && user.twitter_username) {
twitterUsernames[email] = user.twitter_username;
return user.twitter_username;
}
} {
twitterUsernames[email] = null;
}
} else {
twitterUsernames[email] = null;
}
}
};
const gitBlame = (args) => {
try {
const results = execSync(`git blame -e ${args.join(' ')}`);
return results.toString();
} catch (error) {
console.log(error);
}
};
const emailToTwitter = async (blameOutput) => {
const lines = blameOutput.split('\n');
for (const line of lines) {
const matches = line.match(/<(.+@.+)>/);
if (matches && matches[1] && !matches[1].startsWith('not.committed.yet')) {
const email = matches[1];
const twitter = await getTwitterByEmail(email);
if (twitter) {
const twitterUsername = `<@${twitter}>`.padEnd(email.length + 2);
console.log(line.replace(/<.+@.+>/, twitterUsername));
} else if(line) {
console.log(line);
}
} else if (line) {
console.log(line);
}
}
};
(async () => {
const args = process.argv.slice(2);
await emailToTwitter(gitBlame(args));
})();