aboutsummaryrefslogtreecommitdiff
path: root/kanboard_automation/kanboard_automation.php
blob: 730754a490c1b5d0a1cc2912a3b91c284ff57b3d (plain) (blame)
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
<?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));
    }
  }

?>