diff options
Diffstat (limited to 'app/assets/javascripts/pipelines/utils.js')
-rw-r--r-- | app/assets/javascripts/pipelines/utils.js | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/app/assets/javascripts/pipelines/utils.js b/app/assets/javascripts/pipelines/utils.js index bd53b22784c..7d1a1762e0d 100644 --- a/app/assets/javascripts/pipelines/utils.js +++ b/app/assets/javascripts/pipelines/utils.js @@ -5,6 +5,8 @@ export const validateParams = params => { return pickBy(params, (val, key) => SUPPORTED_FILTER_PARAMETERS.includes(key) && val); }; +export const createUniqueJobId = (stageName, jobName) => `${stageName}-${jobName}`; + /** * This function takes a json payload that comes from a yml * file converted to json through `jsyaml` library. Because we @@ -18,6 +20,16 @@ export const validateParams = params => { export const preparePipelineGraphData = jsonData => { const jsonKeys = Object.keys(jsonData); const jobNames = jsonKeys.filter(job => jsonData[job]?.stage); + // Creates an object with only the valid jobs + const jobs = jsonKeys.reduce((acc, val) => { + if (jobNames.includes(val)) { + return { + ...acc, + [val]: { ...jsonData[val], id: createUniqueJobId(jsonData[val].stage, val) }, + }; + } + return { ...acc }; + }, {}); // We merge both the stages from the "stages" key in the yaml and the stage associated // with each job to show the user both the stages they explicitly defined, and those @@ -40,10 +52,45 @@ export const preparePipelineGraphData = jsonData => { return { name: stage, groups: stageJobs.map(job => { - return { name: job, jobs: [{ ...jsonData[job] }] }; + return { + name: job, + jobs: [{ ...jsonData[job] }], + id: createUniqueJobId(stage, job), + }; }), }; }); - return { stages: pipelineData }; + return { stages: pipelineData, jobs }; +}; + +export const generateJobNeedsDict = ({ jobs }) => { + const arrOfJobNames = Object.keys(jobs); + + return arrOfJobNames.reduce((acc, value) => { + const recursiveNeeds = jobName => { + if (!jobs[jobName]?.needs) { + return []; + } + + return jobs[jobName].needs + .map(job => { + const { id } = jobs[job]; + // If we already have the needs of a job in the accumulator, + // then we use the memoized data instead of the recursive call + // to save some performance. + const newNeeds = acc[id] ?? recursiveNeeds(job); + + return [id, ...newNeeds]; + }) + .flat(Infinity); + }; + + // To ensure we don't have duplicates job relationship when 2 jobs + // needed by another both depends on the same jobs, we remove any + // duplicates from the array. + const uniqueValues = Array.from(new Set(recursiveNeeds(value))); + + return { ...acc, [jobs[value].id]: uniqueValues }; + }, {}); }; |