add message with workflow url

This commit is contained in:
Daylin Morgan 2024-11-15 15:21:34 -06:00
parent 6adc7e7f7d
commit dd5ee96b87
Signed by: daylin
GPG key ID: 950D13E9719334AD

View file

@ -1,4 +1,4 @@
import std/[httpclient,logging, os, strformat, strutils, json, tables, tempfiles] import std/[httpclient,logging, os, strformat, strutils, json, tables, tempfiles, times]
import jsony, hwylterm, hwylterm/logging, zippy/ziparchives import jsony, hwylterm, hwylterm/logging, zippy/ziparchives
import ./[exec, context] import ./[exec, context]
@ -12,6 +12,33 @@ template withTmpDir(body: untyped): untyped =
body body
removeDir tmpDir removeDir tmpDir
type
GhArtifact = object
id: int
name: string
url: string
archive_download_url*: string
GhWorkflowRun = object
id*: int
node_id: string
run_number: int
event: string
status: string
conclusion: string
html_url: string
workflow_id: int
created_at*: string # use datetime?
updated_at: string # use datetime?
ListGhArtifactResponse = object
total_count: int
artifacts: seq[GhArtifact]
ListGhWorkflowResponse = object
total_count: int
workflow_runs: seq[GhWorkflowRun]
var ghToken = getEnv "GITHUB_TOKEN" var ghToken = getEnv "GITHUB_TOKEN"
proc checkToken() {.inline.} = proc checkToken() {.inline.} =
@ -52,47 +79,35 @@ proc postGhApi(url: string, body: JsonNode) =
except: except:
errorQuit "failed to get response code" errorQuit "failed to get response code"
proc getInProgressRun(workflow: string, timeout: int = 5000): (GhWorkflowRun, bool) =
let
start = now()
timeoutDuration = initDuration(milliseconds = timeout)
withSpinner fmt"waiting for {workflow} workflow to start":
while (now() - start) < timeoutDuration:
let response = getGhApi(fmt"https://api.github.com/repos/daylinmorgan/oizys/actions/workflows/{workflow}/runs")
let runs = fromJson(response.body, ListGhWorkflowResponse).workflow_runs
if runs[0].status == "in_progress":
spinner.stop() # cleanup
return (runs[0], true)
sleep 500
warn "timeout reached waiting for workflow to start"
proc createDispatch*(workflowFileName: string, `ref`: string) = proc createDispatch*(workflowFileName: string, `ref`: string) =
## https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event ## https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#create-a-workflow-dispatch-event
var workflow = let workflow =
if workflowFileName.endsWith(".yml") or workflowFileName.endsWith(".yaml"): workflowFileName if workflowFileName.endsWith(".yml") or workflowFileName.endsWith(".yaml"): workflowFileName
else: workflowFileName & ".yml" else: workflowFileName & ".yml"
let body = %*{"ref": `ref`}
info fmt"creating dispatch event for {workflow}" info fmt"creating dispatch event for {workflow}"
postGhApi( postGhApi(
fmt"https://api.github.com/repos/daylinmorgan/oizys/actions/workflows/{workflow}/dispatches", fmt"https://api.github.com/repos/daylinmorgan/oizys/actions/workflows/{workflow}/dispatches",
%*{ body
"ref": `ref`,
}
) )
let (run, ok) = getInProgressRun(workflow)
type if ok: info "view workflow run at: " & run.html_url
GhArtifact = object
id: int
name: string
url: string
archive_download_url*: string
GhWorkflowRun = object
id*: int
node_id: string
run_number: int
event: string
status: string
conclusion: string
html_url: string
workflow_id: int
created_at*: string # use datetime?
updated_at: string # use datetime?
ListGhArtifactResponse = object
total_count: int
artifacts: seq[GhArtifact]
ListGhWorkflowResponse = object
total_count: int
workflow_runs: seq[GhWorkflowRun]
proc listUpdateRuns(): seq[GhWorkflowRun] = proc listUpdateRuns(): seq[GhWorkflowRun] =
## get update.yml runs ## get update.yml runs