From 0171baf65926949fd6b6472a5dd4e10097f20822 Mon Sep 17 00:00:00 2001
From: Luca Bilke <luca@bil.ke>
Date: Wed, 16 Oct 2024 11:14:01 +0200
Subject: [PATCH] openproject_hours: new script

---
 common/.local/bin/openproject_hours | 49 +++++++++++++++++++++++++++++
 1 file changed, 49 insertions(+)
 create mode 100755 common/.local/bin/openproject_hours

diff --git a/common/.local/bin/openproject_hours b/common/.local/bin/openproject_hours
new file mode 100755
index 00000000..53d8b694
--- /dev/null
+++ b/common/.local/bin/openproject_hours
@@ -0,0 +1,49 @@
+#!/bin/sh
+# shellcheck disable=2089,2090
+
+year=$1
+month=$2
+
+USER_ID=12
+API_KEY=$(cat "${HOME}/.secrets/openproject_apikey")
+API_URL=https://op.tralios.de
+PAGE_SIZE=1000
+
+filters='[
+    {
+        "spentOn": {
+            "operator": "<>d",
+            "values": ["'"${year}-${month}-01"'", "'"${year}-${month}-30"'"]
+        }
+    },
+    {
+        "user": {
+            "operator": "=",
+            "values": ["'"$USER_ID"'"]
+        }
+    }
+]'
+
+curl -s \
+    -u "apikey:${API_KEY}" \
+    -G "${API_URL}/api/v3/time_entries" \
+    --data-urlencode "filters=${filters}" \
+    --data-urlencode "pageSize=${PAGE_SIZE}" |
+    jq -r '._embedded.elements | map(.hours) | join(" ")' |
+    awk '{
+        gsub(/PT/, "");
+        gsub(/H/, "H ");
+        gsub(/M/, "M ");
+        for (i=1; i<=NF; i++) {
+            if ($i ~ /H/) {
+                split($i, a, "H");
+                hours += a[1];
+            } else if ($i ~ /M/) {
+                split($i, a, "M");
+                hours += a[1] / 60;
+            }
+        }
+    }
+    END {
+        printf "%.2f\n", hours;
+    }'