From d63c6181f48cef829e7e81716637f93b2f3148ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20de=20Pedro=20L=C3=B3pez?= Date: Tue, 2 Mar 2021 18:55:16 +0100 Subject: [PATCH] Fix lint issues --- ghec-audit-log-cli.js | 67 ++++++++++++++++++++++------------------ ghec-audit-log-client.js | 16 +++++----- 2 files changed, 45 insertions(+), 38 deletions(-) diff --git a/ghec-audit-log-cli.js b/ghec-audit-log-cli.js index dfe138c..af994d2 100755 --- a/ghec-audit-log-cli.js +++ b/ghec-audit-log-cli.js @@ -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) - }) \ No newline at end of file + }) diff --git a/ghec-audit-log-client.js b/ghec-audit-log-client.js index a042f83..ef9450d 100644 --- a/ghec-audit-log-client.js +++ b/ghec-audit-log-client.js @@ -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 }