From 0741c6f30717c101db0f13dc4f0bbb6ef2004543 Mon Sep 17 00:00:00 2001 From: tpikachu <46133358+tpikachu@users.noreply.github.com> Date: Mon, 26 Feb 2024 12:29:13 -0600 Subject: [PATCH] use api --- .env | 2 +- package-lock.json | 136 ++++++++++++++++++++++------------ package.json | 4 +- src/index.js | 181 ++++++++++++---------------------------------- 4 files changed, 141 insertions(+), 182 deletions(-) diff --git a/.env b/.env index 2273905..a926442 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -GH_TOKEN=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 \ No newline at end of file +IS_DEV=true \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 4c0352d..77d4dc5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,9 +7,7 @@ "dependencies": { "@actions/core": "^1.10.1", "@actions/github": "^6.0.0", - "@octokit/rest": "^20.0.2", - "uuid": "^9.0.1", - "uzip": "^0.20201231.0" + "axios": "^1.6.7" }, "devDependencies": { "@vercel/ncc": "^0.38.1", @@ -130,17 +128,6 @@ "@octokit/core": ">=5" } }, - "node_modules/@octokit/plugin-request-log": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/@octokit/plugin-request-log/-/plugin-request-log-4.0.0.tgz", - "integrity": "sha512-2uJI1COtYCq8Z4yNSnM231TgH50bRkheQ9+aH8TnZanB6QilOnx8RMD2qsnamSOXtDj0ilxvevf5fGsBhBBzKA==", - "engines": { - "node": ">= 18" - }, - "peerDependencies": { - "@octokit/core": ">=5" - } - }, "node_modules/@octokit/plugin-rest-endpoint-methods": { "version": "10.2.0", "resolved": "https://registry.npmjs.org/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-10.2.0.tgz", @@ -182,20 +169,6 @@ "node": ">= 18" } }, - "node_modules/@octokit/rest": { - "version": "20.0.2", - "resolved": "https://registry.npmjs.org/@octokit/rest/-/rest-20.0.2.tgz", - "integrity": "sha512-Ux8NDgEraQ/DMAU1PlAohyfBBXDwhnX2j33Z1nJNziqAfHi70PuxkFYIcIt8aIAxtRE7KVuKp8lSR8pA0J5iOQ==", - "dependencies": { - "@octokit/core": "^5.0.0", - "@octokit/plugin-paginate-rest": "^9.0.0", - "@octokit/plugin-request-log": "^4.0.0", - "@octokit/plugin-rest-endpoint-methods": "^10.0.0" - }, - "engines": { - "node": ">= 18" - } - }, "node_modules/@octokit/types": { "version": "12.5.0", "resolved": "https://registry.npmjs.org/@octokit/types/-/types-12.5.0.tgz", @@ -213,11 +186,45 @@ "ncc": "dist/ncc/cli.js" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + }, + "node_modules/axios": { + "version": "1.6.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.7.tgz", + "integrity": "sha512-/hDJGff6/c7u0hDkvkGxR/oy6CbCs8ziCsC7SqmhjfozqiJGc8Z11wrv9z9lYfY4K8l+H9TpjcMDX0xOZmx+RA==", + "dependencies": { + "follow-redirects": "^1.15.4", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/before-after-hook": { "version": "2.2.3", "resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz", "integrity": "sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==" }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/deprecation": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/deprecation/-/deprecation-2.3.1.tgz", @@ -235,6 +242,57 @@ "url": "https://dotenvx.com" } }, + "node_modules/follow-redirects": { + "version": "1.15.5", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.5.tgz", + "integrity": "sha512-vSFWUON1B+yAw1VN4xMfxgn5fTUiaOzAJCKBwIIgT/+7CuGy9+r+5gITvP62j3RmaD5Ph65UaERdOSRGUzZtgw==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", + "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/once": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", @@ -243,6 +301,11 @@ "wrappy": "1" } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + }, "node_modules/tunnel": { "version": "0.0.6", "resolved": "https://registry.npmjs.org/tunnel/-/tunnel-0.0.6.tgz", @@ -267,23 +330,6 @@ "resolved": "https://registry.npmjs.org/universal-user-agent/-/universal-user-agent-6.0.1.tgz", "integrity": "sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==" }, - "node_modules/uuid": { - "version": "9.0.1", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", - "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", - "funding": [ - "https://github.com/sponsors/broofa", - "https://github.com/sponsors/ctavan" - ], - "bin": { - "uuid": "dist/bin/uuid" - } - }, - "node_modules/uzip": { - "version": "0.20201231.0", - "resolved": "https://registry.npmjs.org/uzip/-/uzip-0.20201231.0.tgz", - "integrity": "sha512-OZeJfZP+R0z9D6TmBgLq2LHzSSptGMGDGigGiEe0pr8UBe/7fdflgHlHBNDASTXB5jnFuxHpNaJywSg8YFeGng==" - }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/package.json b/package.json index 85067de..e8b1e97 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,7 @@ "dependencies": { "@actions/core": "^1.10.1", "@actions/github": "^6.0.0", - "@octokit/rest": "^20.0.2", - "uuid": "^9.0.1", - "uzip": "^0.20201231.0" + "axios": "^1.6.7" }, "devDependencies": { "@vercel/ncc": "^0.38.1", diff --git a/src/index.js b/src/index.js index 5d8b095..5ac8786 100644 --- a/src/index.js +++ b/src/index.js @@ -1,126 +1,65 @@ const core = require("@actions/core"); -const github = require("@actions/github"); const config = require("./config"); -import { v4 as uuidv4 } from "uuid"; -const UZip = require("uzip"); +const axios = require("axios"); +require("dotenv").config(); -function setOutput(summary, shareLink, conclusion) { +function setOutput(summary, shareLink) { core.setOutput("summary", summary); core.setOutput("share-link", shareLink); - core.setOutput("conclusion", conclusion); } const waitFor = (ms) => new Promise((r) => setTimeout(r, ms)); (async function () { - const owner = "replayableio"; - const repo = "testdriver"; - const branch = "michael/flag-based-action-or-bot"; - const dispatchWorkflow = "interpret-comment.yml"; - - const octokit = github.getOctokit(process.env.GH_TOKEN); - - const dispatchId = uuidv4(); - - await octokit.request( - "POST /repos/{owner}/{repo}/actions/workflows/{workflow_id}/dispatches", + const baseUrl = + (process.env.IS_DEV + ? "https://replayable-dev-michael.ngrok.io" + : "https://replayable-api-production.herokuapp.com") + "/api/v1"; + + const repo = process.env.IS_DEV + ? "replayableio/testdriver-action" + : config.githubContext.owner + "/" + config.githubContext.repo; + const branch = process.env.IS_DEV ? "main" : config.githubContext.branch; + const prompt = process.env.IS_DEV ? "open youtube" : config.input.prompt; + + const { + data: { workflowId }, + } = await axios.post( + `${baseUrl}/testdriver-dispatch`, { - owner, repo, - workflow_id: dispatchWorkflow, - ref: branch, - inputs: { - repo: config.githubContext.owner + "/" + config.githubContext.repo, - branch: config.githubContext.branch, - dispatchId, - comment: config.input.prompt, - isFromAction: "true", - }, + branch, + prompt, + }, + { + Accept: "application/json", + "Content-Type": "application/json", } ); - const findWorkFlow = async () => { - const twoMinutesAgo = new Date(); - twoMinutesAgo.setMinutes(twoMinutesAgo.getMinutes() - 2); - - 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 2 minutes since we poll every 1 minute - time: twoMinutesAgo.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; - } - } - }; - - const waitUntilWorkflowAvailable = async () => { - let workflowId; - while (!workflowId) { - await waitFor(1000 * 60); - workflowId = await findWorkFlow(); - } - - return workflowId; - }; - - const workflowId = await waitUntilWorkflowAvailable(); - - console.log("workflow found", workflowId); + console.log("workflow id:", workflowId); const checkStatus = async () => { - const workflow = await octokit.request( - "GET /repos/{owner}/{repo}/actions/runs/{run_id}", + const { + data: { status, conclusion }, + } = await axios.post( + `${baseUrl}/testdriver-status-check`, + { workflowId }, { - owner, - repo, - run_id: workflowId, + Accept: "application/json", + "Content-Type": "application/json", } ); - return { - status: workflow.data.status, - conclusion: workflow.data.conclusion, - }; + console.log("workflow status: ", status, conclusion); + + return { status, conclusion }; }; const waitUntilComplete = async () => { let { status, conclusion } = await checkStatus(); - while (status != "completed") { + + while (status !== "completed") { await waitFor(1000 * 60 * 1); const resp = await checkStatus(); status = resp.status; @@ -130,51 +69,27 @@ const waitFor = (ms) => new Promise((r) => setTimeout(r, ms)); return conclusion; }; - const conclusion = await waitUntilComplete(); + await waitUntilComplete(); - console.log("completed with ", conclusion); + console.log("workflow completed"); - // list workflow run artifacts - const artifacts = await octokit.request( - "GET /repos/{owner}/{repo}/actions/runs/{run_id}/artifacts", + const { + data: { shareLink, oiResult }, + } = await axios.post( + `${baseUrl}/testdriver-artifacts`, + { workflowId }, { - owner, - repo, - run_id: workflowId, + Accept: "application/json", + "Content-Type": "application/json", } ); - // find artifact id - let artifactId; - for (let i = 0; i < artifacts.data.total_count; i++) { - if (artifacts.data.artifacts[i].name === "share-link") { - artifactId = artifacts.data.artifacts[i].id; - } - } - - // download the artifact - let downloadedArtifact = await octokit.request( - "GET /repos/{owner}/{repo}/actions/artifacts/{artifact_id}/{archive_format}", - { - owner, - repo, - artifact_id: artifactId, - archive_format: "zip", - } - ); - - // get file data - let unzippedData = UZip.parse(downloadedArtifact.data); - const textDecoder = new TextDecoder(); - const shareLink = textDecoder.decode(unzippedData["shareLink.txt"]); - const oiResult = textDecoder.decode(unzippedData["oiResult.txt"]); - const isPassed = oiResult.includes("The test passed"); if (!isPassed) { core.setFailed(oiResult); } - console.log(shareLink, oiResult, conclusion); - setOutput(shareLink, oiResult, conclusion); + console.log("results: ", shareLink, oiResult); + setOutput(shareLink, oiResult); })();