summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/environments/helpers/deployment_data_transformation_helper.js
blob: bfe92fe312575b70beecd0a42d265e02aba3dfb9 (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
import { getIdFromGraphQLId } from '~/graphql_shared/utils';

/**
 * This function transforms Commit object coming from GraphQL to object compatible with app/assets/javascripts/vue_shared/components/commit.vue author object
 * @param {Object} Commit
 * @returns {Object}
 */
export const getAuthorFromCommit = (commit) => {
  if (commit.author) {
    return {
      username: commit.author.name,
      path: commit.author.webUrl,
      avatar_url: commit.author.avatarUrl,
    };
  }
  return {
    username: commit.authorName,
    path: `mailto:${commit.authorEmail}`,
    avatar_url: commit.authorGravatar,
  };
};

/**
 * This function transforms deploymentNode object coming from GraphQL to object compatible with app/assets/javascripts/vue_shared/components/commit.vue
 * @param {Object} deploymentNode
 * @returns {Object}
 */
export const getCommitFromDeploymentNode = (deploymentNode) => {
  if (!deploymentNode.commit) {
    throw new Error("deploymentNode argument doesn't have 'commit' field", deploymentNode);
  }
  return {
    title: deploymentNode.commit.message,
    commitUrl: deploymentNode.commit.webUrl,
    shortSha: deploymentNode.commit.shortId,
    tag: deploymentNode.tag,
    commitRef: {
      name: deploymentNode.ref,
    },
    author: getAuthorFromCommit(deploymentNode.commit),
  };
};

/**
 * This function transforms deploymentNode object coming from GraphQL to object compatible with app/assets/javascripts/environments/environment_details/page.vue table
 * @param {Object} deploymentNode
 * @returns {Object}
 */
export const convertToDeploymentTableRow = (deploymentNode) => {
  return {
    status: deploymentNode.status.toLowerCase(),
    id: deploymentNode.iid,
    triggerer: deploymentNode.triggerer,
    commit: getCommitFromDeploymentNode(deploymentNode),
    job: deploymentNode.job && {
      webPath: deploymentNode.job.webPath,
      label: `${deploymentNode.job.name} (#${getIdFromGraphQLId(deploymentNode.job.id)})`,
    },
    created: deploymentNode.createdAt || '',
    deployed: deploymentNode.finishedAt || '',
  };
};