summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/releases/util.js
blob: f1f5f4bca4c0decb82ce3da97068a5059725527f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import { pick } from 'lodash';
import createGqClient, { fetchPolicies } from '~/lib/graphql';
import { truncateSha } from '~/lib/utils/text_utility';

export const gqClient = createGqClient({}, { fetchPolicy: fetchPolicies.NO_CACHE });

const convertScalarProperties = (graphQLRelease) =>
  pick(graphQLRelease, [
    'name',
    'tagName',
    'tagPath',
    'description',
    'descriptionHtml',
    'upcomingRelease',
  ]);

const convertDateProperties = ({ releasedAt }) => ({
  releasedAt: new Date(releasedAt),
});

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) => ({
      ...l,
      linkType: l.linkType?.toLowerCase(),
    }));
  }

  return {
    assets: {
      count: graphQLRelease.assets.count,
      sources,
      links,
    },
  };
};

const convertEvidences = (graphQLRelease) => ({
  evidences: (graphQLRelease.evidences?.nodes ?? []).map((e) => ({ ...e })),
});

const convertLinks = (graphQLRelease) => ({
  _links: {
    ...graphQLRelease.links,
    self: graphQLRelease.links?.selfUrl,
  },
});

const convertCommit = (graphQLRelease) => {
  if (!graphQLRelease.commit) {
    return {};
  }

  return {
    commit: {
      shortId: truncateSha(graphQLRelease.commit.sha),
      title: graphQLRelease.commit.title,
    },
    commitPath: graphQLRelease.commit.webUrl,
  };
};

const convertAuthor = (graphQLRelease) => ({ author: graphQLRelease.author });

const convertMilestones = (graphQLRelease) => ({
  milestones: graphQLRelease.milestones.nodes.map((m) => ({
    ...m,
    webUrl: m.webPath,
    webPath: undefined,
    issueStats: m.stats
      ? {
          total: m.stats.totalIssuesCount,
          closed: m.stats.closedIssuesCount,
        }
      : {},
    stats: undefined,
  })),
});

/**
 * Converts a single release object fetched from GraphQL
 * into a release object that matches the general structure of the REST API
 *
 * @param graphQLRelease The release object returned from a GraphQL query
 */
export const convertGraphQLRelease = (graphQLRelease) => ({
  ...convertScalarProperties(graphQLRelease),
  ...convertDateProperties(graphQLRelease),
  ...convertAssets(graphQLRelease),
  ...convertEvidences(graphQLRelease),
  ...convertLinks(graphQLRelease),
  ...convertCommit(graphQLRelease),
  ...convertAuthor(graphQLRelease),
  ...convertMilestones(graphQLRelease),
});

/**
 * Converts the response from all_releases.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
 */
export const convertAllReleasesGraphQLResponse = (response) => {
  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
 */
export const convertOneReleaseGraphQLResponse = (response) => {
  const release = convertGraphQLRelease(response.data.project.release);

  return { data: release };
};

export const deleteReleaseSessionKey = (projectPath) => `deleteRelease:${projectPath}`;