forked from roelmagdaleno/cloudways-api-git-pull-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (63 loc) · 1.99 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
const core = require('@actions/core');
const fetch = require('node-fetch');
const apiUri = 'https://api.cloudways.com/api/v1';
async function getOauthToken() {
const body = {
email: core.getInput('email'),
api_key: core.getInput('api-key')
};
const options = {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json'
}
};
return await fetch(`${ apiUri }/oauth/access_token`, options).then(res => res.json());
}
async function deployChanges(token) {
const body = {
server_id: core.getInput('server-id'),
app_id: core.getInput('app-id'),
branch_name: core.getInput('branch-name'),
deploy_path: core.getInput('deploy-path'),
};
const options = {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${ token }`
}
};
return await fetch(`${ apiUri }/git/pull`, options).then(response => {
return response.json().then(data => {
return {
ok: response.ok,
code: response.status,
body: data
}
});
});
}
async function run() {
try {
const oauthToken = await getOauthToken();
if (oauthToken.error) {
throw new Error(oauthToken.error_description);
}
if (!oauthToken.access_token || oauthToken.access_token === '') {
throw new Error('The access token does not exist.');
}
await deployChanges(oauthToken.access_token).then(response => {
if (!response.ok) {
throw new Error(response.body.error_description);
}
core.info(`Success. Operation ID: ${ response.body.operation_id }`);
core.setOutput('operation', response.body.operation_id);
});
} catch (error) {
core.setFailed(error.message);
}
}
run();