Skip to content

Commit

Permalink
fix(everything): very proud to say this is 100% coverage according to…
Browse files Browse the repository at this point in the history
… default jest of all src code (including test)
  • Loading branch information
trilom committed Mar 19, 2020
1 parent 816868e commit dd31d02
Show file tree
Hide file tree
Showing 41 changed files with 1,493 additions and 668 deletions.
21 changes: 15 additions & 6 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
plugins:
- '@typescript-eslint'
- eslint-comments
- jest
- promise
- unicorn
extends:
- airbnb-typescript
- plugin:@typescript-eslint/recommended
- plugin:eslint-comments/recommended
- plugin:jest/recommended
- plugin:promise/recommended
- plugin:unicorn/recommended
- prettier
Expand All @@ -23,8 +21,10 @@ settings:
typescript: {}
rules:
unicorn/filename-case: off
react/static-property-placement: 0
no-prototype-builtins: 0
import/prefer-default-export: 0
'@typescript-eslint/no-explicit-any': 0
import/no-default-export: error
no-use-before-define:
- error
Expand All @@ -37,8 +37,6 @@ rules:
-
allowExpressions: true
allowTypedFunctionExpressions: true
'@typescript-eslint/no-explicit-any':
- off
'@typescript-eslint/no-use-before-define':
- error
-
Expand All @@ -57,5 +55,16 @@ parserOptions:
sourceType: module
env:
node: true
jest: true
browser: true
browser: true
overrides:
- files: ['src/tests/**/*']
plugins:
- jest
extends:
- plugin:jest/recommended
rules:
global-require: 0
'@typescript-eslint/no-var-requires': 0
no-console: 0
'@typescript-eslint/no-unused-vars': 0
'@typescript-eslint/no-throw-literal': 0
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# file-changes-action

[![codecov](https://codecov.io/gh/trilom/file-changes-action/branch/master/graph/badge.svg)](https://codecov.io/gh/trilom/file-changes-action)
![Integration Tests](https://github.com/trilom/file-changes-action/workflows/Integration%20Tests/badge.svg)

This action will take the information from the Push/Pull Request and output some variables and write files that will let you know what was changed, removed, or added.

## Inputs
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: "node",
testRunner: 'jest-circus/runner',
testMatch: ['**/*.test.ts'],
clearMocks: true,
collectCoverage: true,
collectCoverage: false,
coverageThreshold: {
global: {
branches: 50,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"main": "lib/main.js",
"scripts": {
"build": "yarn && tsc",
"build-package": "yarn build && ncc build",
"build-package": "yarn build --build tsconfig.build.json && ncc build",
"build-release": "yarn build-package --minify",
"test": "jest --passWithNoTests",
"test": "jest",
"test-coverage": "jest --coverage",
"format": "prettier --write **/*.ts",
"format-check": "prettier --check **/*.ts",
Expand Down Expand Up @@ -44,6 +44,7 @@
"@typescript-eslint/eslint-plugin": "^2.24.0",
"@typescript-eslint/parser": "^2.24.0",
"@zeit/ncc": "^0.20.5",
"codecov": "^3.6.5",
"eslint": "^6.8.0",
"eslint-config-airbnb-typescript": "^7.0.0",
"eslint-config-prettier": "^6.10.0",
Expand Down
15 changes: 9 additions & 6 deletions src/FilesHelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
setOutput as coreSetOutput,
debug as coreDebug
} from '@actions/core'
import { writeFileSync } from 'fs'
import { setOutput as coreSetOutput, debug as coreDebug } from '@actions/core'
import type {ChangedFiles} from 'typings/ChangedFiles'
import type { GitHubFile } from './typings/GitHubFile'
import type { GitHubFile } from 'typings/GitHubFile'
import {getErrorString} from './UtilsHelper'

/**
Expand All @@ -19,8 +22,8 @@ export function sortChangedFiles(files: GitHubFile[]): ChangedFiles {
})
return changedFiles
} catch (error) {
const eString = `There was an issue sorting files changed files.`
throw new Error(getErrorString(error.name || 'unknown', error.status, sortChangedFiles.name, eString, JSON.stringify(error)))
const eString = `There was an issue sorting files changed.`
throw new Error(getErrorString(error.name, error.status, sortChangedFiles.name, eString, JSON.stringify(error)))
}
}

Expand Down Expand Up @@ -78,7 +81,7 @@ export function writeFiles(format: string, key: string, files: string[]): void {
)
} catch (error) {
const eString = `There was an issue writing output files.`
throw new Error(getErrorString(error.name || 'unknown', error.status, writeFiles.name, eString, JSON.stringify(error)))
throw new Error(getErrorString(error.name, error.status, writeFiles.name, eString, JSON.stringify(error)))
}
}

Expand All @@ -96,6 +99,6 @@ export function writeOutput(format: string, key: string, files: string[]): void
coreSetOutput(fileName, formatChangedFiles(format, files))
} catch (error) {
const eString = `There was an issue setting action outputs.`
throw new Error(getErrorString(error.name || 'unknown', error.status, writeOutput.name, eString, JSON.stringify(error)))
throw new Error(getErrorString(error.name, error.status, writeOutput.name, eString, JSON.stringify(error)))
}
}
32 changes: 17 additions & 15 deletions src/GithubHelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {GitHub} from '@actions/github'
import { GitHub } from '@actions/github'
import type {GitHubFile} from 'typings/GitHubFile'
import type { Inferred } from "typings/Inferred"
import { getErrorString } from './UtilsHelper'
import type { Inferred } from "./typings/Inferred"
import type {GitHubFile} from './typings/GitHubFile'
/**
* @function initClient
* @throws {Error} not sure what might trigger this, but it will throw an error.
Expand All @@ -14,8 +14,7 @@ export function initClient(
try {
return new GitHub(token)
} catch (error) {
const eString = `There was an error creating github client. \
Please check your token.`
const eString = `There was an error creating github client. Please check your token.`
throw new Error(getErrorString(error.name, error.status, initClient.name, eString, error))
}
}
Expand Down Expand Up @@ -44,8 +43,7 @@ export async function getChangedPRFiles(
)
return files
} catch(error) {
const eString = `There was an error getting change files for
repo:${repo} owner:${owner} pr:${pullNumber}`
const eString = `There was an error getting change files for repo:${repo} owner:${owner} pr:${pullNumber}`
let ePayload: string
if (error.name === 'HttpError' && +error.status === 404) ePayload = getErrorString(error.name, error.status, getChangedPRFiles.name, eString, error)
else ePayload = getErrorString(`Unknown Error:${error.name || ''}`, error.status, getChangedPRFiles.name, eString, error.message)
Expand Down Expand Up @@ -79,8 +77,7 @@ export async function getChangedPushFiles(
)
return files
} catch (error) {
const eString = `There was an error getting change files for
repo:${repo} owner:${owner} base:${base} head:${head}`
const eString = `There was an error getting change files for repo:${repo} owner:${owner} base:${base} head:${head}`
let ePayload: string
if (error.name === 'HttpError' && +error.status === 404) ePayload = getErrorString(error.name, error.status, getChangedPushFiles.name, eString, error)
else ePayload = getErrorString(`Unknown Error:${error.name || ''}`, error.status, getChangedPushFiles.name, eString, error.message)
Expand All @@ -100,8 +97,9 @@ export async function getChangedFiles(
{ before, after, pr = NaN }: Inferred
): Promise<GitHubFile[]> {
try {
if (repoFull.split('/').length > 2)
throw new Error(getErrorString(`Bad-Repo`, 500, getChangedFiles.name, `Repo input of ${repoFull} has more than 2 length after splitting.`))
if (repoFull.split('/').length > 2) {
throw new Error(getErrorString(`Bad-Repo`, 500, 'self', `Repo input of ${repoFull} has more than 2 length after splitting.`))
}
const owner = repoFull.split('/')[0]
const repo = repoFull.split('/')[1]
let files:GitHubFile[] = []
Expand All @@ -114,9 +112,13 @@ export async function getChangedFiles(
const pError = JSON.parse(error.message)
if (pError.from.includes('getChanged'))
throw new Error(JSON.stringify({ ...pError, ...{ from: `${error.status}/${error.name}`} }, null, 2))
const eString = `There was an error getting change files outputs
pr: ${pr} before: ${before} after: ${after}`
const ePayload: string = getErrorString(`Unknown Error:${error.name || ''}`, error.status, getChangedFiles.name, eString, error.message)
const eString = `There was an error getting change files outputs pr: ${pr} before: ${before} after: ${after}`
const ePayload: string = getErrorString(
`Unknown Error:${error.name}`,
error.status,
getChangedFiles.name,
eString,
error.message)
throw new Error(ePayload)
}
}
}
9 changes: 5 additions & 4 deletions src/InputHelper.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
warning as coreWarning,
getInput as coreGetInput } from '@actions/core'
getInput as coreGetInput
} from '@actions/core'
import { context } from '@actions/github'
import type {Inferred} from 'typings/Inferred'
import type {Inputs} from 'typings/Inputs'
import { getErrorString } from './UtilsHelper'
import {Inputs} from './typings/Inputs'
import {Inferred} from './typings/Inferred'

/**
* @function getInputs
Expand All @@ -24,7 +25,7 @@ export function getInputs():Inputs {
prNumber: +coreGetInput('prNumber') || ((typeof (context.issue.number) === 'undefined') ? NaN : context.issue.number),
output: coreGetInput('output') || 'json',
fileOutput: coreGetInput('fileOutput') || 'json',
event: context.eventName || 'push'
event: context.eventName
} as Inputs
} catch (error) {
const eString = `Received an issue getting action inputs.`
Expand Down
30 changes: 27 additions & 3 deletions src/UtilsHelper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import {setFailed} from '@actions/core'
import * as t from '@actions/core'
import {ActionError} from './typings/ActionError'

import type {ActionError} from 'typings/ActionError'
/**
* @function getErrorString
* @param name name of error
* @param status status code of error
* @param from name of function that error is thrown from
* @param message error message
* @param error error object to stringify and attach
*/
export function getErrorString(
name: string,
status = 500,
Expand All @@ -21,4 +27,22 @@ export function getErrorString(
setFailed(`Error throwing error.\n ${JSON.stringify(error_.message)}`)
throw(new Error(JSON.stringify({name: '500/undefined', message: 'Error throwing error.'})))
}
}
/**
* @function errorMessage
* @param f name of function
* @param e error object
* @returns error message for function
*/
export function errorMessage (f:string, e:Error): string {
const error = JSON.stringify(e, null, 2)
let ret
if (f.includes('getInputs')) ret = `There was an getting action inputs.`
if (f.includes('inferInput')) ret = `There was an issue inferring inputs to the action.`
if (f.includes('initClient')) ret = `There was an issue initilizing the github client.`
if (f.includes('getChangedFiles')) ret = `There was an issue getting changed files from Github.`
if (f.includes('sortChangedFiles')) ret = `There was an issue sorting changed files from Github.`
if (f.includes('writeFiles')) ret = `There was an issue writing output files.`
if (f.includes('writeOutput')) ret = `There was an issue writing output variables.`
return `${ret}\nException: ${error}`
}
50 changes: 20 additions & 30 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
// External Dependencies
import {error as coreError, setFailed as coreSetFailed} from '@actions/core'
import {getInputs, inferInput} from './InputHelper'
import { writeOutput, writeFiles, sortChangedFiles} from './FilesHelper'
import { getChangedFiles, initClient} from './GithubHelper'

import {
setFailed as coreSetFailed
} from '@actions/core'
import {
getInputs,
inferInput
} from './InputHelper'
import {
writeOutput,
writeFiles,
sortChangedFiles
} from './FilesHelper'
import {
getChangedFiles,
initClient
} from './GithubHelper'
import {
errorMessage
} from './UtilsHelper'
// figure out if it is a PR or Push
export async function run(): Promise<void> {
try {
Expand All @@ -27,30 +40,7 @@ export async function run(): Promise<void> {
process.exit(0)
} catch (error) {
const pError = JSON.parse(error.message)
const prettyError = JSON.stringify(error.message, null, 2)
// catch error from getInputs
if (pError.from.includes(getInputs.name))
coreError(`There was an getting action inputs.\nException: ${prettyError}`)
// catch error from inferInput
if (pError.from.includes(inferInput.name))
coreError(`There was an issue inferring inputs to the action.\nException: ${prettyError}`)
// catch error from initClient
else if (pError.from.includes(initClient.name))
coreError(`There was an issue initilizing the github client.\nException: ${prettyError}`)
// catch error from getChangedFiles
else if (pError.from.includes(getChangedFiles.name))
coreError(`There was an issue getting changed files from Github.\nException: ${prettyError}`)
// catch error from sortChangedFiles
else if (pError.from.includes(sortChangedFiles.name))
coreError(`There was an issue sorting changed files from Github.\nException: ${prettyError}`)
// catch error from writeFiles
else if (pError.from.includes(writeFiles.name))
coreError(`There was an issue writing output files.\nException: ${prettyError}`)
// catch error from writeOutput
else if (pError.from.includes(writeOutput.name))
coreError(`There was an issue writing output variables.\nException: ${prettyError}`)
else coreError(JSON.stringify(pError))
coreSetFailed(error.message)
coreSetFailed(errorMessage(pError.from, pError))
}
}

Expand Down
Loading

0 comments on commit dd31d02

Please sign in to comment.