forked from github/ghec-audit-log-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathghec-audit-log-client.js
50 lines (43 loc) · 1.31 KB
/
ghec-audit-log-client.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
const { allEntriesQuery } = require('./ghec-audit-log-queries')
async function requestEntries (requestExecutor, org, limit, cursor) {
let entries = []
const variables = {
org: org,
page: null
}
let hasNextPage = true
let firstPageCursorId = null
let foundCursor = false
const hasLimit = limit || false
let limitReached = false
while (hasNextPage && !foundCursor && !limitReached) {
const data = await requestExecutor(allEntriesQuery, variables)
let newEntries = data.organization.auditLog.nodes
// Cursor check
if (cursor != null) {
const index = newEntries.findIndex((elem) => elem.id === cursor)
if (index !== -1) {
newEntries = newEntries.slice(0, index)
foundCursor = true
}
}
entries = entries.concat(newEntries)
hasNextPage = data.organization.auditLog.pageInfo.hasNextPage
variables.page = data.organization.auditLog.pageInfo.endCursor
// Check limit
if (hasLimit) {
if (entries.length >= limit) {
entries = entries.slice(0, limit)
}
limitReached = true
}
// Store last cursor request
if (!firstPageCursorId && newEntries.length !== 0) {
firstPageCursorId = newEntries[0].id
}
}
return { data: entries, newestCursorId: firstPageCursorId }
}
module.exports = {
requestEntries
}