+#!/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(", ")
+ ) + "\"")'