Skip to content

Commit

Permalink
refactor(nodejs): upgrade to nodejs 20 (#4)
Browse files Browse the repository at this point in the history
- change runtime to nodejs 20 for action
- upgrade node packages
- move logic from index.ts to main.ts for tests
- update tests
- run formatter
- setup eslint
- update actions to use node.js 20

BREAKING CHANGE: Upgrades to Node.js 20.x
  • Loading branch information
ryanvade authored Nov 24, 2023
1 parent 5cd12cc commit eec04d3
Show file tree
Hide file tree
Showing 15 changed files with 34,433 additions and 12,487 deletions.
34 changes: 34 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = {
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"overrides": [
{
"env": {
"node": true
},
"files": [
".eslintrc.{js,cjs}"
],
"parserOptions": {
"sourceType": "script"
}
}
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
},
"ignorePatterns": ["dist/*.js"]
}
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [12.x]
node-version: [20.x]

steps:
- uses: actions/checkout@v2
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/
node_modules/
.idea/
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v12
v20
104 changes: 54 additions & 50 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,63 @@
import { getMinimumLength, getPullRequestDescription } from "../src/index";
import * as github from "@actions/github";
import { readFileSync } from "fs";
import { getDefaultSettings } from "http2";

describe("index", () => {
describe("getMinimumIndex", () => {
const name = "minLength";
it("returns the default length if one is not provided", () => {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = "";
const minLength = getMinimumLength();
expect(minLength).toBe(1);
});

it("returns the provided min length if it is valid", () => {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = "5";
const minLength = getMinimumLength();
expect(minLength).toBe(5);
});

it("throws an error if the provided min length is not a number", () => {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = "a";
expect(getMinimumLength).toThrowError("minLength must be a positive number");
});
import { getMinimumLength, getPullRequestDescription } from "../src/main";

describe("main", () => {
describe("getMinimumIndex", () => {
const name = "minLength";
it("returns the default length if one is not provided", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "";
const minLength = getMinimumLength();
expect(minLength).toBe(1);
});

it("returns the provided min length if it is valid", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "5";
const minLength = getMinimumLength();
expect(minLength).toBe(5);
});

it("throws an error if the provided min length is negative", () => {
process.env[`INPUT_${name.replace(/ /g, '_').toUpperCase()}`] = "-4";
expect(getMinimumLength).toThrowError("minLength must be a positive number");
});
it("throws an error if the provided min length is not a number", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "a";
expect(getMinimumLength).toThrow("minLength must be a positive number");
});

describe("getPullRequestDescription", () => {
it("gets a pull request event description", () => {
process.env["GITHUB_EVENT_PATH"] = __dirname + "/valid-context.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' })
);
const description = getPullRequestDescription();
expect(description).toEqual("Valid Body");
});
it("throws an error if the provided min length is negative", () => {
process.env[`INPUT_${name.replace(/ /g, "_").toUpperCase()}`] = "-4";
expect(getMinimumLength).toThrow("minLength must be a positive number");
});
});

describe("getPullRequestDescription", () => {
it("gets a pull request event description", () => {
process.env["GITHUB_EVENT_PATH"] = __dirname + "/valid-context.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: "utf8" }),
);
const description = getPullRequestDescription();
expect(description).toEqual("Valid Body");
});

it("throws an error if the event is not a pull_request type", () => {
process.env["GITHUB_EVENT_PATH"] = __dirname + "/wrong-event-type-context.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' })
);
expect(getPullRequestDescription).toThrowError("This action should only be run with Pull Request Events");
});
it("throws an error if the event is not a pull_request type", () => {
process.env["GITHUB_EVENT_PATH"] =
__dirname + "/wrong-event-type-context.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: "utf8" }),
);
expect(getPullRequestDescription).toThrow(
"This action should only be run with Pull Request Events",
);
});

it("throws an error if the description is not provided", () => {
process.env["GITHUB_EVENT_PATH"] = __dirname + "/event-without-a-body.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: 'utf8' })
);
expect(getPullRequestDescription).toThrowError("This action should only be run with Pull Request Events");
});
it("throws an error if the description is not provided", () => {
process.env["GITHUB_EVENT_PATH"] =
__dirname + "/event-without-a-body.json";
github.context.payload = JSON.parse(
readFileSync(process.env.GITHUB_EVENT_PATH, { encoding: "utf8" }),
);
expect(getPullRequestDescription).toThrow(
"This action should only be run with Pull Request Events",
);
});
})
});
});
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ name: Enforce Pull Request Description Length
description: Check that a PR description is filled out
inputs:
minLength:
description: 'Specific minimum length the descrption must be.'
description: 'Specific minimum length the description must be.'
required: false
runs:
using: node12
using: node20
main: dist/index.js
Loading

0 comments on commit eec04d3

Please sign in to comment.