Fix lint issues
This commit is contained in:
Родитель
d62398af6c
Коммит
d63c6181f4
|
@ -34,6 +34,36 @@ try {
|
|||
// TODO idea: maybe add support for other formats like PUTVAL to forward the data in an easier way
|
||||
const { cursor, pretty, limit, api, apiType, token, org, outputFile } = validateInput(program, config)
|
||||
|
||||
function buildV3Octokit () {
|
||||
const Octo = Octokit.plugin(retry, throttling)
|
||||
const octokit = new Octo({
|
||||
auth: token,
|
||||
throttle: {
|
||||
onRateLimit: (retryAfter, _) => {
|
||||
octokit.log.warn(
|
||||
`[${new Date().toISOString()}] ${program} Request quota exhausted for request, will retry in ${retryAfter}`
|
||||
)
|
||||
return true
|
||||
},
|
||||
onAbuseLimit: (retryAfter, _) => {
|
||||
octokit.log.warn(
|
||||
`[${new Date().toISOString()}] ${program} Abuse detected for request, will retry in ${retryAfter}`
|
||||
)
|
||||
return true
|
||||
}
|
||||
}
|
||||
})
|
||||
return octokit
|
||||
}
|
||||
|
||||
function buildGraphQLOctokit () {
|
||||
return graphql.defaults({
|
||||
headers: {
|
||||
authorization: `token ${token}`
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Function containing the GitHub API v4 Graphql calls for the audit log
|
||||
*/
|
||||
|
@ -42,44 +72,21 @@ async function queryAuditLog () {
|
|||
let queryRunner
|
||||
switch (api) {
|
||||
case 'v4': // API v4 call with cursor
|
||||
const graphqlWithAuth = graphql.defaults({
|
||||
headers: {
|
||||
authorization: `token ${token}`
|
||||
}
|
||||
})
|
||||
queryRunner = () => requestV4Entries(graphqlWithAuth, org, limit, cursor || null)
|
||||
break;
|
||||
queryRunner = () => requestV4Entries(buildGraphQLOctokit(), org, limit, cursor || null)
|
||||
break
|
||||
case 'v3': // API v3 call with cursor
|
||||
const Octo = Octokit.plugin(retry, throttling)
|
||||
const octokit = new Octo({
|
||||
auth: token,
|
||||
throttle: {
|
||||
onRateLimit: (retryAfter, _) => {
|
||||
octokit.log.warn(
|
||||
`[${new Date().toISOString()}] ${program} Request quota exhausted for request, will retry in ${retryAfter}`
|
||||
)
|
||||
return true
|
||||
},
|
||||
onAbuseLimit: (retryAfter, _) => {
|
||||
octokit.log.warn(
|
||||
`[${new Date().toISOString()}] ${program} Abuse detected for request, will retry in ${retryAfter}`
|
||||
)
|
||||
return true
|
||||
}
|
||||
}
|
||||
})
|
||||
queryRunner = () => requestV3Entries(octokit, org, limit, cursor || null, apiType)
|
||||
break;
|
||||
queryRunner = () => requestV3Entries(buildV3Octokit(), org, limit, cursor || null, apiType)
|
||||
break
|
||||
}
|
||||
|
||||
// Sanity check the switch
|
||||
if(!queryRunner) return []
|
||||
if (!queryRunner) return []
|
||||
|
||||
// Run the query and store the most recent cursor
|
||||
const { data, newestCursorId } = await queryRunner()
|
||||
const entries = data
|
||||
if (newestCursorId) {
|
||||
const cursorFileName = `.last-${api === 'v3' ? 'v3-': '-'}cursor-update`
|
||||
const cursorFileName = `.last-${api === 'v3' ? 'v3-' : '-'}cursor-update`
|
||||
fs.writeFileSync(cursorFileName, newestCursorId)
|
||||
}
|
||||
|
||||
|
@ -105,4 +112,4 @@ queryAuditLog()
|
|||
.catch((err) => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -57,9 +57,9 @@ async function requestV3Entries (octokit, org, limit, cursor, apiType) {
|
|||
for await (const { data } of octokit.paginate.iterator(`GET /orgs/{org}/audit-log?include=${apiType}&per_page=${Math.min(100, limit)}`, {
|
||||
org: org
|
||||
})) {
|
||||
let newEntries = data;
|
||||
let newEntries = data
|
||||
|
||||
//If we find the entry in the current request, we should add the remaining and stop
|
||||
// If we find the entry in the current request, we should add the remaining and stop
|
||||
if (cursor != null) {
|
||||
const index = findHashedEntry(cursor, data)
|
||||
if (index !== -1) {
|
||||
|
@ -80,12 +80,12 @@ async function requestV3Entries (octokit, org, limit, cursor, apiType) {
|
|||
}
|
||||
|
||||
// Stop going through the iterator if either we reached limit or found the cursor
|
||||
if(foundLimit || foundCursor) break
|
||||
if (foundLimit || foundCursor) break
|
||||
}
|
||||
|
||||
//Calculate the newest element that was provided
|
||||
// Calculate the newest element that was provided
|
||||
let lastCursor = null
|
||||
if(entries.length > 0) {
|
||||
if (entries.length > 0) {
|
||||
lastCursor = generateHashAudit(entries[0])
|
||||
}
|
||||
|
||||
|
@ -93,16 +93,16 @@ async function requestV3Entries (octokit, org, limit, cursor, apiType) {
|
|||
return { data: entries, newestCursorId: lastCursor }
|
||||
}
|
||||
|
||||
function generateHashAudit(entry) {
|
||||
function generateHashAudit (entry) {
|
||||
const hashed = hash.digest(entry)
|
||||
return Buffer.from(hashed).toString('base64')
|
||||
}
|
||||
|
||||
function findHashedEntry(cursor, entries) {
|
||||
function findHashedEntry (cursor, entries) {
|
||||
return entries.findIndex((elem) => generateHashAudit(elem) === cursor)
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
requestV4Entries,
|
||||
requestV3Entries,
|
||||
requestV3Entries
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче