debian-mirror-gitlab/app/assets/javascripts/releases/util.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

132 lines
3.4 KiB
JavaScript
Raw Normal View History

2020-11-24 15:15:51 +05:30
import { pick } from 'lodash';
import createGqClient, { fetchPolicies } from '~/lib/graphql';
2021-03-11 19:13:27 +05:30
import { truncateSha } from '~/lib/utils/text_utility';
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
export const gqClient = createGqClient({}, { fetchPolicy: fetchPolicies.NO_CACHE });
2021-03-08 18:12:59 +05:30
const convertScalarProperties = (graphQLRelease) =>
2020-11-24 15:15:51 +05:30
pick(graphQLRelease, [
'name',
'tagName',
'tagPath',
2021-06-08 01:23:25 +05:30
'description',
2020-11-24 15:15:51 +05:30
'descriptionHtml',
'releasedAt',
'upcomingRelease',
]);
2021-06-08 01:23:25 +05:30
const convertAssets = (graphQLRelease) => {
let sources = [];
if (graphQLRelease.assets.sources?.nodes) {
sources = [...graphQLRelease.assets.sources.nodes];
}
let links = [];
if (graphQLRelease.assets.links?.nodes) {
links = graphQLRelease.assets.links.nodes.map((l) => ({
2020-11-24 15:15:51 +05:30
...l,
linkType: l.linkType?.toLowerCase(),
2021-06-08 01:23:25 +05:30
}));
}
return {
assets: {
count: graphQLRelease.assets.count,
sources,
links,
},
};
};
2020-11-24 15:15:51 +05:30
2021-03-08 18:12:59 +05:30
const convertEvidences = (graphQLRelease) => ({
2021-06-08 01:23:25 +05:30
evidences: (graphQLRelease.evidences?.nodes ?? []).map((e) => ({ ...e })),
2020-11-24 15:15:51 +05:30
});
2021-03-08 18:12:59 +05:30
const convertLinks = (graphQLRelease) => ({
2020-11-24 15:15:51 +05:30
_links: {
...graphQLRelease.links,
self: graphQLRelease.links?.selfUrl,
},
});
2021-03-08 18:12:59 +05:30
const convertCommit = (graphQLRelease) => {
2020-11-24 15:15:51 +05:30
if (!graphQLRelease.commit) {
return {};
}
return {
commit: {
shortId: truncateSha(graphQLRelease.commit.sha),
title: graphQLRelease.commit.title,
},
commitPath: graphQLRelease.commit.webUrl,
};
};
2021-03-08 18:12:59 +05:30
const convertAuthor = (graphQLRelease) => ({ author: graphQLRelease.author });
2020-11-24 15:15:51 +05:30
2021-03-08 18:12:59 +05:30
const convertMilestones = (graphQLRelease) => ({
milestones: graphQLRelease.milestones.nodes.map((m) => ({
2020-11-24 15:15:51 +05:30
...m,
webUrl: m.webPath,
webPath: undefined,
2021-06-08 01:23:25 +05:30
issueStats: m.stats
? {
total: m.stats.totalIssuesCount,
closed: m.stats.closedIssuesCount,
}
: {},
2020-11-24 15:15:51 +05:30
stats: undefined,
})),
});
/**
2021-01-03 14:25:43 +05:30
* Converts a single release object fetched from GraphQL
2021-06-08 01:23:25 +05:30
* into a release object that matches the general structure of the REST API
2021-01-03 14:25:43 +05:30
*
* @param graphQLRelease The release object returned from a GraphQL query
*/
2021-03-08 18:12:59 +05:30
export const convertGraphQLRelease = (graphQLRelease) => ({
2021-01-03 14:25:43 +05:30
...convertScalarProperties(graphQLRelease),
...convertAssets(graphQLRelease),
...convertEvidences(graphQLRelease),
...convertLinks(graphQLRelease),
...convertCommit(graphQLRelease),
...convertAuthor(graphQLRelease),
...convertMilestones(graphQLRelease),
});
/**
* Converts the response from all_releases.query.graphql into the
2020-11-24 15:15:51 +05:30
* same shape as is returned from the Releases REST API.
*
* This allows the release components to use the response
* from either endpoint interchangeably.
*
* @param response The response received from the GraphQL endpoint
*/
2021-03-08 18:12:59 +05:30
export const convertAllReleasesGraphQLResponse = (response) => {
2021-01-03 14:25:43 +05:30
const releases = response.data.project.releases.nodes.map(convertGraphQLRelease);
const paginationInfo = {
...response.data.project.releases.pageInfo,
};
return { data: releases, paginationInfo };
};
/**
* Converts the response from one_release.query.graphql into the
* same shape as is returned from the Releases REST API.
*
* This allows the release components to use the response
* from either endpoint interchangeably.
*
* @param response The response received from the GraphQL endpoint
*/
2021-03-08 18:12:59 +05:30
export const convertOneReleaseGraphQLResponse = (response) => {
2021-01-03 14:25:43 +05:30
const release = convertGraphQLRelease(response.data.project.release);
return { data: release };
2020-11-24 15:15:51 +05:30
};