Skip to content

Commit

Permalink
convert to polling
Browse files Browse the repository at this point in the history
  • Loading branch information
tpikachu committed Feb 22, 2024
1 parent 920fd81 commit cadac73
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 89 deletions.
103 changes: 61 additions & 42 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32388,9 +32388,10 @@ const config = __nccwpck_require__(4570);

const UZip = __nccwpck_require__(8222);

function setOutput(summary, shareLink) {
function setOutput(summary, shareLink, conclusion) {
core.setOutput("summary", summary);
core.setOutput("share-link", shareLink);
core.setOutput("conclusion", conclusion);
}

const waitFor = (ms) => new Promise((r) => setTimeout(r, ms));
Expand Down Expand Up @@ -32419,53 +32420,66 @@ const waitFor = (ms) => new Promise((r) => setTimeout(r, ms));
}
);

await waitFor(1000 * 60 * 1); // wait 5 minutes for workflow to complete
const findWorkFlow = async () => {
const oneMinuteAgo = new Date();
oneMinuteAgo.setMinutes(oneMinuteAgo.getMinutes() - 1);

const fiveMinsAgo = new Date();
fiveMinsAgo.setMinutes(fiveMinsAgo.getMinutes() - 1);

const workflowResponse = await octokit.request(
`GET /repos/{owner}/{repo}/actions/runs?created=>{time}&branch={branch}&event={trigger}`,
{
owner,
repo,
time: fiveMinsAgo.toISOString(),
branch,
trigger: "workflow_dispatch",
}
);
const runs = workflowResponse.data.workflow_runs || [];
const workflowResponse = await octokit.request(
`GET /repos/{owner}/{repo}/actions/runs?created=>{time}&branch={branch}&event={trigger}`,
{
owner,
repo,
// Can just check dispatches in the past 1 minutes since we poll every 1 minute
time: oneMinuteAgo.toISOString(),
branch,
trigger: "workflow_dispatch",
}
);
const runs = workflowResponse.data.workflow_runs || [];

let workflowId;
let workflowId;

for (let i in runs) {
const workflowRun = runs[i];
for (let i in runs) {
const workflowRun = runs[i];

// fetch jobs for the workflow run
const jobsUrl = new URL(workflowRun.jobs_url);
const response = await octokit.request(`GET ${jobsUrl.pathname}`);
const jobs = response.data.jobs || [];
// fetch jobs for the workflow run
const jobsUrl = new URL(workflowRun.jobs_url);
const response = await octokit.request(`GET ${jobsUrl.pathname}`);
const jobs = response.data.jobs || [];

// iterate over jobs within workflow run
for (let j in jobs) {
const job = jobs[j];
const steps = job.steps;
// iterate over jobs within workflow run
for (let j in jobs) {
const job = jobs[j];
const steps = job.steps;

if (steps.length > 1) {
// if after setup phase is a step that contains our
// dispatch id then capture workflow details and break
if (steps[1].name == dispatchId) {
workflowId = job.run_id;
break;
if (steps.length > 1) {
// if after setup phase is a step that contains our
// dispatch id then capture workflow details and break
if (steps[1].name == dispatchId) {
workflowId = job.run_id;
break;
}
}
}

// break iteration of workflow runs if workflow id found
if (workflowId) {
return workflowId;
}
}
};

// break iteration of workflow runs if workflow id found
if (workflowId) {
break;
const waitUntilWorkflowAvailable = async () => {
let workflowId = await findWorkFlow();
while (!workflowId) {
await waitFor(1000 * 60 * 1);
workflowId = await findWorkFlow();
}
}

return workflowId;
};

const workflowId = await waitUntilWorkflowAvailable();

const checkStatus = async () => {
const workflow = await octokit.request(
Expand All @@ -32477,18 +32491,23 @@ const waitFor = (ms) => new Promise((r) => setTimeout(r, ms));
}
);

return workflow.data.status;
return {
status: workflow.data.status,
conclusion: workflow.data.conclusion,
};
};

const waitUntilComplete = async () => {
let status = await checkStatus();
let { status, conclusion } = await checkStatus();
while (status !== "completed") {
await waitFor(1000 * 60 * 1);
status = await checkStatus();
}

return conclusion;
};

await waitUntilComplete();
const conclusion = await waitUntilComplete();

// list workflow run artifacts
const artifacts = await octokit.request(
Expand Down Expand Up @@ -32525,9 +32544,9 @@ const waitFor = (ms) => new Promise((r) => setTimeout(r, ms));
const shareLink = textDecoder.decode(unzippedData["shareLink.txt"]);
const oiResult = textDecoder.decode(unzippedData["oiResult.txt"]);

console.log(shareLink, oiResult);
console.log(shareLink, oiResult, conclusion);

setOutput(shareLink, oiResult);
setOutput(shareLink, oiResult, conclusion);
})();

})();
Expand Down
113 changes: 66 additions & 47 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ const config = require("./config");
import { v4 as uuidv4 } from "uuid";
const UZip = require("uzip");

function setOutput(summary, shareLink) {
function setOutput(summary, shareLink, conclusion) {
core.setOutput("summary", summary);
core.setOutput("share-link", shareLink);
core.setOutput("conclusion", conclusion);
}

const waitFor = (ms) => new Promise((r) => setTimeout(r, ms));
Expand Down Expand Up @@ -35,53 +36,66 @@ const waitFor = (ms) => new Promise((r) => setTimeout(r, ms));
}
);

await waitFor(1000 * 60 * 1); // wait 5 minutes for workflow to complete
const findWorkFlow = async () => {
const oneMinuteAgo = new Date();
oneMinuteAgo.setMinutes(oneMinuteAgo.getMinutes() - 1);

const fiveMinsAgo = new Date();
fiveMinsAgo.setMinutes(fiveMinsAgo.getMinutes() - 1);

const workflowResponse = await octokit.request(
`GET /repos/{owner}/{repo}/actions/runs?created=>{time}&branch={branch}&event={trigger}`,
{
owner,
repo,
time: fiveMinsAgo.toISOString(),
branch,
trigger: "workflow_dispatch",
}
);
const runs = workflowResponse.data.workflow_runs || [];

let workflowId;

for (let i in runs) {
const workflowRun = runs[i];

// fetch jobs for the workflow run
const jobsUrl = new URL(workflowRun.jobs_url);
const response = await octokit.request(`GET ${jobsUrl.pathname}`);
const jobs = response.data.jobs || [];

// iterate over jobs within workflow run
for (let j in jobs) {
const job = jobs[j];
const steps = job.steps;

if (steps.length > 1) {
// if after setup phase is a step that contains our
// dispatch id then capture workflow details and break
if (steps[1].name == dispatchId) {
workflowId = job.run_id;
break;
const workflowResponse = await octokit.request(
`GET /repos/{owner}/{repo}/actions/runs?created=>{time}&branch={branch}&event={trigger}`,
{
owner,
repo,
// Can just check dispatches in the past 1 minutes since we poll every 1 minute
time: oneMinuteAgo.toISOString(),
branch,
trigger: "workflow_dispatch",
}
);
const runs = workflowResponse.data.workflow_runs || [];

let workflowId;

for (let i in runs) {
const workflowRun = runs[i];

// fetch jobs for the workflow run
const jobsUrl = new URL(workflowRun.jobs_url);
const response = await octokit.request(`GET ${jobsUrl.pathname}`);
const jobs = response.data.jobs || [];

// iterate over jobs within workflow run
for (let j in jobs) {
const job = jobs[j];
const steps = job.steps;

if (steps.length > 1) {
// if after setup phase is a step that contains our
// dispatch id then capture workflow details and break
if (steps[1].name == dispatchId) {
workflowId = job.run_id;
break;
}
}
}

// break iteration of workflow runs if workflow id found
if (workflowId) {
return workflowId;
}
}
};

// break iteration of workflow runs if workflow id found
if (workflowId) {
break;
const waitUntilWorkflowAvailable = async () => {
let workflowId = await findWorkFlow();
while (!workflowId) {
await waitFor(1000 * 60 * 1);
workflowId = await findWorkFlow();
}
}

return workflowId;
};

const workflowId = await waitUntilWorkflowAvailable();

const checkStatus = async () => {
const workflow = await octokit.request(
Expand All @@ -93,18 +107,23 @@ const waitFor = (ms) => new Promise((r) => setTimeout(r, ms));
}
);

return workflow.data.status;
return {
status: workflow.data.status,
conclusion: workflow.data.conclusion,
};
};

const waitUntilComplete = async () => {
let status = await checkStatus();
let { status, conclusion } = await checkStatus();
while (status !== "completed") {
await waitFor(1000 * 60 * 1);
status = await checkStatus();
}

return conclusion;
};

await waitUntilComplete();
const conclusion = await waitUntilComplete();

// list workflow run artifacts
const artifacts = await octokit.request(
Expand Down Expand Up @@ -141,7 +160,7 @@ const waitFor = (ms) => new Promise((r) => setTimeout(r, ms));
const shareLink = textDecoder.decode(unzippedData["shareLink.txt"]);
const oiResult = textDecoder.decode(unzippedData["oiResult.txt"]);

console.log(shareLink, oiResult);
console.log(shareLink, oiResult, conclusion);

setOutput(shareLink, oiResult);
setOutput(shareLink, oiResult, conclusion);
})();

0 comments on commit cadac73

Please sign in to comment.