/////////////////////////////////////////////////////////////////////////// The on-modify event is triggered separately for each task added or modified Input: - line of JSON for the original task - line of JSON for the modified task, the diff being the modification Output: - emitted JSON for the input task is saved, if the exit code is zero, otherwise ignored. - all emitted non-JSON lines are co
| 276 | // depending on the status code. |
| 277 | // |
| 278 | void Hooks::onModify(Task& before, Task& after) const { |
| 279 | if (!_enabled) return; |
| 280 | |
| 281 | Timer timer; |
| 282 | |
| 283 | std::vector<std::string> matchingScripts = scripts("on-modify"); |
| 284 | if (matchingScripts.size()) { |
| 285 | // Convert vector of tasks to a vector of strings. |
| 286 | std::vector<std::string> input; |
| 287 | input.push_back(before.composeJSON()); // [line 0] original, never changes |
| 288 | input.push_back(after.composeJSON()); // [line 1] modified |
| 289 | |
| 290 | // Call the hook scripts. |
| 291 | for (auto& script : matchingScripts) { |
| 292 | std::vector<std::string> output; |
| 293 | int status = callHookScript(script, input, output); |
| 294 | |
| 295 | std::vector<std::string> outputJSON; |
| 296 | std::vector<std::string> outputFeedback; |
| 297 | separateOutput(output, outputJSON, outputFeedback); |
| 298 | |
| 299 | if (status == 0) { |
| 300 | assertNTasks(outputJSON, 1, script); |
| 301 | assertValidJSON(outputJSON, script); |
| 302 | assertSameTask(outputJSON, before, script); |
| 303 | |
| 304 | // Propagate accepted changes forward to the next script. |
| 305 | input[1] = outputJSON[0]; |
| 306 | |
| 307 | for (auto& message : outputFeedback) Context::getContext().footnote(message); |
| 308 | } else { |
| 309 | assertFeedback(outputFeedback, script); |
| 310 | for (auto& message : outputFeedback) Context::getContext().error(message); |
| 311 | |
| 312 | throw 0; // This is how hooks silently terminate processing. |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | after = Task(input[1]); |
| 317 | } |
| 318 | |
| 319 | Context::getContext().time_hooks_us += timer.total_us(); |
| 320 | } |
| 321 | |
| 322 | //////////////////////////////////////////////////////////////////////////////// |
| 323 | std::vector<std::string> Hooks::list() const { return _scripts; } |
no test coverage detected