Skip to content

Commit

Permalink
chore(deps): bump dependencies and Node version
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesIves committed Nov 9, 2024
1 parent 7f4a29e commit e2ce353
Show file tree
Hide file tree
Showing 15 changed files with 84 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
node-version-file: '.node-version'
registry-url: 'https://registry.npmjs.org'

- name: Install Yarn
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ on:
workflow_dispatch:
schedule:
- cron: 30 15 * * 0-6

push:
tags-ignore:
- '*.*'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
node-version-file: '.node-version'
registry-url: 'https://registry.npmjs.org'

- name: Install Yarn
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
node-version-file: '.node-version'
registry-url: 'https://registry.npmjs.org'
scope: '@jamesives'

Expand Down Expand Up @@ -60,7 +60,7 @@ jobs:
# Setup .npmrc file to publish to GitHub Packages
- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
node-version-file: '.node-version'
registry-url: 'https://npm.pkg.github.com'
scope: '@jamesives'

Expand Down
1 change: 1 addition & 0 deletions .node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v22.11.0
1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

51 changes: 26 additions & 25 deletions __tests__/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import {retrieveData, generateExport} from '../src/fetch'
import nock from 'nock'

jest.setTimeout(1000000)
nock.enableNetConnect()

describe('fetch', () => {
describe('retrieveData', () => {
afterEach(nock.cleanAll)
afterAll(nock.restore)
afterEach(() => {
jest.clearAllMocks()
})

it('should return some data', async () => {
nock('https://jamesiv.es').get('/').reply(200, {
data: '12345'
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue({data: '12345'}),
text: jest.fn().mockResolvedValue('{"data":"12345"}')
})

const data = await retrieveData({
Expand All @@ -22,11 +23,11 @@ describe('fetch', () => {
})

it('should handle the triple bracket replacements', async () => {
nock('https://jives.dev/')
.post('/', '{"bestCat":"montezuma"}')
.reply(200, {
data: '12345'
})
global.fetch = jest.fn().mockResolvedValue({
ok: true,
json: jest.fn().mockResolvedValue({data: '12345'}),
text: jest.fn().mockResolvedValue('{"data":"12345"}')
})

const data = await retrieveData({
debug: true,
Expand All @@ -45,8 +46,6 @@ describe('fetch', () => {

it('should error if improperly formatted json is passed in', async () => {
try {
nock('https://jamesiv.es').get('/').reply(200)

await retrieveData({
debug: true,
endpoint: 'https://example.com',
Expand All @@ -61,8 +60,10 @@ describe('fetch', () => {
})

it('should error if the response is not ok', async () => {
nock('https://jamesiv.es').post('/').reply(404, {
a: 1
global.fetch = jest.fn().mockResolvedValue({
ok: false,
json: jest.fn().mockResolvedValue({a: 1}),
text: jest.fn().mockResolvedValue('{"a":1}')
})

try {
Expand All @@ -83,18 +84,18 @@ describe('fetch', () => {
}
})

it('should error if the response is not ok after several retrys', async () => {
it('should error if the response is not ok after several retries', async () => {
jest.setTimeout(1000000)

try {
nock('https://jives.dev').get('/').once().replyWithError({
message: 'This is catastrophic'
})

nock('https://jives.dev').get('/').reply(200, {
data: '12345'
global.fetch = jest
.fn()
.mockRejectedValueOnce(new Error('This is catastrophic'))
.mockResolvedValueOnce({
ok: true,
json: jest.fn().mockResolvedValue({data: '12345'}),
text: jest.fn().mockResolvedValue('{"data":"12345"}')
})

try {
await retrieveData({
debug: true,
endpoint: 'https://jives.dev',
Expand All @@ -117,7 +118,7 @@ describe('fetch', () => {
expect(process.env['fetchApiData']).toBe('{"bestCat":"montezuma"}')
})

it('should save non standard file types', async () => {
it('should save non-standard file types', async () => {
await generateExport({
data: 'hello',
format: 'txt',
Expand Down
30 changes: 21 additions & 9 deletions __tests__/lib.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {exportVariable, setFailed} from '@actions/core'
import nock from 'nock'
import {action} from '../src/constants'
import run from '../src/lib'
import '../src/main'
Expand All @@ -15,38 +14,49 @@ jest.mock('@actions/core', () => ({

describe('lib', () => {
beforeEach(() => {
nock('https://jamesiv.es').get('/').reply(200, {
data: '12345'
jest.clearAllMocks()

global.fetch = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue({data: '12345'}),
text: jest.fn().mockResolvedValue('{"data":"12345"}'),
ok: true
})
})

afterEach(() => {
nock.restore()
Object.assign(action, JSON.parse(originalAction))
})

afterEach(nock.cleanAll)

it('should run through the commands', async () => {
Object.assign(action, {
debug: true,
endpoint: 'https://jamesiv.es',
endpoint: 'https://jives.dev',
setOutput: true
})

await run(action)

expect(exportVariable).toHaveBeenCalled()
expect(exportVariable).toHaveBeenCalledTimes(1)
expect(global.fetch).toHaveBeenCalledWith(
'https://jives.dev',
expect.any(Object)
)
})

it('should run through the commands but not save output', async () => {
Object.assign(action, {
debug: true,
endpoint: 'https://jamesiv.es',
endpoint: 'https://jives.dev',
setOutput: false
})

await run(action)

expect(exportVariable).toHaveBeenCalledTimes(0)
expect(global.fetch).toHaveBeenCalledWith(
'https://jives.dev',
expect.any(Object)
)
})

it('should throw an error if no endpoint is provided', async () => {
Expand All @@ -58,6 +68,7 @@ describe('lib', () => {
try {
await run(action)
} catch (error) {
console.error(error)
expect(setFailed).toHaveBeenCalled()
}
})
Expand All @@ -73,6 +84,7 @@ describe('lib', () => {
try {
await run(action)
} catch (error) {
console.error(error)
expect(setFailed).toHaveBeenCalled()
}
})
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default tseslint.config(
},
rules: {
'jest/no-conditional-expect': 'off',
'@typescript-eslint/ban-types': [
'@typescript-eslint/no-restricted-types': [
'error',
{
types: {
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@actions/core": "1.11.1",
"@actions/io": "1.1.3",
"async-retry": "1.3.3",
"cross-fetch": "4.0.0",
"mustache": "4.2.0"
},
"devDependencies": {
Expand All @@ -55,7 +54,6 @@
"eslint-plugin-prettier": "5.2.1",
"jest": "29.7.0",
"jest-circus": "29.7.0",
"nock": "13.5.5",
"prettier": "3.3.3",
"ts-jest": "29.2.5",
"typescript": "5.6.3",
Expand Down
13 changes: 12 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {getInput} from '@actions/core'
import {isNullOrUndefined} from './util'

/**
* Required action data that gets initialized when running within the GitHub Actions environment.
*/
export interface ActionInterface {
/** Allows you to log the retrieved data to the terminal. */
debug?: boolean
Expand Down Expand Up @@ -28,6 +31,9 @@ export interface ActionInterface {
variableName?: string
}

/**
* Required data to fetch the data.
*/
export interface DataInterface {
/** Allows you to log the retrieved data to the terminal. */
debug?: boolean
Expand All @@ -43,6 +49,9 @@ export interface DataInterface {
retry?: boolean | null
}

/**
* Required data to export the data.
*/
export interface ExportInterface {
/** The data to save. */
data: string
Expand All @@ -60,7 +69,9 @@ export interface ExportInterface {
variableName?: string
}

// Required action data that gets initialized when running within the GitHub Actions environment.
/**
* Required action data that gets initialized when running within the GitHub Actions environment.
*/
export const action = {
debug: !isNullOrUndefined(getInput('debug'))
? getInput('debug').toLowerCase() === 'true'
Expand Down
9 changes: 6 additions & 3 deletions src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import {
debug
} from '@actions/core'
import {mkdirP} from '@actions/io'
import 'cross-fetch/polyfill'
import {promises as fs} from 'fs'
import {render} from 'mustache'
import retryRequest from 'async-retry'
import {DataInterface, ExportInterface, Status} from './constants'
import {parseData} from './util'

/* Fetches or Posts data to an API. If auth is provided it will replace the mustache variables with the data from it. */
/**
* Retrieves data from an API endpoint.
*/
export async function retrieveData({
debug: requestDebug,
endpoint,
Expand Down Expand Up @@ -67,7 +68,9 @@ export async function retrieveData({
}
}

/* Saves the data to the local file system and exports an environment variable containing the retrieved data. */
/**
* Generates an export file from the data provided.
*/
export async function generateExport({
data,
encoding,
Expand Down
1 change: 1 addition & 0 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default async function run(
hasRequiredParameters(settings)

let auth = ''

if (settings.tokenEndpoint) {
auth = await retrieveData({
debug: settings.debug,
Expand Down
15 changes: 12 additions & 3 deletions src/util.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import {ActionInterface} from './constants'

/* Utility function that checks to see if a value is undefined or not. */
/**
* Checks to see if a value is null or undefined.
*/
export const isNullOrUndefined = (value: string | undefined | null): boolean =>
typeof value === 'undefined' || value === null || value === ''

/* Checks for the required inputs. Throws an error if any case is matched. */
/**
* Checks to see if the action has the required parameters to run.
*/
export const hasRequiredParameters = (action: ActionInterface): void => {
if (isNullOrUndefined(action.endpoint)) {
throw new Error(
Expand All @@ -13,14 +17,19 @@ export const hasRequiredParameters = (action: ActionInterface): void => {
}
}

/**
* Extracts the error message from an error object or string.
*/
export const extractErrorMessage = (error: unknown): string =>
error instanceof Error
? error.message
: typeof error == 'string'
? error
: JSON.stringify(error)

/* Attempt to parse data as JSON and catch any errors. */
/**
* Parses a string into a JSON object.
*/
export const parseData = (data: string): Record<string, unknown> | null => {
try {
return JSON.parse(data)
Expand Down
Loading

0 comments on commit e2ce353

Please sign in to comment.