This repository has been archived by the owner on Sep 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathteams.gs
81 lines (71 loc) · 2.91 KB
/
teams.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
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
var fetchCachedGitHubTeamSlugs, getGitHubService, githubClientId, githubClientSecret, githubTeamSlugs, loadGitHubTeams, resetGitHubAuthorization;
githubTeamSlugs = false;
function fetchCachedGitHubTeamSlugs() {
var cache, cachedSlugs, slugs;
Logger.log("Trying to load GitHub teams from cache");
if (githubTeamSlugs) {
Logger.log("fetchCachedGitHubTeamSlugs: local cache hit");
} else {
cache = CacheService.getUserCache();
cachedSlugs = cache.get("github-team-slugs");
if (cachedSlugs) {
githubTeamSlugs = JSON.parse(cachedSlugs);
Logger.log("fetchCachedGitHubTeamSlugs: cache hit");
} else {
Logger.log("fetchCachedGitHubTeamSlugs: cache miss");
slugs = loadGitHubTeams();
if (slugs === "err") {
return "err";
}
githubTeamSlugs = slugs;
cache.put("github-team-slugs", JSON.stringify(slugs), 3600);
}
}
Logger.log(JSON.stringify(githubTeamSlugs));
return githubTeamSlugs;
};
function loadGitHubTeams() {
var githubService, response, t, teamSlugs, teams;
Logger.log("loadGitHubTeams: Loading GitHub teams from the API");
githubService = getGitHubService();
if (!githubService.hasAccess() || !githubService.getAccessToken()) {
Logger.log("loadGitHubTeams: Missing Oauth authorization");
return "err";
}
response = UrlFetchApp.fetch("https://api.github.com/user/teams", {
muteHttpExceptions: true,
headers: {
Authorization: "token " + (githubService.getAccessToken())
}
});
if (response.getResponseCode() !== 200) {
Logger.log("loadGitHubTeams: Non 200 response from the GitHub API");
return "err";
}
teams = JSON.parse(response.getContentText());
teamSlugs = (function() {
var i, len, results;
results = [];
for (i = 0, len = teams.length; i < len; i++) {
t = teams[i];
results.push("@" + t["organization"]["login"] + "/" + t["slug"]);
}
return results;
})();
Logger.log("loadGitHubTeams: Loaded teams from GitHub API.");
return teamSlugs;
};
function getGitHubService() {
return OAuth2.createService('github').setAuthorizationBaseUrl('https://github.com/login/oauth/authorize').setTokenUrl('https://github.com/login/oauth/access_token').setClientId(githubClientId()).setClientSecret(githubClientSecret()).setProjectKey('M0FjeQ5yKTbPoxSB2lDmzPjxjc-ysB9lY').setCallbackFunction('authCallback').setPropertyStore(PropertiesService.getUserProperties()).setScope('read:org');
};
function githubClientId() {
return PropertiesService.getScriptProperties().getProperty("github_client_id");
};
function githubClientSecret() {
return PropertiesService.getScriptProperties().getProperty("github_client_secret");
};
function resetGitHubAuthorization() {
var githubService;
githubService = getGitHubService();
return githubService.reset();
};