This commit is contained in:
Javier de Pedro López 2021-03-02 18:55:16 +01:00
Родитель d62398af6c
Коммит d63c6181f4
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: F43A23D4F89CCC6F
2 изменённых файлов: 45 добавлений и 38 удалений

Просмотреть файл

@ -34,22 +34,7 @@ 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 containing the GitHub API v4 Graphql calls for the audit log
*/
async function queryAuditLog () {
// Select the query to run
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;
case 'v3': // API v3 call with cursor
function buildV3Octokit () {
const Octo = Octokit.plugin(retry, throttling)
const octokit = new Octo({
auth: token,
@ -68,18 +53,40 @@ async function queryAuditLog () {
}
}
})
queryRunner = () => requestV3Entries(octokit, org, limit, cursor || null, apiType)
break;
return octokit
}
function buildGraphQLOctokit () {
return graphql.defaults({
headers: {
authorization: `token ${token}`
}
})
}
/**
* Function containing the GitHub API v4 Graphql calls for the audit log
*/
async function queryAuditLog () {
// Select the query to run
let queryRunner
switch (api) {
case 'v4': // API v4 call with cursor
queryRunner = () => requestV4Entries(buildGraphQLOctokit(), org, limit, cursor || null)
break
case 'v3': // API v3 call with cursor
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)
}

Просмотреть файл

@ -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
}