]> git.r.bdr.sh - rbdr/r.bdr.sh/blob - swiftbar/sourcehut-builds.1m.sh
For some reason setting that value to 10 breaks it
[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 # <swiftbar.hideRunInTerminal>true</swiftbar.hideRunInTerminal>
13 # <swiftbar.environment>VAR_SOURCEHUT_API_TOKEN=no, VAR_BUILD_COUNT=9</swiftbar.environment>
14
15 ###############################################################################
16 # The Variables
17 ###############################################################################
18 VAR_SOURCEHUT_API_TOKEN=${VAR_SOURCEHUT_API_TOKEN:-'ADD YOUR TOKEN HERE'}
19 VAR_BUILD_COUNT=${VAR_BUILD_COUNT:-10}
20
21 ###############################################################################
22 # The Code
23 ###############################################################################
24
25 curl \
26 --oauth2-bearer "$VAR_SOURCEHUT_API_TOKEN" \
27 -H 'Content-Type: application/json' \
28 -d '{"query": "query { me { canonicalName jobs { results { id created updated status note tags tasks { name status } } } } }"}' \
29 https://builds.sr.ht/query 2>/dev/null | \
30 jq -r --arg count "$VAR_BUILD_COUNT" --arg now "$(date +%s)" '
31 .data.me.canonicalName as $id |
32 (
33 [.data.me.jobs.results[0:($count|tonumber)] | .[] |
34 select(
35 (($now|tonumber) - ((.created|sub("\\.[0-9]+Z$"; "Z")|fromdate|tostring)|tonumber)) < 900 and
36 .status == "FAILED"
37 )] | length
38 ) as $failed_count |
39 (
40 [.data.me.jobs.results[0:($count|tonumber)] | .[] |
41 select(
42 (($now|tonumber) - ((.created|sub("\\.[0-9]+Z$"; "Z")|fromdate|tostring)|tonumber)) < 900 and
43 .status == "SUCCESS"
44 )] | length
45 ) as $success_count |
46 (
47 [.data.me.jobs.results[0:($count|tonumber)] | .[] |
48 select(
49 (($now|tonumber) - ((.created|sub("\\.[0-9]+Z$"; "Z")|fromdate|tostring)|tonumber)) < 900 and
50 .status == "RUNNING"
51 )] | length
52 ) as $running_count |
53 (
54 ":gearshape.arrow.trianglehead.2.clockwise.rotate.90:" +
55 (if $success_count > 0 then ":checkmark:\($success_count) " else "" end) +
56 (if $failed_count > 0 then ":xmark:\($failed_count) " else "" end) +
57 (if $running_count > 0 then ":play.circle: \($running_count) " else "" end)
58 ),
59 ("---"),
60 (.data.me.jobs.results[0:($count|tonumber)] | .[] | (
61 if .status == "PENDING" then ":circle.dotted:"
62 elif .status == "QUEUED" then ":circle:"
63 elif .status == "RUNNING" then ":play.circle:"
64 elif .status == "SUCCESS" then ":checkmark:"
65 elif .status == "FAILED" then ":xmark:"
66 elif .status == "TIMEOUT" then ":clock.badge.xmark:"
67 elif .status == "CANCELLED" then ":circle.slash:"
68 else "?" end
69 ) + " " + $id + "/" + .tags[0] + " (" + (.id|tostring) + ") " + (
70 [.tasks[] |
71 if .status == "PENDING" then ":circle.dotted:"
72 elif .status == "RUNNING" then ":play.circle:"
73 elif .status == "SUCCESS" then ":checkmark:"
74 elif .status == "FAILED" then ":xmark:"
75 elif .status == "SKIPPED" then ":circle.slash:"
76 else "?" end
77 ] | join("")
78 ) + " | href=https://builds.sr.ht/" + $id +"/job/" + (.id|tostring) + " tooltip=\"" + (
79 [.tasks[] | .name + ": " + .status] | join(", ")
80 ) + "\"")'