Skip to content

Instantly share code, notes, and snippets.

@zebapy
Last active February 6, 2024 13:35
Show Gist options
  • Select an option

  • Save zebapy/84464e8810ede9e0be515241dc9d9979 to your computer and use it in GitHub Desktop.

Select an option

Save zebapy/84464e8810ede9e0be515241dc9d9979 to your computer and use it in GitHub Desktop.
A raycast command for fetching the latest failed action step you made
#!/usr/bin/env node
// Required parameters:
// @raycast.schemaVersion 1
// @raycast.title Latest failed github action step link
// @raycast.mode silent
// Optional parameters:
// @raycast.icon 😿
// Documentation:
// @raycast.author Zeb Pykosz
// @raycast.authorURL https://github.com/zebapy
const { exec } = require("child_process");
const username = "ORG_USERNAME";
const repo = "REPO_NAME";
const accessToken = "SECRET"; // Replace with your own access token. Need repo permissions.
const targetActorLogin = "YOUR_LOGIN"; // Replace with the actor login of the user you want to find the failed step for
const runsEndpoint = `https://api.github.com/repos/${username}/${repo}/actions/runs`;
async function fetchGitHubApi(url) {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
if (!response.ok) {
throw new Error(`Failed to fetch data from ${url}`);
}
return response.json();
}
async function getLinkToFailedStep() {
try {
const data = await fetchGitHubApi(runsEndpoint);
const failedRun = data.workflow_runs.find(
(run) =>
run.conclusion === "failure" && run.actor.login === targetActorLogin
);
if (!failedRun) {
throw new Error(`No failed workflow runs found for ${targetActorLogin}.`);
}
const jobsEndpoint = `https://api.github.com/repos/${username}/${repo}/actions/runs/${failedRun.id}/jobs`;
const { jobs } = await fetchGitHubApi(jobsEndpoint);
const failedJob = jobs.find((job) => job.conclusion === "failure");
if (!failedJob) throw new Error(`No failed jobs`);
const failedStep = failedJob.steps.find(
(step) => step.conclusion === "failure"
);
if (!failedStep) throw new Error("No failed steps");
const link = `https://github.com/${username}/${repo}/actions/runs/${failedRun.id}/job/${failedJob.id}#step:${failedStep.number}:1`;
if (!link) throw new Error("No failed steps found within the job.");
exec(`open ${url}`);
} catch (e) {
console.error("Failed to get link to failed action step: ", e.message);
}
}
getLinkToFailedStep();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment