Replace cursor value for cursor id

This commit is contained in:
Javier de Pedro López 2020-05-15 14:33:16 +02:00
Родитель 0b0ac45654
Коммит db75fc8e8c
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: F43A23D4F89CCC6F
3 изменённых файлов: 24 добавлений и 48 удалений

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

@ -1,42 +1,35 @@
const {allEntriesQuery, newEntriesQuery} = require('./ghec-audit-log-queries');
const {allEntriesQuery} = require('./ghec-audit-log-queries');
async function requestAllEntries(requestExecutor, org){
async function requestEntries(requestExecutor, org, cursor){
let entries = [];
let variables = {
"org": org,
"page": null,
};
let firstPageCursor = null;
let hasNextPage = true;
while(hasNextPage) {
let firstPageCursorId = null;
let foundCursor = false;
while(hasNextPage && !foundCursor) {
const data = await requestExecutor(allEntriesQuery, variables);
entries = entries.concat(data.organization.auditLog.nodes);
let newEntries = data.organization.auditLog.nodes;
if(cursor != null){
let 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;
if(!firstPageCursor) firstPageCursor = data.organization.auditLog.pageInfo.startCursor
if(!firstPageCursorId && newEntries.length !== 0) {
firstPageCursorId = newEntries[0].id
}
}
return {data: entries, newestCursor: firstPageCursor};
}
async function requestNewestEntries(requestExecutor, org, cursor) {
let entries = [];
let variables = {
"org": org,
"page": cursor,
};
let hasPreviousPage = true;
while(hasPreviousPage) {
const data = await requestExecutor(newEntriesQuery, variables);
entries = entries.concat(data.organization.auditLog.nodes);
hasPreviousPage = data.organization.auditLog.pageInfo.hasPreviousPage;
variables.page = data.organization.auditLog.pageInfo.startCursor;
}
return {data: entries, newestCursor: variables.page};
return {data: entries, newestCursorId: firstPageCursorId};
}
module.exports = {
requestNewestEntries,
requestAllEntries
requestEntries
};

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

@ -2,7 +2,7 @@
const YAML = require('yaml');
const fs = require('fs');
const {graphql} = require('@octokit/graphql');
const {requestNewestEntries, requestAllEntries} = require('./ghce-audit-log-client');
const {requestEntries} = require('./ghce-audit-log-client');
//---- Obtain configuration
const { program } = require('commander');
@ -42,15 +42,15 @@ async function queryAuditLog() {
// Select the query to run
let queryRunner;
if (cursor) {
queryRunner = () => requestNewestEntries(graphqlWithAuth, org, cursor);
queryRunner = () => requestEntries(graphqlWithAuth, org, cursor);
} else {
queryRunner = () => requestAllEntries(graphqlWithAuth, org);
queryRunner = () => requestEntries(graphqlWithAuth, org);
}
// Run the query and store the most recent cursor
let {data, newestCursor} = await queryRunner();
let {data, newestCursorId} = await queryRunner();
let entries = data;
if(newestCursor) fs.writeFileSync('.last-cursor-update', newestCursor);
if(newestCursorId) fs.writeFileSync('.last-cursor-update', newestCursorId);
// Return the data
if (pretty) {

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

@ -5,7 +5,6 @@ query($org: String!, $page: String) {
organization(login: $org) {
auditLog(first: 100, after: $page){
pageInfo {
startCursor
endCursor
hasNextPage
}
@ -16,22 +15,6 @@ query($org: String!, $page: String) {
}
}`;
const newEntriesQuery = `
query($org: String!, $page: String!) {
organization(login: $org) {
auditLog(last: 100, before: $page){
pageInfo {
startCursor
hasNextPage
}
nodes {
${auditLogEntries}
}
}
}
}`;
module.exports = {
allEntriesQuery,
newEntriesQuery
};