-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.js
63 lines (54 loc) · 2.82 KB
/
run.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
const core = require('@actions/core');
const { SteadybitAPI } = require('./steadybitAPI');
const { delay } = require('./util');
exports.run = async function run() {
const baseURL = core.getInput('baseURL');
const apiAccessToken = core.getInput('apiAccessToken');
let experimentKey = core.getInput('experimentKey');
const externalId = core.getInput('externalId');
const parallelExecution = core.getInput('parallel') === 'true';
const maxRetries = parseInt(core.getInput('maxRetries'));
const maxRetriesOnExpectationFailure = parseInt(core.getInput('maxRetriesOnExpectationFailure') || 0);
const delayBetweenRetriesOnExpectationFailure = parseInt(core.getInput('delayBetweenRetriesOnExpectationFailure') || 0);
const expectedState = core.getInput('expectedState');
const expectedReason = core.getInput('expectedFailureReason') || core.getInput('expectedReason');
const getExperimentSummary = (experiment) => `${experiment.key} ("${experiment.name.length > 20 ? `${experiment.name.substring(0, 20)}...` : experiment.name}")`;
if (!apiAccessToken) {
core.setFailed('apiAccessToken not provided.');
return;
}
if (!experimentKey && !externalId) {
core.setFailed('Neither experimentKey or externalId is provided.');
return;
}
const steadybitAPI = new SteadybitAPI(baseURL, apiAccessToken);
if (!experimentKey && externalId) {
core.info(`Lookup Experiment Key for external Id ${externalId}.`);
experimentKey = await steadybitAPI.lookupByExternalId(externalId);
}
const experiment = await steadybitAPI.getExperiment(experimentKey);
let lastResult;
let lastError;
const maximumAttempts = Math.max(1, maxRetriesOnExpectationFailure);
for (let attempt = 0; attempt < maximumAttempts && lastResult == null; attempt++) {
lastResult = null;
lastError = null;
if (attempt > 0) {
core.info(`Sleeping for ${delayBetweenRetriesOnExpectationFailure}ms before retrying.`);
await delay(delayBetweenRetriesOnExpectationFailure);
}
try {
core.info(`Triggering experiment ${getExperimentSummary(experiment)} for attempt ${attempt + 1}/${maximumAttempts}.`);
const executionUrl = await steadybitAPI.runExperiment(experimentKey, parallelExecution, maxRetries);
core.debug(`Experiment ${getExperimentSummary(experiment)} is running, checking status...`);
lastResult = await steadybitAPI.awaitExecutionState(executionUrl, expectedState, expectedReason);
} catch (error) {
lastError = error;
}
}
if (lastError) {
core.setFailed(`Experiment ${getExperimentSummary(experiment)} failed: ${lastError}`);
} else {
core.info(`Experiment ${getExperimentSummary(experiment)} ended. ${lastResult}`);
}
};