Edits history of script submission #23228 for ' List Workflow Runs (github)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    export type DynSelect_workflow_id = string
    
    // Dropdown of the repository's workflows; fill owner and repo first.
    export async function workflow_id(
      auth: RT.Github,
      owner: string,
      repo: string
    ) {
      if (!auth?.token || !owner || !repo) return []
      const response = await fetch(
        `https://api.github.com/repos/${owner}/${repo}/actions/workflows?per_page=100`,
        {
          headers: {
            Authorization: `Bearer ${auth.token}`,
            Accept: "application/vnd.github+json",
            "X-GitHub-Api-Version": "2022-11-28",
          },
        }
      )
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
      const { workflows } = (await response.json()) as {
        workflows: { id: number; name: string; path: string }[]
      }
      return workflows.map((w) => ({
        value: String(w.id),
        label: `${w.name} (${w.path})`,
      }))
    }
    
    /**
     * List Workflow Runs
     * List GitHub Actions runs of a repository, or of one workflow when workflow_id is set, filtered by branch, event or status. Paginate with page.
     */
    export async function main(
      auth: RT.Github,
      owner: string,
      repo: string,
      workflow_id: DynSelect_workflow_id | undefined,
      branch: string | undefined,
      event: string | undefined,
      status:
        | "completed"
        | "action_required"
        | "cancelled"
        | "failure"
        | "neutral"
        | "skipped"
        | "stale"
        | "success"
        | "timed_out"
        | "in_progress"
        | "queued"
        | "requested"
        | "waiting"
        | "pending"
        | undefined,
      per_page: number = 30,
      page: number = 1
    ) {
      const base = `https://api.github.com/repos/${owner}/${repo}/actions`
      const url = new URL(
        workflow_id ? `${base}/workflows/${workflow_id}/runs` : `${base}/runs`
      )
      for (const [k, v] of Object.entries({
        branch,
        event,
        status,
        per_page,
        page,
      })) {
        if (v !== undefined && v !== "") url.searchParams.append(k, String(v))
      }
    
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/vnd.github+json",
          "X-GitHub-Api-Version": "2022-11-28",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 2 hours ago