forked from JamilOmar/github-reports
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.service.js
More file actions
129 lines (116 loc) · 3.55 KB
/
Copy pathgithub.service.js
File metadata and controls
129 lines (116 loc) · 3.55 KB
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
const { Octokit } = require("octokit");
const PAGE_SIZE = 50;
class GithubService {
constructor(authToken) {
this.octokit = new Octokit({
auth: authToken,
});
}
async getRepos(org) {
const iterator = this.octokit.paginate.iterator(
this.octokit.rest.repos.listForOrg, {
org: org,
per_page: PAGE_SIZE,
});
const result = [];
for await (const { data: repos } of iterator) {
for (const repo of repos) {
result.push({ name: repo.name, branch: repo.default_branch });
}
}
return result;
}
async getCollaborators(org, repo, outsideCollaborators) {
const iterator = this.octokit.paginate.iterator(
this.octokit.rest.repos.listCollaborators,
{
owner: org,
repo: repo,
per_page: PAGE_SIZE,
}
);
const result = [];
for await (const { data: collaborators } of iterator) {
for (const collaborator of collaborators) {
const username = collaborator.login;
const isOutside = (outsideCollaborators != undefined) ? outsideCollaborators.find((x => x.username == username)) != undefined : undefined
result.push({
username,
url: collaborator.url,
type: collaborator.type,
isOutside,
siteAdmin: collaborator.site_admin,
role: collaborator.role_name,
repo,
org
});
}
}
return result;
}
async getOutsideCollaborators(org) {
const iterator = this.octokit.paginate.iterator(
this.octokit.rest.orgs.listOutsideCollaborators,
{
org,
per_page: PAGE_SIZE,
}
);
const result = [];
for await (const { data: collaborators } of iterator) {
for (const collaborator of collaborators) {
result.push({
username: collaborator.login,
url: collaborator.url,
type: collaborator.type,
isOutside: true,
siteAdmin: collaborator.site_admin,
role: collaborator.role_name,
repo: "",
org
});
}
}
return result;
}
async getReposWithCollaborators(org) {
const respositories = await this.getRepos(org);
const outsideCollaborators = await this.getOutsideCollaborators(org);
const result = [];
for (const repo of respositories) {
let collaborators = await this.getCollaborators(org, repo.name, outsideCollaborators);
for (let collaborator of collaborators) {
result.push(collaborator)
}
}
return result;
}
async getSingleRepoWithCollaborators(org, repo) {
const outsideCollaborators = await this.getOutsideCollaborators(org);
return await this.getCollaborators(org, repo, outsideCollaborators);
}
async getReposBranchProtection(org, verbose = false) {
const respositories = await this.getRepos(org);
const results = [];
for (const repo of respositories) {
try {
const ret = await this.octokit.request('GET /repos/{owner}/{repo}/branches/{branch}/protection', {
owner: org,
repo: repo.name,
branch: repo.branch
})
if (verbose) { console.log(`Got branch protection ${repo.name}}`); }
results.push({
name: repo.name,
branch: repo.branch,
protection: ret.data
});
} catch (error) {
if (verbose) { console.log(`Failed to get branch protection for ${repo.name}}`); }
}
}
if (verbose) { console.log(`Got branch protection for ${results.length} repos`); }
return results;
}
}
module.exports = GithubService;