-
Notifications
You must be signed in to change notification settings - Fork 1
/
gh-org-members.js
209 lines (175 loc) · 6.98 KB
/
gh-org-members.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
const ins = require("util").inspect;
const shell = require('shelljs');
const { Command } = require('commander');
const { getMembers, showError, setDefaultOrg, fzfGetOrg } = require('@crguezl/gh-utilities');
function writeMembers(members, options, csvKeys) {
if (regexp) {
members = members.filter(m => {
let someFieldMatch = false;
//console.log(`${m.login}`);
for(let k of Object.keys(m)) {
//console.log(k);
if (regexp.test(m[k])) {
someFieldMatch = true;
break;
}
}
return someFieldMatch;
});
}
if (options.json) {
console.log(JSON.stringify(members, null, 2));
process.exit(0);
}
let set = new Set(Object.keys(options));
set.delete('regexp');
set.delete('json');
set.delete('org');
set.delete('debug');
members.forEach(x => {
let output = [];
for (opt of set) {
//console.log(opt);
if (opt !== 'csv') output.push(`"${x[opt]}"`);
else { // opt is csv
if (Array.isArray(options.csv)) {
options.csv.forEach(c => {
if (x[c]) output.push(`"${x[c]}"`);
})
} else {
csvKeys.forEach(c => {
if (x[c]) output.push(`"${x[c]}"`);
});
}
}
}
console.log(output.join(","));
});
process.exit(0)
}
function main(members, options, org) {
// read the csv file and decorate the filtered members with the additional info
// Get the teams of those members and decorate the result with the team info
// Decorate: add site
members.forEach(x => x.site = `https://${x.login}.github.io`)
members.map(x => x.orgurl = `https://github.com/orgs/${org}/people/${x.login}`)
members.forEach(x => x.fullname = `${x?.name || x?.login}`);
let csvFilePath;
if (options.csv) {
if (options.pathcsv) {
csvFilePath = options.pathcsv;
} else { // Get the last file matching /${org}.*\.csv/ in the downloads folder
const downloadsFolder = require('downloads-folder');
const path = require('path');
const fs = require('fs');
var downloadsDir = downloadsFolder(); // your downloads directory
let escapedOrg = org.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
let csvFileRegexp = new RegExp(`^${escapedOrg}.*\\.csv$`);
var files = fs.readdirSync(downloadsFolder()).filter(file => file.match(csvFileRegexp)).map(file => path.join(downloadsDir, file));
//console.log(files);
if (files.length === 0) {
console.error(`No csv file matching regexp pattern /${regexp.source}/ found for organization "${org}" in folder "${downloadsDir}"`);
process.exit(1);
}
// sort by date
files.sort((a, b) => fs.statSync(a).mtime.getTime()-fs.statSync(b).mtime.getTime());
//console.log(files);
csvFilePath = files.pop(); // get the last file matching the org name in the downloads folder
}
//console.log(csvFilePath);
const csv=require('csvtojson');
csv()
.fromFile(csvFilePath)
.then((jsonObj)=>{
// console.log(JSON.stringify(jsonObj, null, 2));
let csvKeys = Object.keys(jsonObj[0]);
jsonObj.forEach((x, j) => {
if (x.login && x.login.length) {
let xfound = false;
members.forEach(m => {
if (m.login === x.login) {
//console.log(`${m.login},${x.id}`);
xfound = true;
if (x.id) m.id = x.id;
//console.log(options.csv)
if (Array.isArray(options.csv)) {
options.csv.forEach(c => {
m[c] = x[c];
}
)
} else {
m = Object.assign(m, x);
}
}
});
if (!xfound) console.error(`Spreadsheet entry:\n"${JSON.stringify(x)}"\nnot found in GitHub organization ${org}!`);
}
});
writeMembers(members, options, csvKeys);
})
.catch(err =>{
console.error(`Error trying to read file "${csvFilePath}"\n${err}`);
process.exit(1);
})
} else {
writeMembers(members, options, null);
};
} // end main
const program = new Command();
program.version(require('./package.json').version);
program
.name("gh org-members")
.usage("[options] [organization]")
.option('-f, --fullname', 'show name of the user (if available)')
.option('-j, --json', 'returns the full json object')
.option('-r, --regexp <regexp>', 'Only members with some field matching <regexp> will be shown')
.option('-u, --url', 'show github user url')
.option('-l, --login', 'show github user login')
.option('-w, --orgurl', 'show github user url as a member of the org')
.option('-s, --site', 'show url of the members github pages web sites')
.option('-c, --csv [field...]', 'shows the values of the fields of the organization csv')
.option('-p, --pathcsv <csv file>', 'path to the csv file')
.option('-o --org <org>', 'default organization')
.option(' --default', 'Set selected "org" as default organization for future uses');
program.addHelpText('after', `
- If the organization is not explicitly specified or there is a default org,
the selection will be done interactively among the list of your organizations using 'fzf'
- You can set the default organization through the "--default" option for future uses of this program
- When in 'fzf', use CTRL-A to select all, tab to select/deselect
- You can merge the results of the GitHub API info with info from info in a '.csv' file using the "-c" and "-p" options. For instance: "gh org-members -jr sara -c -p ./ULL-MFP-AET-2122.csv"
- If the option '-c' is used but the '.csv' file is not specified via the '-p' option, it will use the most recent '*.csv' file in your 'Downloads' folder mathching the regular expression pattern '/<org>.*\.csv/' where 'org' refers to the specified or default organization
- When using '-c' it can be followed by any list of field names in the '.csv' file.
- The '.csv' file has to have a column named 'login' having the Github login of the members
`
);
program.parse(process.argv);
const options = program.opts();
if (Object.keys(options).length === 0) {
program.help();
}
const deb = (...args) => {
console.error(ins(...args, { depth: null }));
};
// deb(options);
if (!shell.which('git')) {
showError('Sorry, this extension requires git installed!');
}
if (!shell.which('gh')) {
showError('Sorry, this extension requires GitHub Cli (gh) installed!');
}
debugger;
if (!options.org && (program.args.length == 1)) options.org = program.args[0];
let org = options.org || fzfGetOrg() || process.env["GITHUB_ORG"];
if (options.default) setDefaultOrg(org);
let regexp = /./;
if (options.regexp) {
regexp = new RegExp(options.regexp, 'i');
}
if (!org) { // set org to the owner of the current repo
let r = shell.exec(`gh browse -n`);
if (r.code !== 0) program.help();
org = r.stdout.split('/').slice(-2)[0];
//deb(org);
}
let members = getMembers(org);
main(members, options, org);