-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.js
71 lines (66 loc) · 2.34 KB
/
common.js
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
const { log } = require('@spaship/common/lib/logging/pino');
const axios = require('axios');
const { config } = require('../config');
const orchestratorDeploymentRequest = async (data) => {
log.info(data);
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.orchestratorSecret}`
};
try {
const response = await axios.post(`${config.orchestratorBaseUrl}/applications/git/deploy`, data, { headers });
log.info(response?.data);
return response?.data?.data;
} catch (error) {
log.error('Error in sending data to Orchestrator');
log.error(error);
throw new Error(error?.response?.data.message);
}
};
const validateToken = async (token) => {
log.info(token);
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`
};
try {
const response = await axios.get(`${config.orchestratorBaseUrl}/apikey/validate`, { headers });
log.info(response?.data);
} catch (error) {
log.error(error);
throw new Error(error?.response?.data.message);
}
};
const orchestratorEnvListRequest = async (repoUrl, contextDir) => {
const headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${config.orchestratorSecret}`
};
try {
const response = await axios.get(`${config.orchestratorBaseUrl}/environment/git?repoUrl=${repoUrl}&contextDir=${contextDir}`, { headers });
log.info(response?.data);
return response?.data?.data;
} catch (error) {
log.error('Error in sending data to Orchestrator');
log.error(error);
throw new Error(error?.response?.data.message);
}
};
const createOrchestratorPayload = (payload, contextDir, envs, ref, ephemeral) => {
return {
repoUrl: payload?.repository?.html_url || payload?.project?.web_url,
gitRef: payload?.pull_request?.head?.ref || payload?.object_attributes?.source_branch || ref,
commitId: payload?.pull_request?.head?.sha || payload?.object_attributes?.last_commit?.id || payload?.object_attributes?.commit_id,
mergeId: payload?.pull_request?.number?.toString() || payload?.object_attributes?.iid?.toString(),
projectId: `${payload.project.id}` || 'NA',
contextDir: contextDir,
ephemeral: ephemeral,
envs: envs
};
};
module.exports = {
orchestratorDeploymentRequest,
createOrchestratorPayload,
orchestratorEnvListRequest,
validateToken
};