Skip to content

Commit

Permalink
feat(GH-40): align implementation with net vulnerable action (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavanlamb committed Jun 19, 2024
2 parents ff3484b + 1b329bc commit 363b577
Show file tree
Hide file tree
Showing 16 changed files with 153 additions and 97 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ steps:
- name: Restore
run: dotnet restore
- name: My action
uses: gavanlamb/.net-vulnerable@latest
with:
add-pr-comment: true
add-check-run: true
Expand All @@ -65,6 +66,7 @@ steps:
- name: Restore
run: dotnet restore
- name: My action
uses: gavanlamb/.net-vulnerable@latest
with:
add-pr-comment: true
add-check-run: true
Expand All @@ -74,6 +76,7 @@ steps:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: My action
uses: gavanlamb/.net-vulnerable@latest
with:
add-pr-comment: true
add-check-run: true
Expand Down
29 changes: 29 additions & 0 deletions __tests__/errors/dotnetCommandProblemError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe('DotnetOutdatedCommandProblemError', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.resetModules();
});

it('should create an instance of DotnetCommandProblemError with the correct properties', async () => {
const projectName = 'testInput.csproj';
const message = 'Project not restored';

const { DotnetCommandProblemError } = await import("../../src/errors/dotnetCommandProblemError");
const error = new DotnetCommandProblemError(projectName, message);

expect(error).toBeInstanceOf(DotnetCommandProblemError);
expect(error.name).toBe('DotnetCommandProblemError');
expect(error.projectName).toBe(projectName);
expect(error.message).toBe(message);
});

it('should have a stack trace', async () => {
const projectName = 'testInput.csproj';
const message = 'Project not restored';

const { DotnetCommandProblemError } = await import("../../src/errors/dotnetCommandProblemError");
const error = new DotnetCommandProblemError(projectName, message);

expect(error.stack).toBeDefined();
});
});
29 changes: 0 additions & 29 deletions __tests__/errors/dotnetOutdatedCommandProblemError.test.ts

This file was deleted.

29 changes: 29 additions & 0 deletions __tests__/errors/invalidGitHubActionInputError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe('InvalidGitHubActionInputError', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.resetModules();
});

it('should create an instance of InvalidGitHubActionInputError with the correct properties', async () => {
const inputName = 'testInput';
const message = 'Invalid input provided';

const { InvalidGitHubActionInputError } = await import("../../src/errors/invalidGitHubActionInputError");
const error = new InvalidGitHubActionInputError(inputName, message);

expect(error).toBeInstanceOf(InvalidGitHubActionInputError);
expect(error.name).toBe('InvalidGitHubActionInputError');
expect(error.inputName).toBe(inputName);
expect(error.message).toBe(message);
});

it('should have a stack trace', async () => {
const inputName = 'testInput';
const message = 'Invalid input provided';

const { InvalidGitHubActionInputError } = await import("../../src/errors/invalidGitHubActionInputError");
const error = new InvalidGitHubActionInputError(inputName, message);

expect(error.stack).toBeDefined();
});
});
29 changes: 0 additions & 29 deletions __tests__/errors/invalidGitHubActionsInputError.test.ts

This file was deleted.

2 changes: 1 addition & 1 deletion __tests__/helpers/inputHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('getBooleanInput', () => {
['invalid'],
['F A L S E'],
['T R U E']
])('should throw an InvalidGitHubActionsInputError when the input is "%s"', async (value: string) => {
])('should throw an InvalidGitHubActionInputError when the input is "%s"', async (value: string) => {
const name = "inputName";

const debugMock = jest.fn();
Expand Down
20 changes: 20 additions & 0 deletions __tests__/helpers/jsonHelper.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
isValidJSON
} from '../../src/helpers/jsonHelper';

describe('getStringInput', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.resetModules();
});

test.each([
['', false],
['{}', true],
['Error encountered', false],
])('should return null when the input is "%s"', async (input: string, expected: boolean) => {
const result = isValidJSON(input);

expect(result).toBe(expected);
});
});
2 changes: 1 addition & 1 deletion __tests__/services/dotnetService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ describe("listOutdatedPackages", () => {
expect(debugMock).toHaveBeenCalledWith(`Executed "dotnet ${expectedArgs.join(" ")}" and the output is ${stderr}`);
});

it("should throw an DotnetOutdatedCommandProblemError when there are problems", async () => {
it("should throw an DotnetCommandProblemError when there are problems", async () => {
const configuration: Configuration = {
parameters: "-outdated",
version: 1,
Expand Down
45 changes: 30 additions & 15 deletions dist/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/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dotnet-outdated",
"description": "Dotnet outdated GitHub Actions",
"description": "Dotnet outdated GitHub Action",
"version": "0.0.0",
"author": "Gavan Lamb",
"homepage": "https://github.com/marketplace/actions/dotnet-outdated",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* Error indicating a dotnet outdated command problem
*/
class DotnetOutdatedCommandProblemError extends Error {
class DotnetCommandProblemError extends Error {
projectName: string;

constructor(projectName: string, message: string) {
super(message);
this.name = 'DotnetOutdatedCommandProblemError';
this.name = 'DotnetCommandProblemError';
this.projectName = projectName;
}
}

export {
DotnetOutdatedCommandProblemError
DotnetCommandProblemError
};
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* Error indicating a GitHub Actions Input error
*/
class InvalidGitHubActionsInputError extends Error {
class InvalidGitHubActionInputError extends Error {
inputName: string;

constructor(inputName: string, message: string) {
super(message);
this.name = 'InvalidGitHubActionsInputError';
this.name = 'InvalidGitHubActionInputError';
this.inputName = inputName;
}
}

export {
InvalidGitHubActionsInputError
InvalidGitHubActionInputError
};
10 changes: 5 additions & 5 deletions src/helpers/inputHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
getInput
} from '@actions/core';
import {
InvalidGitHubActionsInputError
} from "../errors/invalidGitHubActionsInputError";
InvalidGitHubActionInputError
} from "../errors/invalidGitHubActionInputError";

/**
* Gets a boolean input from the GitHub Actions workflow.
Expand All @@ -26,7 +26,7 @@ function getBooleanInput(

const lowerValueStr = valueStr.toLowerCase();
if (lowerValueStr !== 'true' && lowerValueStr !== 'false') {
throw new InvalidGitHubActionsInputError(
throw new InvalidGitHubActionInputError(
inputName,
`The retrieved boolean value for: ${inputName} is ${valueStr}, expected values are 'true' or 'false'`);
}
Expand Down Expand Up @@ -55,7 +55,7 @@ function getIntegerInput(

const value = parseInt(valueStr, 10);
if (isNaN(value))
throw new InvalidGitHubActionsInputError(
throw new InvalidGitHubActionInputError(
inputName,
`The retrieved integer value for: ${inputName} is ${valueStr}, which is deemed to be a NaN`);
debug(`The retrieved integer input value for: ${inputName} parsed to: '${value}'`);
Expand Down Expand Up @@ -118,7 +118,7 @@ function getStringInputAndValidateAgainstAllowedValues(
}
}

throw new InvalidGitHubActionsInputError(
throw new InvalidGitHubActionInputError(
inputName,
`The retrieved string value for: ${inputName} is ${value}, which is not one of the expected values ${allowedValues.join(', ')}.`);
}
Expand Down
13 changes: 13 additions & 0 deletions src/helpers/jsonHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function isValidJSON(
input: string): boolean {
try {
JSON.parse(input);
return true;
} catch (e) {
return false;
}
}

export {
isValidJSON
};
Loading

0 comments on commit 363b577

Please sign in to comment.