Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const ignoreExtraTriggeredPipelines = pipelineTuples.filter((t) => {
// empty event is not created when it is triggered by extra triggers (e.g. ~tag, ~release)
if (EXTRA_TRIGGERS.test(t.startFrom) && !hasTriggeredJob(t.pipeline, t.startFrom)) {
logger.warn(`Event not created: there are no jobs triggered by ${t.startFrom}`);
return false;
}
return true;
});
async function getMaxLines({ baseUrl, authToken }) {
let linesInFirstPage;
// check lines per file by looking at the first file
try {
linesInFirstPage = await fetchLog({
baseUrl,
authToken,
sort: 'ascending',
linesFrom: 0,
page: 0 });
} catch (err) {
logger.error(err);
throw new Error(err);
}
return linesInFirstPage.length > MAX_LINES_SMALL ? MAX_LINES_BIG : MAX_LINES_SMALL;
}
async function loadLines({
baseUrl,
linesFrom,
authToken,
pagesToLoad = 10,
sort = 'ascending',
maxLines
}) {
const page = Math.floor(linesFrom / maxLines);
let morePages = false;
let lines;
try {
lines = await fetchLog({ baseUrl, linesFrom, authToken, page, sort });
} catch (err) {
logger.error(err);
throw new Error(err);
}
const linesCount = lines.length;
const pagesToLoadUpdated = pagesToLoad - 1;
const linesFromUpdated = sort === 'descending' ?
linesFrom - linesCount : linesCount + linesFrom;
// If we got lines AND there are more lines to load
const descLoadNext = sort === 'descending' && linesCount > 0 && linesFrom - linesCount > 0;
// If we got lines AND we reached the edge of a page
const ascLoadNext = sort === 'ascending' && linesCount > 0
&& (linesCount + linesFrom) % maxLines === 0;
// Load from next log if there's still lines left
if (ascLoadNext || descLoadNext) {
if (pagesToLoadUpdated > 0) {
delete newAdmins[username];
// This is needed to make admins dirty and update db
pipeline.admins = newAdmins;
return pipeline.update();
}
// Add user as admin if permissions good and does not already exist
if (!pipeline.admins[username]) {
newAdmins[username] = true;
// This is needed to make admins dirty and update db
pipeline.admins = newAdmins;
return pipeline.update();
}
} catch (err) {
logger.info(err.message);
}
return Promise.resolve();
}
const gracefulStop = async () => {
try {
logger.info('gracefully shutting down server');
await server.root.stop({
timeout: 5000
});
process.exit(0);
} catch (err) {
logger.error(err);
process.exit(1);
}
};
function hasTriggeredJob(pipeline, startFrom) {
try {
const nextJobs = workflowParser.getNextJobs(pipeline.workflowGraph, {
trigger: startFrom
});
return nextJobs.length > 0;
} catch (err) {
logger.error(`Error finding triggered jobs for ${pipeline.id}: ${err}`);
return false;
}
}
const onSigterm = async () => {
try {
logger.info('got SIGTERM; running triggers before shutdown');
const res = await promiseTimeout(taskHandler(), options.terminationGracePeriod * 1000);
if (res) {
logger.error(res);
}
await gracefulStop();
} catch (err) {
logger.error(err);
}
};
.catch(err => logger.error('Failed to start server', err));
});
const taskHandler = async () => {
try {
await Promise.all(Object.keys(tasks).map(async (key) => {
logger.info(`executing task ${key}`);
const item = tasks[key];
await item.task();
}));
return Promise.resolve();
} catch (err) {
logger.error(err);
throw err;
}
};
);
}
const hasBuildEvents = events.filter(e => e.builds !== null);
if (hasBuildEvents.length === 0) {
return reply({ message: 'No jobs to start' }).code(204);
}
hasBuildEvents.forEach((e) => {
request.log(['webhook', hookId, e.id], `Event ${e.id} started`);
});
return reply().code(201);
} catch (err) {
logger.error(`[${hookId}]: ${err}`);
return reply(boom.boomify(err));
}
}