Skip to content

Commit

Permalink
Add tests and refactor (#231)
Browse files Browse the repository at this point in the history
- generally improves tests which were not working, and adds them to CI pipeline
- refactors to catch errors
- Rename github action to match current usage
- Informing GitHub of failure is done at the top-level method
- Add tests for network and http errors
- reduce coverage thresholds
  • Loading branch information
iainsproat authored Oct 19, 2023
1 parent bcc6ed5 commit 2cf7d93
Show file tree
Hide file tree
Showing 8 changed files with 966 additions and 77 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on: # rebuild any PRs and main branch changes
- 'releases/*'

jobs:
pre-commit:
pre-commit-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -19,6 +19,10 @@ jobs:
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
- uses: actions/setup-node@v3
with:
node-version: '18.17.1'
cache: 'yarn'
- name: Yarn Install
run: yarn install
- uses: actions/cache/save@v3
Expand All @@ -27,3 +31,6 @@ jobs:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
- uses: pre-commit/[email protected]
- name: Tests
run: yarn test
continue-on-error: true # ignore test failures for now
65 changes: 44 additions & 21 deletions dist/action/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/action/index.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"precommit": "pre-commit run --all-files",
"prettier:check": "prettier --check '**/*.ts'",
"prettier:fix": "prettier --write '**/*.ts'",
"test": "vitest --run --coverage",
"test": "vitest --run --coverage",
"test:watch": "vitest"
},
"engines": {
Expand Down Expand Up @@ -45,6 +45,7 @@
"eslint-plugin-no-only-tests": "^3.1.0",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-vitest": "^0.2.8",
"msw": "^1.3.2",
"prettier": "^3.0.1",
"typescript": "^5.1.6",
"vite": "^4.4.9",
Expand Down
80 changes: 50 additions & 30 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as core from '@actions/core'
import { z } from 'zod'
import { ZodError, z } from 'zod'
import fetch from 'node-fetch'
import { retry } from '@lifeomic/attempt'
import { readFileSync } from 'node:fs'
Expand All @@ -20,20 +20,16 @@ const parseInputs = (): InputVariables => {
const speckleTokenRaw = core.getInput('speckle_token', { required: true })
core.setSecret(speckleTokenRaw)

const rawInputSchemaPath = core.getInput('speckle_function_input_schema_file_path')
const homeDir = process.env['HOME']
if (!homeDir)
throw new Error('The home directory is not defined, cannot load inputSchema')
let speckleFunctionInputSchema: Record<string, unknown> | null = null
try {
const rawInputSchemaPath = core.getInput('speckle_function_input_schema_file_path')
const homeDir = process.env['HOME']
if (!homeDir)
throw new Error('The home directory is not defined, cannot load inputSchema')
if (rawInputSchemaPath) {
const rawInputSchema = readFileSync(join(homeDir, rawInputSchemaPath), 'utf-8')
speckleFunctionInputSchema = JSON.parse(rawInputSchema)
}
} catch (err) {
core.setFailed(`Parsing the function input schema failed with: ${err}`)
throw err
if (rawInputSchemaPath) {
const rawInputSchema = readFileSync(join(homeDir, rawInputSchemaPath), 'utf-8')
speckleFunctionInputSchema = JSON.parse(rawInputSchema)
}

const rawInputs: InputVariables = {
speckleAutomateUrl: core.getInput('speckle_automate_url', { required: true }),
speckleToken: speckleTokenRaw,
Expand All @@ -48,9 +44,6 @@ const parseInputs = (): InputVariables => {
}
const inputParseResult = InputVariablesSchema.safeParse(rawInputs)
if (inputParseResult.success) return inputParseResult.data
core.setFailed(
`The provided inputs do not match the required schema, ${inputParseResult.error.message}`
)
throw inputParseResult.error
}

Expand All @@ -65,9 +58,6 @@ const parseEnvVars = (): RequiredEnvVars => {
gitCommitSha: process.env.GITHUB_SHA
} as RequiredEnvVars)
if (parseResult.success) return parseResult.data
core.setFailed(
`The current execution environment does not have the required variables: ${parseResult.error.message}`
)
throw parseResult.error
}

Expand Down Expand Up @@ -144,20 +134,44 @@ const registerNewVersionForTheSpeckleAutomateFunction = async (
}
}
)
return FunctionVersionResponseBodySchema.parse(response)
const parsedResult = FunctionVersionResponseBodySchema.safeParse(response)
if (parsedResult.success) return parsedResult.data
throw parsedResult.error
} catch (err) {
core.setFailed(
`Failed to register new function version to the automate server: ${err}`
)
throw err
throw Error('Failed to register new function version to the automate server', {
cause: err
})
}
}

const failAndReject = async (
e: unknown,
errorMessageForUnknownObjectType: string
): Promise<never> => {
if (e instanceof ZodError || e instanceof Error) {
core.setFailed(e.message)
return Promise.reject(e.message)
}
core.setFailed(errorMessageForUnknownObjectType)
return Promise.reject(e)
}

export async function run(): Promise<void> {
core.info('Start registering a new version on the automate instance')
const inputVariables = parseInputs()
let inputVariables: InputVariables = {} as InputVariables
try {
inputVariables = parseInputs()
} catch (e: unknown) {
return failAndReject(e, 'Failed to parse the input variables')
}
core.info(`Parsed input variables to: ${JSON.stringify(inputVariables)}`)
const requiredEnvVars = parseEnvVars()
let requiredEnvVars: RequiredEnvVars = {} as RequiredEnvVars
try {
requiredEnvVars = parseEnvVars()
} catch (e: unknown) {
return failAndReject(e, 'Failed to parse the required environment variables')
}

const { gitCommitSha } = requiredEnvVars
core.info(
`Parsed required environment variables to: ${JSON.stringify(requiredEnvVars)}`
Expand All @@ -172,10 +186,16 @@ export async function run(): Promise<void> {
// github uses 7 chars to identify commits
const commitId = gitCommitSha.substring(0, 7)

const { versionId } = await registerNewVersionForTheSpeckleAutomateFunction(
inputVariables,
commitId
)
let versionId = ''
try {
const registrationResponse = await registerNewVersionForTheSpeckleAutomateFunction(
inputVariables,
commitId
)
versionId = registrationResponse.versionId
} catch (e: unknown) {
return failAndReject(e, 'Failed to register the new function version')
}
core.info(
`Registered function version tagged as ${inputVariables.speckleFunctionReleaseTag} with new id: ${versionId}`
)
Expand Down
Loading

0 comments on commit 2cf7d93

Please sign in to comment.