Edits history of script submission #23202 for ' Enable Workflow (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})`,
      }))
    }
    
    /**
     * Enable Workflow
     * Enable a GitHub Actions workflow so it runs again on its triggers.
     */
    export async function main(
      auth: RT.Github,
      owner: string,
      repo: string,
      workflow_id: DynSelect_workflow_id
    ) {
      const response = await fetch(
        `https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflow_id}/enable`,
        {
          method: "PUT",
          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 { success: true }
    }
    

    Submitted by hugo989 2 hours ago