-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
140 lines (126 loc) · 3.43 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
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
138
139
140
const core = require('@actions/core');
const fetch = require('node-fetch');
/**
* The Cloudways API URI.
* It'll be used to make the requests.
*
* @since 1.0.0
*
* @type {string}
*/
const apiUri = 'https://api.cloudways.com/api/v1';
/**
* Get access token.
*
* We need the Cloudways API Key and the email address of the account.
* The access token will be used to authenticate the request.
*
* @link https://developers.cloudways.com/docs/#!/AuthenticationApi#getOAuthAccessToken
*
* @since 1.0.0
* @since 1.2.0 - Add error handling.
*
* @returns {Promise<unknown>}
*/
async function getOauthToken() {
const body = {
api_key: core.getInput('api-key'),
email: core.getInput('email'),
};
const options = {
method: 'POST',
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json' },
};
/**
* The API response.
*
* If the request is successful, the response will contain the access token.
* Otherwise, it'll contain the error message.
*
* @since 1.0.0
*
* @type {{
* error: boolean,
* error_description: string,
* access_token: string,
* expires_in: number,
* token_type: string
* }}
*/
const response = await fetch(`${ apiUri }/oauth/access_token`, options).then(res => res.json());
if (response.error) {
throw new Error(response.error_description);
}
if (!response.access_token || response.access_token === '') {
throw new Error('The access token does not exist.');
}
return response.access_token;
}
/**
* Deploy changes.
*
* It'll pull the new changes from the repository and deploy them to the server.
*
* We need:
*
* - `server_id`. Numeric id of the server.
* - `app_id`. Numeric id of the application.
* - `branch_name`. Name of the branch to pull.
* - `deploy_path`. Path to deploy the changes.
*
* @link https://developers.cloudways.com/docs/#!/GitApi#startGitPull
*
* @since 1.0.0
*
* @param {string} token The access token.
* @returns {Promise<{code: *, ok: *, body: *}>}
*/
async function deployChanges(token) {
const body = {
app_id: core.getInput('app-id'),
branch_name: core.getInput('branch-name'),
deploy_path: core.getInput('deploy-path'),
server_id: core.getInput('server-id'),
};
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
}
});
});
}
/**
* Run the action.
* It'll get the access token and deploy the changes.
*
* @since 1.0.0
*
* @returns {Promise<void>}
*/
async function run() {
try {
const access_token = await getOauthToken();
await deployChanges(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();