2021-06-08 01:23:25 +05:30
|
|
|
import { isEmpty } from 'lodash';
|
2021-02-22 17:27:13 +05:30
|
|
|
import Visibility from 'visibilityjs';
|
|
|
|
import { getIdFromGraphQLId } from '~/graphql_shared/utils';
|
2021-04-29 21:17:54 +05:30
|
|
|
import { unwrapStagesWithNeedsAndLookup } from '../unwrapping_utils';
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
const addMulti = (mainPipelineProjectPath, linkedPipeline) => {
|
|
|
|
return {
|
|
|
|
...linkedPipeline,
|
|
|
|
multiproject: mainPipelineProjectPath !== linkedPipeline.project.fullPath,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
/* eslint-disable @gitlab/require-i18n-strings */
|
|
|
|
const getQueryHeaders = (etagResource) => {
|
|
|
|
return {
|
|
|
|
fetchOptions: {
|
|
|
|
method: 'GET',
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
'X-GITLAB-GRAPHQL-FEATURE-CORRELATION': 'verify/ci/pipeline-graph',
|
|
|
|
'X-GITLAB-GRAPHQL-RESOURCE-ETAG': etagResource,
|
|
|
|
'X-Requested-With': 'XMLHttpRequest',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const serializeGqlErr = (gqlError) => {
|
|
|
|
const { locations = [], message = '', path = [] } = gqlError;
|
|
|
|
|
|
|
|
return `
|
|
|
|
${message}.
|
|
|
|
Locations: ${locations
|
|
|
|
.flatMap((loc) => Object.entries(loc))
|
|
|
|
.flat(2)
|
|
|
|
.join(' ')}.
|
|
|
|
Path: ${path.join(', ')}.
|
|
|
|
`;
|
|
|
|
};
|
|
|
|
|
|
|
|
const serializeLoadErrors = (errors) => {
|
|
|
|
const { gqlError, graphQLErrors, networkError, message } = errors;
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
if (!isEmpty(graphQLErrors)) {
|
2021-04-17 20:07:23 +05:30
|
|
|
return graphQLErrors.map((err) => serializeGqlErr(err)).join('; ');
|
|
|
|
}
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
if (!isEmpty(gqlError)) {
|
2021-04-17 20:07:23 +05:30
|
|
|
return serializeGqlErr(gqlError);
|
|
|
|
}
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
if (!isEmpty(networkError)) {
|
2021-04-17 20:07:23 +05:30
|
|
|
return `Network error: ${networkError.message}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return message;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* eslint-enable @gitlab/require-i18n-strings */
|
|
|
|
|
|
|
|
const toggleQueryPollingByVisibility = (queryRef, interval = 10000) => {
|
|
|
|
const stopStartQuery = (query) => {
|
|
|
|
if (!Visibility.hidden()) {
|
|
|
|
query.startPolling(interval);
|
|
|
|
} else {
|
|
|
|
query.stopPolling();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
stopStartQuery(queryRef);
|
|
|
|
Visibility.change(stopStartQuery.bind(null, queryRef));
|
|
|
|
};
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
const transformId = (linkedPipeline) => {
|
2021-02-22 17:27:13 +05:30
|
|
|
return { ...linkedPipeline, id: getIdFromGraphQLId(linkedPipeline.id) };
|
|
|
|
};
|
|
|
|
|
|
|
|
const unwrapPipelineData = (mainPipelineProjectPath, data) => {
|
|
|
|
if (!data?.project?.pipeline) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { pipeline } = data.project;
|
|
|
|
|
|
|
|
const {
|
|
|
|
upstream,
|
|
|
|
downstream,
|
|
|
|
stages: { nodes: stages },
|
|
|
|
} = pipeline;
|
|
|
|
|
2021-04-29 21:17:54 +05:30
|
|
|
const { stages: updatedStages, lookup } = unwrapStagesWithNeedsAndLookup(stages);
|
2021-02-22 17:27:13 +05:30
|
|
|
|
|
|
|
return {
|
|
|
|
...pipeline,
|
|
|
|
id: getIdFromGraphQLId(pipeline.id),
|
2021-04-29 21:17:54 +05:30
|
|
|
stages: updatedStages,
|
|
|
|
stagesLookup: lookup,
|
2021-02-22 17:27:13 +05:30
|
|
|
upstream: upstream
|
|
|
|
? [upstream].map(addMulti.bind(null, mainPipelineProjectPath)).map(transformId)
|
|
|
|
: [],
|
|
|
|
downstream: downstream
|
|
|
|
? downstream.nodes.map(addMulti.bind(null, mainPipelineProjectPath)).map(transformId)
|
|
|
|
: [],
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
const validateConfigPaths = (value) => value.graphqlResourceEtag?.length > 0;
|
2021-03-08 18:12:59 +05:30
|
|
|
|
2021-04-17 20:07:23 +05:30
|
|
|
export {
|
|
|
|
getQueryHeaders,
|
|
|
|
serializeGqlErr,
|
|
|
|
serializeLoadErrors,
|
|
|
|
toggleQueryPollingByVisibility,
|
|
|
|
unwrapPipelineData,
|
|
|
|
validateConfigPaths,
|
2021-03-08 18:12:59 +05:30
|
|
|
};
|