-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Script.gs
44 lines (42 loc) · 1.55 KB
/
Script.gs
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
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('MLH Fellowship GitHub Invites')
.addItem('Invite All Fellows to GitHub', 'inviteFellows')
.addToUi();
}
function inviteFellows() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const values = sheet.getDataRange().getValues()
const headers = values[0];
const GITHUB_ACCESS_TOKEN = "FILL ME IN";
const GITHUB_USERNAME = "MLHProgram";
const GITHUB_ORG = "MLH-Fellowship"
const UsernameIndex = headers.indexOf('Fellow GitHub Username');
const StatusIndex = headers.indexOf('Status');
for (let i = 1; i < values.length; i++) {
var gh_username = values[i][0]
var id_url = 'https://api.github.com/users/' + encodeURIComponent(gh_username)
var id_response = UrlFetchApp.fetch(id_url);
Logger.log(id_response)
var data = JSON.parse(id_response.getContentText())
var auth = {
"Authorization": "Basic " + Utilities.base64Encode(GITHUB_USERNAME + ":" + GITHUB_ACCESS_TOKEN)
}
var options = {
"method": "post",
"headers": auth,
"contentType": 'application/json',
"payload": JSON.stringify({"invitee_id": data.id}),
}
Logger.log(options)
var invite_url = 'https://api.github.com/orgs/' + GITHUB_ORG + '/invitations'
try {
var invite_response = UrlFetchApp.fetch(invite_url, options)
Logger.log(invite_response)
sheet.getRange(i + 1, StatusIndex + 1).setValue("Success")
} catch (e) {
Logger.log(e)
sheet.getRange(i + 1, StatusIndex + 1).setValue(e)
}
}
}