diff options
Diffstat (limited to 'app/assets/javascripts/pipelines/utils.js')
-rw-r--r-- | app/assets/javascripts/pipelines/utils.js | 87 |
1 files changed, 31 insertions, 56 deletions
diff --git a/app/assets/javascripts/pipelines/utils.js b/app/assets/javascripts/pipelines/utils.js index 7d1a1762e0d..28d6c0edb0f 100644 --- a/app/assets/javascripts/pipelines/utils.js +++ b/app/assets/javascripts/pipelines/utils.js @@ -5,66 +5,42 @@ export const validateParams = params => { return pickBy(params, (val, key) => SUPPORTED_FILTER_PARAMETERS.includes(key) && val); }; -export const createUniqueJobId = (stageName, jobName) => `${stageName}-${jobName}`; +export const createUniqueLinkId = (stageName, jobName) => `${stageName}-${jobName}`; /** - * This function takes a json payload that comes from a yml - * file converted to json through `jsyaml` library. Because we - * naively convert the entire yaml to json, some keys (like `includes`) - * are irrelevant to rendering the graph and must be removed. We also - * restructure the data to have the structure from an API response for the - * pipeline data. - * @param {Object} jsonData - * @returns {Array} - Array of stages containing all jobs + * This function takes the stages array and transform it + * into a hash where each key is a job name and the job data + * is associated to that key. + * @param {Array} stages + * @returns {Object} - Hash of jobs */ -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 - // that they added under jobs. We also remove duplicates. - const jobStages = jobNames.map(job => jsonData[job].stage); - const userDefinedStages = jsonData?.stages ?? []; - - // The order is important here. We always show the stages in order they were - // defined in the `stages` key first, and then stages that are under the jobs. - const stages = Array.from(new Set([...userDefinedStages, ...jobStages])); - - const arrayOfJobsByStage = stages.map(val => { - return jobNames.filter(job => { - return jsonData[job].stage === val; - }); - }); +export const createJobsHash = (stages = []) => { + const jobsHash = {}; - const pipelineData = stages.map((stage, index) => { - const stageJobs = arrayOfJobsByStage[index]; - return { - name: stage, - groups: stageJobs.map(job => { - return { - name: job, - jobs: [{ ...jsonData[job] }], - id: createUniqueJobId(stage, job), - }; - }), - }; + stages.forEach(stage => { + if (stage.groups.length > 0) { + stage.groups.forEach(group => { + group.jobs.forEach(job => { + jobsHash[job.name] = job; + }); + }); + } }); - return { stages: pipelineData, jobs }; + return jobsHash; }; -export const generateJobNeedsDict = ({ jobs }) => { +/** + * This function takes the jobs hash generated by + * `createJobsHash` function and returns an easier + * structure to work with for needs relationship + * where the key is the job name and the value is an + * array of all the needs this job has recursively + * (includes the needs of the needs) + * @param {Object} jobs + * @returns {Object} - Hash of jobs and array of needs + */ +export const generateJobNeedsDict = (jobs = {}) => { const arrOfJobNames = Object.keys(jobs); return arrOfJobNames.reduce((acc, value) => { @@ -75,13 +51,12 @@ export const generateJobNeedsDict = ({ jobs }) => { 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); + const newNeeds = acc[job] ?? recursiveNeeds(job); - return [id, ...newNeeds]; + return [job, ...newNeeds]; }) .flat(Infinity); }; @@ -91,6 +66,6 @@ export const generateJobNeedsDict = ({ jobs }) => { // duplicates from the array. const uniqueValues = Array.from(new Set(recursiveNeeds(value))); - return { ...acc, [jobs[value].id]: uniqueValues }; + return { ...acc, [value]: uniqueValues }; }, {}); }; |