-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
81 lines (76 loc) · 3.21 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
const core = require('@actions/core');
const github = require('@actions/github');
const axios = require('axios')
const functionCode = core.getInput('function-code')
const functionName = core.getInput('function-name')
const workspaceID = core.getInput('workspaceID')
const functionID = core.getInput('function-id')
const token = core.getInput('token')
const functionType = core.getInput('function-type').toUpperCase()
// Appends the Auth Header to all Axios requests.
axios.interceptors.request.use(function (config) {
config.headers.Authorization = `Bearer ${token}`;
return config;
});
try {
//First returns a list of all functions in a workspace
axios.get(`https://platform.segmentapis.com/v1beta/workspaces/${workspaceID}/functions`, {
params: {
type: functionType
}
})
.then(function (response) {
console.log(response.data.functions)
const functionsList = response.data.functions
// Checks for presence of a FunctionID input and checks to see if it exists
if (functionID) {
console.log('found functionID')
functionsList.forEach(functionReturned => {
// If the function exists, update it.
if (functionReturned.id == functionID) {
console.log('function exists, update')
// Build body for Update / Patch request
const patchBodyParams = {
update_mask: "function.code",
function: {
id: functionID,
workspace_id: workspaceID,
code: functionCode,
buildpack: "boreal"
}
}
updateFunction(workspaceID, patchBodyParams, functionID)
}
})
}
else {
// If function is not found, default to creating a new one.
console.log('creating new function')
// Puts together the body of the payload sent to Create function endpoint.
const createBodyParams = {
type: functionType,
function: {
display_name: functionName,
code: functionCode,
buildpack: "boreal"
}
}
createFunction(workspaceID, createBodyParams)
}
})
} catch (error) {
core.setFailed(error.message);
}
function updateFunction(workspaceIDInput, patchParamsInput, functionIDInput) {
axios.patch(`https://platform.segmentapis.com/v1beta/workspaces/${workspaceIDInput}/functions/${functionIDInput}`,
patchParamsInput)
console.log('calling updating function')
}
function createFunction(workspaceIDInput, createBodyParamsInput) {
console.log('calling creating function')
axios.post(`https://platform.segmentapis.com/v1beta/workspaces/${workspaceIDInput}/functions`,
createBodyParamsInput)
.then(function (response) {
console.log('Function Created Successfully')
})
}