-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from OpenZeppelin/plat-1927-platform-sdk-acti…
…ons-missing-function-typo Added missing actions utilities
- Loading branch information
Showing
14 changed files
with
202 additions
and
182 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { existsSync } from 'fs'; | ||
import { ActionClient } from './api'; | ||
import { | ||
ActionRunErrorData, | ||
ActionRunListItemResponse, | ||
ActionRunStatus, | ||
ActionRunSuccessData, | ||
} from './models/action-run.res'; | ||
|
||
/** | ||
* Regex Validator for Action and Action run IDs. | ||
*/ | ||
export function validateId(id: string): void { | ||
const regex = /^[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{12}$/i; | ||
if (regex.test(id)) { | ||
return; | ||
} else { | ||
throw new Error(`invalid id '${id}'`); | ||
} | ||
} | ||
|
||
/** | ||
* Checks if path exists, otherwise throws an error. | ||
*/ | ||
export function validatePath(path: string): void { | ||
if (existsSync(path)) { | ||
return; | ||
} else { | ||
throw new Error(`path ${path} does not exist`); | ||
} | ||
} | ||
|
||
export async function tailLogsFor(client: ActionClient, actionId: string) { | ||
try { | ||
validateId(actionId); | ||
console.warn(`\nPolling latest runs of action '${actionId}'...\n`); | ||
// Poll action runs every 2 seconds and if there are new runs, get run details and print them out. | ||
let lastRun: ActionRunListItemResponse | undefined; | ||
while (true) { | ||
const newRuns = await client.listActionRuns(actionId, {}); | ||
// If cached last run id has changed | ||
if (newRuns.items[0]?.actionRunId !== lastRun?.actionRunId) { | ||
lastRun = newRuns.items[0]; // cache new last run to avoid duplicates. | ||
if (!lastRun) throw new Error('last run not found'); | ||
|
||
const status = lastRun.status as ActionRunStatus; | ||
if (status === 'pending') { | ||
lastRun = undefined; // clean up so we can check it again on the next poll. | ||
} else if (status === 'error') { | ||
const runDetails = (await client.getActionRun(lastRun.actionRunId)) as ActionRunErrorData; | ||
console.log(`\nError: ${runDetails.message}`); | ||
runDetails.decodedLogs ? console.log(`\n${runDetails.decodedLogs}`) : console.log(`No logs available.`); | ||
} else if (status === 'success') { | ||
const runDetails = (await client.getActionRun(lastRun.actionRunId)) as ActionRunSuccessData; | ||
console.log(`\n${runDetails.decodedLogs}`); | ||
} else if (status === 'throttled') { | ||
console.warn( | ||
`\nThis autotask run was canceled since the hourly run capacity for your account has been exceeded. Contact us at defender-support@openzeppelin.com for additional capacity.`, | ||
); | ||
} | ||
} | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 2000)); | ||
} | ||
} catch (e) { | ||
throw e; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.