-
Notifications
You must be signed in to change notification settings - Fork 0
/
user-access-report.js
120 lines (112 loc) · 3.48 KB
/
user-access-report.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
require('dotenv').config()
const argv = require('yargs').argv
const fs = require('fs')
let { graphql } = require('@octokit/graphql')
graphql = graphql.defaults({
baseUrl: process.env.GITHUB_ENDPOINT,
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`
}
})
const outputBaseFolder = process.env.OUTPUT_FOLDER
// Full path and name of output file to create
const outputFile = `${outputBaseFolder}/user-${argv.user}-report-${Date.now()}.csv`
if (argv.user.length > 0) {
createCSV()
getUserAccess()
} else {
console.log('Invalid options passed\n')
console.log('To use this script, you must specify a user: ')
console.log('node user-access-report.js --user <username>\n')
}
async function getUserAccess () {
let paginationOrg = null
let paginationRepo = null
const query =
`query ($enterprise: String! $user: String! $cursorOrg: String $cursorRepo: String) {
enterprise(slug: $enterprise) {
organizations(first: 100 after: $cursorOrg) {
pageInfo {
hasNextPage
endCursor
}
nodes {
login
repositories(first:100 after: $cursorRepo) {
pageInfo {
hasNextPage
endCursor
}
nodes {
name
collaborators(first:1 query: $user){
edges {
permission
node {
login
}
}
}
}
}
}
}
}
}`
try {
let hasNextPageOrg = false
let hasNextPageRepo = false
let getUserAccessResult = null
do {
do {
getUserAccessResult = await graphql({
query,
enterprise: process.env.ENTERPRISE,
user: argv.user,
cursorOrg: paginationOrg,
cursorRepo: paginationRepo
})
hasNextPageOrg = getUserAccessResult.enterprise.organizations.pageInfo.hasNextPage
const orgsObj = getUserAccessResult.enterprise.organizations.nodes
let orgNode = 0
for (const org of orgsObj) {
const reposObj = getUserAccessResult.enterprise.organizations.nodes[orgNode].repositories.nodes
hasNextPageRepo = getUserAccessResult.enterprise.organizations.nodes[orgNode].repositories.pageInfo.hasNextPage
if (Object.keys(reposObj).length > 0) {
for (const repo of reposObj) {
const orgName = org.login
const repoName = repo.name
const accessLevel = repo.collaborators.edges[0].permission
addToCSV(orgName, repoName, accessLevel.toLowerCase())
}
if (hasNextPageRepo) {
paginationRepo = getUserAccessResult.enterprise.organizations.nodes[orgNode].repositories.pageInfo.endCursor
} else {
paginationRepo = null
}
}
orgNode++
}
} while (hasNextPageRepo)
if (hasNextPageOrg) {
paginationOrg = getUserAccessResult.enterprise.organizations.pageInfo.endCursor
}
} while (hasNextPageOrg)
} catch (error) {
console.log('Request failed:', error.request)
console.log(error.message)
console.log(error)
}
}
function createCSV () {
if (!fs.existsSync(outputBaseFolder)) {
fs.mkdirSync(outputBaseFolder)
}
const header = 'Organization,Repository,AccessLevel'
fs.appendFileSync(outputFile, header + '\n', err => {
if (err) return console.log(err)
})
}
function addToCSV (org, repo, access) {
fs.appendFileSync(outputFile, `${org},${repo},${access}\n`)
}