//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})`,
}))
}
/**
* Create Workflow Dispatch
* Trigger a GitHub Actions workflow that has a workflow_dispatch trigger, on a branch or tag. Inputs must match the workflow's declared inputs. OAuth connections need the workflow scope.
*/
export async function main(
auth: RT.Github,
owner: string,
repo: string,
workflow_id: DynSelect_workflow_id,
ref: string,
inputs: { [key: string]: any } | undefined
) {
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflow_id}/dispatches`,
{
method: "POST",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
body: JSON.stringify({ ref, inputs }),
}
)
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return { success: true }
}
Submitted by hugo989 2 hours ago