|
|
@@ -4435,6 +4435,98 @@ static void setupSmtpAPI(JSContext* ctx, JSValue smartbotic) {
|
|
|
JS_SetPropertyStr(ctx, smartbotic, "smtp", smtp_obj);
|
|
|
}
|
|
|
|
|
|
+// workflows.setActive(id, active) - switch another workflow on or off
|
|
|
+static JSValue js_workflows_set_active(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
|
+ const ScriptContext* script_ctx = static_cast<const ScriptContext*>(JS_GetContextOpaque(ctx));
|
|
|
+ JSValue response = JS_NewObject(ctx);
|
|
|
+
|
|
|
+ if (!script_ctx || !script_ctx->workflow_set_active) {
|
|
|
+ JS_SetPropertyStr(ctx, response, "success", JS_FALSE);
|
|
|
+ JS_SetPropertyStr(ctx, response, "error",
|
|
|
+ JS_NewString(ctx, "Workflow control is not available on this runner"));
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ if (argc < 1 || !JS_IsString(argv[0])) {
|
|
|
+ JS_SetPropertyStr(ctx, response, "success", JS_FALSE);
|
|
|
+ JS_SetPropertyStr(ctx, response, "error",
|
|
|
+ JS_NewString(ctx, "workflows.setActive needs a workflow id"));
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ const char* id = JS_ToCString(ctx, argv[0]);
|
|
|
+ const std::string workflow_id = id ? id : "";
|
|
|
+ if (id) JS_FreeCString(ctx, id);
|
|
|
+ const bool active = argc > 1 ? static_cast<bool>(JS_ToBool(ctx, argv[1])) : true;
|
|
|
+
|
|
|
+ auto result = script_ctx->workflow_set_active(workflow_id, active);
|
|
|
+ if (result.failed()) {
|
|
|
+ JS_SetPropertyStr(ctx, response, "success", JS_FALSE);
|
|
|
+ JS_SetPropertyStr(ctx, response, "error", JS_NewString(ctx, result.error().message().c_str()));
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ JS_SetPropertyStr(ctx, response, "success", JS_TRUE);
|
|
|
+ JS_SetPropertyStr(ctx, response, "active", active ? JS_TRUE : JS_FALSE);
|
|
|
+ // False when the workflow was already in that state - nothing happened,
|
|
|
+ // which is worth being able to tell apart from having done something.
|
|
|
+ JS_SetPropertyStr(ctx, response, "changed", result.value() ? JS_TRUE : JS_FALSE);
|
|
|
+ return response;
|
|
|
+}
|
|
|
+
|
|
|
+// workflows.get(id) - read another workflow's state
|
|
|
+static JSValue js_workflows_get(JSContext* ctx, JSValue this_val, int argc, JSValue* argv) {
|
|
|
+ const ScriptContext* script_ctx = static_cast<const ScriptContext*>(JS_GetContextOpaque(ctx));
|
|
|
+ JSValue response = JS_NewObject(ctx);
|
|
|
+
|
|
|
+ if (!script_ctx || !script_ctx->workflow_get_state) {
|
|
|
+ JS_SetPropertyStr(ctx, response, "success", JS_FALSE);
|
|
|
+ JS_SetPropertyStr(ctx, response, "error",
|
|
|
+ JS_NewString(ctx, "Workflow control is not available on this runner"));
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ if (argc < 1 || !JS_IsString(argv[0])) {
|
|
|
+ JS_SetPropertyStr(ctx, response, "success", JS_FALSE);
|
|
|
+ JS_SetPropertyStr(ctx, response, "error",
|
|
|
+ JS_NewString(ctx, "workflows.get needs a workflow id"));
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ const char* id = JS_ToCString(ctx, argv[0]);
|
|
|
+ const std::string workflow_id = id ? id : "";
|
|
|
+ if (id) JS_FreeCString(ctx, id);
|
|
|
+
|
|
|
+ auto result = script_ctx->workflow_get_state(workflow_id);
|
|
|
+ if (result.failed()) {
|
|
|
+ JS_SetPropertyStr(ctx, response, "success", JS_FALSE);
|
|
|
+ JS_SetPropertyStr(ctx, response, "error", JS_NewString(ctx, result.error().message().c_str()));
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ const auto& state = result.value();
|
|
|
+ JS_SetPropertyStr(ctx, response, "success", JS_TRUE);
|
|
|
+ JS_SetPropertyStr(ctx, response, "name", JS_NewString(ctx, state.name.c_str()));
|
|
|
+ JS_SetPropertyStr(ctx, response, "active", state.active ? JS_TRUE : JS_FALSE);
|
|
|
+ JS_SetPropertyStr(ctx, response, "consecutiveFailures",
|
|
|
+ JS_NewInt32(ctx, state.consecutive_failures));
|
|
|
+ JS_SetPropertyStr(ctx, response, "deactivateAfterFailures",
|
|
|
+ JS_NewInt32(ctx, state.deactivate_after_failures));
|
|
|
+ JS_SetPropertyStr(ctx, response, "deactivatedReason",
|
|
|
+ JS_NewString(ctx, state.deactivated_reason.c_str()));
|
|
|
+ JS_SetPropertyStr(ctx, response, "publishedVersion",
|
|
|
+ JS_NewInt64(ctx, state.published_version));
|
|
|
+ return response;
|
|
|
+}
|
|
|
+
|
|
|
+// Setup workflow control API on smartbotic namespace
|
|
|
+static void setupWorkflowsAPI(JSContext* ctx, JSValue smartbotic) {
|
|
|
+ JSValue workflows = JS_NewObject(ctx);
|
|
|
+ JS_SetPropertyStr(ctx, workflows, "setActive",
|
|
|
+ JS_NewCFunction(ctx, js_workflows_set_active, "setActive", 2));
|
|
|
+ JS_SetPropertyStr(ctx, workflows, "get",
|
|
|
+ JS_NewCFunction(ctx, js_workflows_get, "get", 1));
|
|
|
+ JS_SetPropertyStr(ctx, smartbotic, "workflows", workflows);
|
|
|
+}
|
|
|
+
|
|
|
// Setup credentials API on smartbotic namespace
|
|
|
static void setupCredentialsAPI(JSContext* ctx, JSValue smartbotic) {
|
|
|
JSValue credentials = JS_NewObject(ctx);
|
|
|
@@ -4759,6 +4851,7 @@ ScriptResult ScriptEngine::execute(const std::string& script, const ScriptContex
|
|
|
if (JS_IsObject(smartbotic)) {
|
|
|
setupStorageAPI(context_, smartbotic);
|
|
|
setupCredentialsAPI(context_, smartbotic);
|
|
|
+ setupWorkflowsAPI(context_, smartbotic);
|
|
|
setupImapAPI(context_, smartbotic);
|
|
|
setupSmtpAPI(context_, smartbotic);
|
|
|
#ifdef MYSQL_SUPPORT
|