]> git.r.bdr.sh - rbdr/r.bdr.sh/blob - swiftbar/sourcehut-builds.1m.sh
Add fediverse creator meta tag
[rbdr/r.bdr.sh] / swiftbar / sourcehut-builds.1m.sh
1 #!/usr/bin/env bash
2 # This plugin allows you to see the latest sourcehut builds and their status.
3 #
4 # <xbar.title>Sourcehut Builds</xbar.title>
5 # <xbar.version>v1.0.0</xbar.version>
6 # <xbar.author>Rubén Beltrán del Río</xbar.author>
7 # <xbar.desc>Shows the status of your latest builds in sourcehut.</xbar.desc>
8 # <xbar.dependencies>curl,jq</xbar.dependencies>
9 # <xbar.abouturl>https://r.bdr.sh/swiftbar_scripts.html</xbar.abouturl>
10 # <xbar.var>string(VAR_SOURCEHUT_API_TOKEN): Your sourcehut API Token.</xbar.var>
11 # <xbar.var>number(VAR_BUILD_COUNT=10): How many items to show.</xbar.var>
12 # <xbar.var>string(VAR_RUN_ON_FOCUS): Focus in which to run.</xbar.var>
13 # <xbar.var>string(VAR_HALT_ON_FOCUS): Focus in which to halt.</xbar.var>
14 # <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
15 # <swiftbar.environment>VAR_SOURCEHUT_API_TOKEN=no, VAR_BUILD_COUNT=9, VAR_RUN_ON_FOCUS=, VAR_HALT_ON_FOCUS=</swiftbar.environment>
16
17 ###############################################################################
18 # The Variables
19 ###############################################################################
20 VAR_SOURCEHUT_API_TOKEN=${VAR_SOURCEHUT_API_TOKEN:-'ADD YOUR TOKEN HERE'}
21 VAR_BUILD_COUNT=${VAR_BUILD_COUNT:-10}
22 VAR_RUN_ON_FOCUS=
23 VAR_HALT_ON_FOCUS=
24
25 ###############################################################################
26 # Focus Check
27 ###############################################################################
28
29 if [[ -f "${BASH_SOURCE%/*}/focus.utils.sh" ]]; then
30 source "${BASH_SOURCE%/*}/focus.utils.sh"
31 focus_check
32 fi
33
34 ###############################################################################
35 # The Code
36 ###############################################################################
37
38 curl \
39 --oauth2-bearer "$VAR_SOURCEHUT_API_TOKEN" \
40 -H 'Content-Type: application/json' \
41 -d '{"query": "query { me { canonicalName jobs { results { id created updated status note tags tasks { name status } } } } }"}' \
42 https://builds.sr.ht/query 2>/dev/null | \
43 jq -r --arg count "$VAR_BUILD_COUNT" --arg now "$(date +%s)" '
44 .data.me.canonicalName as $id |
45 (
46 [.data.me.jobs.results[0:($count|tonumber)] | .[] |
47 select(
48 (($now|tonumber) - ((.created|sub("\\.[0-9]+Z$"; "Z")|fromdate|tostring)|tonumber)) < 900 and
49 .status == "FAILED"
50 )] | length
51 ) as $failed_count |
52 (
53 [.data.me.jobs.results[0:($count|tonumber)] | .[] |
54 select(
55 (($now|tonumber) - ((.created|sub("\\.[0-9]+Z$"; "Z")|fromdate|tostring)|tonumber)) < 900 and
56 .status == "SUCCESS"
57 )] | length
58 ) as $success_count |
59 (
60 [.data.me.jobs.results[0:($count|tonumber)] | .[] |
61 select(
62 (($now|tonumber) - ((.created|sub("\\.[0-9]+Z$"; "Z")|fromdate|tostring)|tonumber)) < 900 and
63 .status == "RUNNING"
64 )] | length
65 ) as $running_count |
66 (
67 ":gearshape.arrow.trianglehead.2.clockwise.rotate.90:" +
68 (if $success_count > 0 then ":checkmark:\($success_count) " else "" end) +
69 (if $failed_count > 0 then ":xmark:\($failed_count) " else "" end) +
70 (if $running_count > 0 then ":play.circle: \($running_count) " else "" end)
71 ),
72 ("---"),
73 (.data.me.jobs.results[0:($count|tonumber)] | .[] | (
74 if .status == "PENDING" then ":circle.dotted:"
75 elif .status == "QUEUED" then ":circle:"
76 elif .status == "RUNNING" then ":play.circle:"
77 elif .status == "SUCCESS" then ":checkmark:"
78 elif .status == "FAILED" then ":xmark:"
79 elif .status == "TIMEOUT" then ":clock.badge.xmark:"
80 elif .status == "CANCELLED" then ":circle.slash:"
81 else "?" end
82 ) + " " + $id + "/" + .tags[0] + " (" + (.id|tostring) + ") " + (
83 [.tasks[] |
84 if .status == "PENDING" then ":circle.dotted:"
85 elif .status == "RUNNING" then ":play.circle:"
86 elif .status == "SUCCESS" then ":checkmark:"
87 elif .status == "FAILED" then ":xmark:"
88 elif .status == "SKIPPED" then ":circle.slash:"
89 else "?" end
90 ] | join("")
91 ) + " | href=https://builds.sr.ht/" + $id +"/job/" + (.id|tostring) + " tooltip=\"" + (
92 [.tasks[] | .name + ": " + .status] | join(", ")
93 ) + "\"")'