diff options
author | Thorsten Ortlepp <post@ortlepp.eu> | 2024-11-05 23:23:02 +0100 |
---|---|---|
committer | Thorsten Ortlepp <post@ortlepp.eu> | 2024-11-05 23:23:02 +0100 |
commit | 3d239c579959978a6374098edb10453e85b157c1 (patch) | |
tree | 64baf3c5a42f0e511ee8ec23f0bcbec8fdd3de0f | |
parent | 0ae279aa6279a7862343d74a45227e6298b2669c (diff) | |
download | php-stuff-3d239c579959978a6374098edb10453e85b157c1.zip |
added kanboard_automation
-rw-r--r-- | README | 7 | ||||
-rw-r--r-- | kanboard_automation/kanboard_automation.php | 174 |
2 files changed, 181 insertions, 0 deletions
@@ -9,6 +9,13 @@ Deutschlandfunk Kalenderblatt which is only available online and as RSS feed but not as podcast. Requires PHP 8. +kanboard_automation +------------------- +Some automation for Kanboard (kanboard.org). The script is designed +to run as a cronjob to automate recurring actions, e.g. creating +tasks on a daily basis. + + proxy ----- A very simple kind of proxy. It adds an HTTP header for Basic diff --git a/kanboard_automation/kanboard_automation.php b/kanboard_automation/kanboard_automation.php new file mode 100644 index 0000000..730754a --- /dev/null +++ b/kanboard_automation/kanboard_automation.php @@ -0,0 +1,174 @@ +<?php + define("KANBOARD_URL", "https://example.com/jsonrpc.php"); + define("KANBOARD_USER", "jsonrpc"); + define("KANBOARD_TOKEN", "YOUR_SECRET_TOKEN"); + + define("KB_USER", 1); + define("KB_PROJECT", 1); + define("KB_TASK_COLUMN", 1); + define("KB_TASK_CATEGORY", 1); + define("KB_TASK_SWIMLANE", 1); + + + function get_id() { + global $id; + $id++; + return $id; + } + + function get_tags() { + return [ + "jsonrpc" => "2.0", + "method" => "getTagsByProject", + "id" => get_id(), + "params" => [KB_PROJECT] + ]; + } + + function remove_tag($tag_id) { + return [ + "jsonrpc" => "2.0", + "method" => "removeTag", + "id" => get_id(), + "params" => [$tag_id] + ]; + } + + function create_tag($title, $color) { + return [ + "jsonrpc" => "2.0", + "method" => "createTag", + "id" => get_id(), + "params" => [ + "project_id" => KB_PROJECT, + "tag" => $title, + "color_id" => $color + ] + ]; + } + + function create_task($title, $tag, $color, $date_due) { + return [ + "jsonrpc" => "2.0", + "method" => "createTask", + "id" => get_id(), + "params" => [ + "title" => $title, + "project_id" => KB_PROJECT, + "color_id" => $color, + "column_id" => KB_TASK_COLUMN, + "owner_id" => KB_USER, + "creator_id" => KB_USER, + "date_due" => $date_due, + "category_id" => KB_TASK_CATEGORY, + "swimlane_id" => KB_TASK_SWIMLANE, + "priority" => 1, + "tags" => [$tag] + ] + ]; + } + + function move_task_position($task_id, $position) { + return [ + "jsonrpc" => "2.0", + "method" => "moveTaskPosition", + "id" => get_id(), + "params" => [ + "project_id" => KB_PROJECT, + "task_id" => $task_id, + "column_id" => KB_TASK_COLUMN, + "position" => $position, + "swimlane_id" => KB_TASK_SWIMLANE + ] + ]; + } + + function create_subtask($task_id, $title) { + return [ + "jsonrpc" => "2.0", + "method" => "createSubtask", + "id" => get_id(), + "params" => [ + "task_id" => $task_id, + "title" => $title, + "user_id" => KB_USER, + "status" => 0 + ] + ]; + } + + function send_request($payload) { + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, KANBOARD_URL); + curl_setopt($curl, CURLOPT_USERPWD, KANBOARD_USER.":".KANBOARD_TOKEN); + curl_setopt($curl, CURLOPT_POST, true); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload)); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_FAILONERROR, true); + $response = curl_exec($curl); + curl_close($curl); + return $response; + } + + + + $now = new DateTime('now', new DateTimeZone('Europe/Berlin')); + $id = time(); + + + // --- tags --- \\ + + // define tag + $tag_color = "blue"; + + // old tag to be removed + $old_tag_date = new DateTime('now', new DateTimeZone('Europe/Berlin')); + date_sub($old_tag_date, DateInterval::createFromDateString('4 week')); + $old_tag = $old_tag_date->format('Y-W'); + + // new tag to be created + $new_tag_date = new DateTime('now', new DateTimeZone('Europe/Berlin')); + date_add($new_tag_date, DateInterval::createFromDateString('4 week')); + $new_tag = $new_tag_date->format('Y-W'); + + // get existing tags + $tags = json_decode(send_request(get_tags()))->result; + + // remove old tag, create new tag + $new_tag_exists = FALSE; + foreach ($tags as $tag) { + if ($tag->name == $old_tag) { + send_request(remove_tag($tag->id)); + } + if ($tag->name == $new_tag) { + $new_tag_exists = TRUE; + } + } + if ($new_tag_exists == FALSE) { + send_request(create_tag($new_tag, $tag_color)); + } + + + // --- tasks --- \\ + + // define task and subtasks + $task_title = "My Daily Task ".$now->format('d.m.Y'); + $task_tag = $now->format('Y-W'); + $task_color = "green"; + $task_datedue = $now->format('Y-m-d')." 12:30"; + $task_subtasks = array("First Subtask", "Second Subtask"); + + // create task + $response = send_request(create_task($task_title, $task_tag, $task_color, $task_datedue)); + $created_task_id = json_decode($response)->result; + + // move task to top & create subtasks + if (is_int($created_task_id)) { + send_request(move_task_position($created_task_id, 1)); + + foreach ($task_subtasks as $subtask_title) { + send_request(create_subtask($created_task_id, $subtask_title)); + } + } + +?> |