Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
server.expose('triggerNextJobs', async (config) => {
const { pipeline, job, build, username, scmContext, externalJoin } = config;
const { buildFactory, eventFactory, jobFactory, pipelineFactory } = server.root.app;
const currentJobName = job.name;
const pipelineId = pipeline.id;
const event = await eventFactory.get({ id: build.eventId });
const workflowGraph = event.workflowGraph;
const nextJobs = workflowParser.getNextJobs(workflowGraph,
{ trigger: currentJobName, chainPR: pipeline.chainPR });
// Create a join object like: {A:[B,C], D:[B,F]} where [B,C] join on A, [B,F] join on D, etc.
// This can include external jobs
const joinObj = nextJobs.reduce((obj, jobName) => {
obj[jobName] = workflowParser.getSrcForJoin(workflowGraph, { jobName });
return obj;
}, {});
// Use old flow if external join flag is off
if (!externalJoin) {
return Promise.all(Object.keys(joinObj).map((nextJobName) => {
const joinList = joinObj[nextJobName];
const joinListNames = joinList.map(j => j.name);
const buildConfig = {
jobFactory,
function dfs(workflowGraph, start, builds, visited) {
const jobId = workflowGraph.nodes.find(node => node.name === start).id;
const nextJobs = workflowParser.getNextJobs(workflowGraph, { trigger: start });
// If the start job has no build in parentEvent then just return
if (!builds.find(build => build.jobId === jobId)) {
return visited;
}
visited.add(builds.find(build => build.jobId === jobId).id);
nextJobs.forEach(job => dfs(workflowGraph, job, builds, visited));
return visited;
}
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;
}
}
function dfs(workflowGraph, start, prNum) {
let nextJobsConfig;
if (start === '~pr') {
nextJobsConfig = {
trigger: start,
prNum
};
} else {
nextJobsConfig = {
trigger: start
};
}
const nextJobs = workflowParser.getNextJobs(workflowGraph, nextJobsConfig);
let visited = new Set(nextJobs);
nextJobs.forEach((job) => {
const subJobs = dfs(workflowGraph, job);
visited = new Set([...visited, ...subJobs]);
});
return visited;
}