]> git.r.bdr.sh - rbdr/r.bdr.sh/commitdiff
Add swiftbar page
authorRuben Beltran del Rio <redacted>
Thu, 26 Dec 2024 14:40:03 +0000 (15:40 +0100)
committerRuben Beltran del Rio <redacted>
Thu, 26 Dec 2024 14:40:03 +0000 (15:40 +0100)
swiftbar/sourcehut-builds.1m.sh [new file with mode: 0644]
swiftbar_scripts.gmi [new file with mode: 0644]

diff --git a/swiftbar/sourcehut-builds.1m.sh b/swiftbar/sourcehut-builds.1m.sh
new file mode 100644 (file)
index 0000000..6ad4340
--- /dev/null
@@ -0,0 +1,80 @@
+#!/usr/bin/env bash
+# This plugin allows you to see the latest sourcehut builds and their status.
+#
+#  <xbar.title>Sourcehut Builds</xbar.title>
+#  <xbar.version>v1.0.0</xbar.version>
+#  <xbar.author>Rubén Beltrán del Río</xbar.author>
+#  <xbar.desc>Shows the status of your latest builds in sourcehut.</xbar.desc>
+#  <xbar.dependencies>curl,jq</xbar.dependencies>
+#  <xbar.abouturl>https://r.bdr.sh/swiftbar_scripts.html</xbar.abouturl>
+#  <xbar.var>string(VAR_SOURCEHUT_API_TOKEN): Your sourcehut API Token.</xbar.var>
+#  <xbar.var>number(VAR_BUILD_COUNT=5): How many items to show.</xbar.var>
+#  <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
+#  <swiftbar.environment>VAR_SOURCEHUT_API_TOKEN=no, VAR_BUILD_COUNT=5</swiftbar.environment>
+
+###############################################################################
+# The Variables
+###############################################################################
+VAR_SOURCEHUT_API_TOKEN=${VAR_SOURCEHUT_API_TOKEN:-'ADD YOUR TOKEN HERE'}
+VAR_BUILD_COUNT=${VAR_BUILD_COUNT:-5}
+
+###############################################################################
+# The Code
+###############################################################################
+
+curl \
+  --oauth2-bearer "$VAR_SOURCEHUT_API_TOKEN" \
+  -H 'Content-Type: application/json' \
+  -d '{"query": "query { me { canonicalName jobs { results { id created updated status note tags tasks { name status } } } } }"}' \
+  https://builds.sr.ht/query 2>/dev/null | \
+jq -r --arg count "$VAR_BUILD_COUNT" --arg now "$(date +%s)" '
+.data.me.canonicalName as $id |
+(
+    [.data.me.jobs.results[0:($count|tonumber)] | .[] |
+     select(
+       (($now|tonumber) - ((.created|sub("\\.[0-9]+Z$"; "Z")|fromdate|tostring)|tonumber)) < 900 and 
+       .status == "FAILED"
+     )] | length
+) as $failed_count |
+(
+    [.data.me.jobs.results[0:($count|tonumber)] | .[] |
+     select(
+       (($now|tonumber) - ((.created|sub("\\.[0-9]+Z$"; "Z")|fromdate|tostring)|tonumber)) < 900 and 
+       .status == "SUCCESS"
+     )] | length
+) as $success_count |
+(
+    [.data.me.jobs.results[0:($count|tonumber)] | .[] |
+     select(
+       (($now|tonumber) - ((.created|sub("\\.[0-9]+Z$"; "Z")|fromdate|tostring)|tonumber)) < 900 and 
+       .status == "RUNNING"
+     )] | length
+) as $running_count |
+  (
+    ":gearshape.arrow.trianglehead.2.clockwise.rotate.90:" + 
+    (if $success_count > 0 then ":checkmark:\($success_count) " else "" end) +
+    (if $failed_count > 0 then ":xmark:\($failed_count) " else "" end) +
+    (if $running_count > 0 then ":play.circle: \($running_count) " else "" end)
+  ),
+  ("---"),
+  (.data.me.jobs.results[0:($count|tonumber)] | .[] | (
+    if .status == "PENDING" then ":circle.dotted:"
+    elif .status == "QUEUED" then ":circle:"
+    elif .status == "RUNNING" then ":play.circle:"
+    elif .status == "SUCCESS" then ":checkmark:"
+    elif .status == "FAILED" then ":xmark:"
+    elif .status == "TIMEOUT" then ":clock.badge.xmark:"
+    elif .status == "CANCELLED" then ":circle.slash:"
+    else "?" end
+  ) + " " + $id + "/" + .tags[0] + " (" + (.id|tostring) + ") " + (
+    [.tasks[] | 
+      if .status == "PENDING" then ":circle.dotted:"
+      elif .status == "RUNNING" then ":play.circle:"
+      elif .status == "SUCCESS" then ":checkmark:"
+      elif .status == "FAILED" then ":xmark:"
+      elif .status == "SKIPPED" then ":circle.slash:"
+      else "?" end
+    ] | join("")
+  ) + " | href=https://builds.sr.ht/" + $id +"/job/" + (.id|tostring) + " tooltip=\"" + (
+    [.tasks[] | .name + ": " + .status] | join(", ")
+    ) + "\"")'
diff --git a/swiftbar_scripts.gmi b/swiftbar_scripts.gmi
new file mode 100644 (file)
index 0000000..1e2bb80
--- /dev/null
@@ -0,0 +1,22 @@
+## SwiftBar Scripts 
+
+This page holds the SwiftBar[1] / xbar scripts I've made. Since I use the first
+one, I'm not sure whether they work with xbar[2], but should probably be OK.
+
+=> https://swiftbar.app/ [1] SwiftBar
+=> https://xbarapp.com/ [2] Xbar
+
+## I.
+### Sourcehut Builds
+
+View the status of your last few builds in sourcehut. You can open the URL by
+clicking on the menu items.
+The count of failed, pending and successful runs of the last 15m are shown
+next to the icon.
+
+To use the plugin you'll need to set your sourcehut token in the
+VAR_SOURCEHUT_API_TOKEN variable (See the 'ADD YOUR TOKEN HERE' string). You
+can also control how many builds to show by changing the VAR_BUILD_COUNT
+variable (defaults to 5)
+
+=> ./swiftbar/sourcehut-builds.1m.sh ⧇ source