generated from actions/javascript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
181 lines (160 loc) · 5.9 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
const core = require("@actions/core");
const github = require("@actions/github");
const { promises: fs } = require("fs");
const glob = require("@actions/glob");
const { IGNORE_FILE } = require("./ignore-files");
const {
SOURCE_TYPES,
IMAGE_TYPES,
AUDIO_TYPES,
VIDEO_TYPES,
DATA_TYPES,
} = require("./include-files");
const FormData = require("form-data");
const https = require("https");
const wait = require("./wait");
// CONSTANTS
const personal_access_token_input = `${core.getInput("personal-access-token")}`;
const jspsych_version = `${core.getInput("jspsych-version")}`;
const API_KEY_URL = "https://www.cognition.run/account";
const UPGRADE_URL = "https://www.cognition.run/account";
const SUPPORT_FORM =
"https://docs.google.com/forms/d/e/1FAIpQLSdYg3h6ESzd81rHlGlAib_kXA56ERuy0MBM1CwPeUdTv4lvcQ/viewform";
const ENDPOINT = "https://www.cognition.run/external/api/github/v1/resource";
let repository = github.context.payload.repository;
const REQUEST_URL = `${ENDPOINT}/${repository ? repository.name : ""}`;
const AUTHORIZATION = `Bearer ${personal_access_token_input}`;
async function httpPostFileError(filePath, errorMessage, httpResponse) {
core.error(errorMessage);
core.debug(
`Error uploading ${filePath} to ${httpResponse.method}:${httpResponse.url} with status code ${httpResponse.statusCode}`
);
core.setFailed(`Unable to deploy ${filePath} to Cognition.`);
}
async function uploadFile(filePath, ignoreJS) {
core.debug(`Uploading ${filePath} to Cognition.`);
const fileHandle = await fs.open(filePath);
const fileStats = await fileHandle.stat();
const fileReadStream = fileHandle.createReadStream();
const formData = new FormData();
const filename = filePath.replace(/^.*[\\/]/, "");
if (jspsych_version != null) {
formData.append("jspsych_version", jspsych_version);
}
if (ignoreJS) {
formData.append("ignore_js", "true");
}
formData.append("file", fileReadStream, {
knownLength: fileStats.size,
filename,
filepath: filePath,
contentType: "multipart/form-data",
});
const formDataHeaders = formData.getHeaders();
const requestTotalHeaders = {
Authorization: AUTHORIZATION,
...formDataHeaders,
};
const request = https.request(REQUEST_URL, {
method: "POST",
protocol: "https:",
port: 443,
headers: requestTotalHeaders,
});
formData.pipe(request);
request.on("response", async function (response) {
let data = "";
response.on("data", (chunk) => {
data += chunk;
});
const statusCode = response.statusCode;
response.on("end", () => {
if (data.length > 0) {
core.debug(`Body: ${data}`);
if (statusCode === 200) {
const parsedBody = JSON.parse(data);
core.setOutput("link", parsedBody.public_link);
}
}
});
if (statusCode === 401 || statusCode === 403 || statusCode === 302) {
await httpPostFileError(
filePath,
`Unable to deploy ${filePath} to Cognition. The action script could connect to Cognition server, but the
Personal Access Token secret has expired or is not recognized by the system. Check your Cognition Personal access token at ${API_KEY_URL} .
If the problem persist, please contact us at ${SUPPORT_FORM} .`,
response
);
} else if (statusCode === 402) {
await httpPostFileError(
filePath,
`Unable to deploy ${filePath} to Cognition. Your credentials are correct, but your account has reached
the limit of stored tasks or your current plan do not support the this experiment due to the limit of external libraries (css or js files) and
stimuli allowed per each task. Upgrade your plan at: ${UPGRADE_URL} . If the problem persist, please contact us at ${SUPPORT_FORM} .`,
response
);
} else if (statusCode === 404 || statusCode === 422) {
await httpPostFileError(
filePath,
`Unable to deploy ${filePath} to Cognition. The action could connect to the server, but the deploy
was rejected. This issue can be related to a deprecated action version. Please upgrade your workflow file to require
the last version of this action. If the problem persist, please contact us at ${SUPPORT_FORM} .`,
response
);
} else if (statusCode !== 200) {
await httpPostFileError(
filePath,
`Unable to deploy ${filePath} to Cognition. The script could connect to Cognition server, but it
found an unexpected error. Please try again later. If the problem persist, please contact us at ${SUPPORT_FORM} .`,
response
);
} else {
core.info(`${filePath} successfully uploaded to Cognition.`);
}
});
}
function isExperimentFile(file) {
return !file.includes("node_modules") && !file.startsWith(".");
}
// most @actions toolkit packages have async methods
async function run() {
if (!personal_access_token_input) {
core.setFailed(`personal-access-token action input is required.`);
return;
}
try {
// Look for index.js or index.html
const globber = await glob.create(
SOURCE_TYPES.concat(IMAGE_TYPES)
.concat(AUDIO_TYPES)
.concat(VIDEO_TYPES)
.concat(DATA_TYPES)
.join("\n"),
{
followSymbolicLinks: false,
}
);
const files = globber.globGenerator();
let firstFileUploaded = false;
let ignoreJS = false;
for await (const file of files) {
const filename = file.replace(/^.*[\\/]/, "");
if (isExperimentFile(file) && !IGNORE_FILE.includes(filename)) {
await uploadFile(file, ignoreJS);
// If we sent index.js, ignore index.html javascript
if (filename === "index.js") {
ignoreJS = true;
}
if (!firstFileUploaded) {
firstFileUploaded = true;
await wait(1000); //Make sure one task is created
} else {
await wait(200);
}
}
}
} catch (error) {
core.setFailed(error.message);
}
}
run();