2020-09-15 11:10:08 -04:00
|
|
|
import { pick } from 'lodash';
|
|
|
|
import createGqClient, { fetchPolicies } from '~/lib/graphql';
|
|
|
|
import { truncateSha } from '~/lib/utils/text_utility';
|
2020-08-10 11:09:49 -04:00
|
|
|
import {
|
|
|
|
convertObjectPropsToCamelCase,
|
|
|
|
convertObjectPropsToSnakeCase,
|
|
|
|
} from '~/lib/utils/common_utils';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a release object into a JSON object that can sent to the public
|
|
|
|
* API to create or update a release.
|
|
|
|
* @param {Object} release The release object to convert
|
|
|
|
* @param {string} createFrom The ref to create a new tag from, if necessary
|
|
|
|
*/
|
|
|
|
export const releaseToApiJson = (release, createFrom = null) => {
|
2020-08-10 14:09:54 -04:00
|
|
|
const name = release.name?.trim().length > 0 ? release.name.trim() : null;
|
|
|
|
|
2020-11-10 01:08:58 -05:00
|
|
|
// Milestones may be either a list of milestone objects OR just a list
|
|
|
|
// of milestone titles. The API requires only the titles be sent.
|
2020-12-23 19:10:25 -05:00
|
|
|
const milestones = (release.milestones || []).map((m) => m.title || m);
|
2020-11-10 01:08:58 -05:00
|
|
|
|
2020-08-10 11:09:49 -04:00
|
|
|
return convertObjectPropsToSnakeCase(
|
|
|
|
{
|
2020-08-10 14:09:54 -04:00
|
|
|
name,
|
2020-08-10 11:09:49 -04:00
|
|
|
tagName: release.tagName,
|
|
|
|
ref: createFrom,
|
|
|
|
description: release.description,
|
2020-11-10 01:08:58 -05:00
|
|
|
milestones,
|
2020-08-10 11:09:49 -04:00
|
|
|
assets: release.assets,
|
|
|
|
},
|
|
|
|
{ deep: true },
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a JSON release object returned by the Release API
|
|
|
|
* into the structure this Vue application can work with.
|
|
|
|
* @param {Object} json The JSON object received from the release API
|
|
|
|
*/
|
2020-12-23 19:10:25 -05:00
|
|
|
export const apiJsonToRelease = (json) => {
|
2020-08-10 11:09:49 -04:00
|
|
|
const release = convertObjectPropsToCamelCase(json, { deep: true });
|
|
|
|
|
|
|
|
release.milestones = release.milestones || [];
|
|
|
|
|
|
|
|
return release;
|
|
|
|
};
|
2020-09-15 11:10:08 -04:00
|
|
|
|
|
|
|
export const gqClient = createGqClient({}, { fetchPolicy: fetchPolicies.NO_CACHE });
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
const convertScalarProperties = (graphQLRelease) =>
|
2020-09-15 11:10:08 -04:00
|
|
|
pick(graphQLRelease, [
|
|
|
|
'name',
|
|
|
|
'tagName',
|
|
|
|
'tagPath',
|
|
|
|
'descriptionHtml',
|
|
|
|
'releasedAt',
|
|
|
|
'upcomingRelease',
|
|
|
|
]);
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
const convertAssets = (graphQLRelease) => ({
|
2020-09-15 11:10:08 -04:00
|
|
|
assets: {
|
|
|
|
count: graphQLRelease.assets.count,
|
|
|
|
sources: [...graphQLRelease.assets.sources.nodes],
|
2020-12-23 19:10:25 -05:00
|
|
|
links: graphQLRelease.assets.links.nodes.map((l) => ({
|
2020-09-15 11:10:08 -04:00
|
|
|
...l,
|
|
|
|
linkType: l.linkType?.toLowerCase(),
|
|
|
|
})),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
const convertEvidences = (graphQLRelease) => ({
|
|
|
|
evidences: graphQLRelease.evidences.nodes.map((e) => e),
|
2020-09-15 11:10:08 -04:00
|
|
|
});
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
const convertLinks = (graphQLRelease) => ({
|
2020-09-15 11:10:08 -04:00
|
|
|
_links: {
|
|
|
|
...graphQLRelease.links,
|
|
|
|
self: graphQLRelease.links?.selfUrl,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
const convertCommit = (graphQLRelease) => {
|
2020-09-15 11:10:08 -04:00
|
|
|
if (!graphQLRelease.commit) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
commit: {
|
|
|
|
shortId: truncateSha(graphQLRelease.commit.sha),
|
|
|
|
title: graphQLRelease.commit.title,
|
|
|
|
},
|
|
|
|
commitPath: graphQLRelease.commit.webUrl,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
const convertAuthor = (graphQLRelease) => ({ author: graphQLRelease.author });
|
2020-09-15 11:10:08 -04:00
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
const convertMilestones = (graphQLRelease) => ({
|
|
|
|
milestones: graphQLRelease.milestones.nodes.map((m) => ({
|
2020-09-15 11:10:08 -04:00
|
|
|
...m,
|
|
|
|
webUrl: m.webPath,
|
|
|
|
webPath: undefined,
|
|
|
|
issueStats: {
|
|
|
|
total: m.stats.totalIssuesCount,
|
|
|
|
closed: m.stats.closedIssuesCount,
|
|
|
|
},
|
|
|
|
stats: undefined,
|
|
|
|
})),
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
2020-10-15 05:08:41 -04:00
|
|
|
* Converts a single release object fetched from GraphQL
|
|
|
|
* into a release object that matches the shape of the REST API
|
|
|
|
* (the same shape that is returned by `apiJsonToRelease` above.)
|
|
|
|
*
|
|
|
|
* @param graphQLRelease The release object returned from a GraphQL query
|
|
|
|
*/
|
2020-12-23 19:10:25 -05:00
|
|
|
export const convertGraphQLRelease = (graphQLRelease) => ({
|
2020-10-15 05:08:41 -04:00
|
|
|
...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-09-15 11:10:08 -04:00
|
|
|
* 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
|
|
|
|
*/
|
2020-12-23 19:10:25 -05:00
|
|
|
export const convertAllReleasesGraphQLResponse = (response) => {
|
2020-10-15 05:08:41 -04:00
|
|
|
const releases = response.data.project.releases.nodes.map(convertGraphQLRelease);
|
2020-09-15 11:10:08 -04:00
|
|
|
|
2020-09-18 11:09:22 -04:00
|
|
|
const paginationInfo = {
|
|
|
|
...response.data.project.releases.pageInfo,
|
|
|
|
};
|
|
|
|
|
|
|
|
return { data: releases, paginationInfo };
|
2020-09-15 11:10:08 -04:00
|
|
|
};
|
2020-10-15 05:08:41 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-12-23 19:10:25 -05:00
|
|
|
export const convertOneReleaseGraphQLResponse = (response) => {
|
2020-10-15 05:08:41 -04:00
|
|
|
const release = convertGraphQLRelease(response.data.project.release);
|
|
|
|
|
|
|
|
return { data: release };
|
|
|
|
};
|