Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,18 @@
- run: npm ci
- run: npm run build

test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
cache: npm
- run: npm ci
- run: npm run test

lint:

Check warning

Code scanning / CodeQL

Workflow does not contain permissions Medium test

Actions job or workflow does not limit the permissions of the GITHUB_TOKEN. Consider setting an explicit permissions block, using the following as a minimal starting point: {contents: read}
name: Lint
runs-on: ubuntu-latest
steps:
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
npm run test
npm run build
git add lib
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
6 changes: 4 additions & 2 deletions lib/index.js

Large diffs are not rendered by default.

10,435 changes: 5,521 additions & 4,914 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
"build": "ncc build src/main.ts --minify --out lib",
"lint": "gts lint",
"prepare": "husky",
"test": "echo \"Error: no test specified\" && exit 1",
"test": "jest && npm run lint",
"clean": "gts clean",
"fix": "gts fix",
"pretest": "npm run compile",
"posttest": "npm run lint"
"fix": "gts fix"
},
"dependencies": {
"@actions/core": "^1.11.1",
Expand All @@ -23,13 +21,17 @@
"string-argv": "^0.3.2"
},
"devDependencies": {
"@types/jest": "^29.5.12",
"@types/js-yaml": "^4.0.9",
"@types/node": "20.8.2",
"@vercel/ncc": "^0.38.4",
"all-contributors-cli": "^6.26.1",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-prettier": "^5.5.4",
"gts": "^6.0.2",
"husky": "^9.1.7",
"jest": "^29.7.0",
"ts-jest": "^29.1.5",
"typescript": "~5.9.3"
},
"engines": {
Expand Down
37 changes: 18 additions & 19 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,26 @@ import * as fs from 'fs';
import {input, output} from './io';

type RecordOf<T extends string> = Record<T, string | undefined>;
export const tools = new Toolkit<RecordOf<input>, RecordOf<output>>({
secrets: [
'GITHUB_EVENT_PATH',
'GITHUB_EVENT_NAME',
'GITHUB_REF',
'GITHUB_ACTOR',
],
});
let tools: Toolkit<RecordOf<input>, RecordOf<output>> | undefined;
function getToolkit() {
if (!tools) {
tools = new Toolkit<RecordOf<input>, RecordOf<output>>({
secrets: [
'GITHUB_EVENT_PATH',
'GITHUB_EVENT_NAME',
'GITHUB_REF',
'GITHUB_ACTOR',
],
});
}

return tools;
}

export async function getUserInfo(username?: string) {
if (!username) return undefined;

const res = await tools.github.users.getByUsername({username});
const res = await getToolkit().github.users.getByUsername({username});

core.debug(
`Fetched github actor from the API: ${JSON.stringify(res?.data, null, 2)}`,
Expand Down Expand Up @@ -70,19 +77,11 @@ export function matchGitArgs(string: string) {
}

/**
* Tries to parse a JSON array, then a YAML array.
* If both fail, it returns an array containing the input value as its only element
* Tries to parse a YAML sequence (which can be a JSON array).
* If it fails, it returns an array containing the input value as its only element.
*/
export function parseInputArray(input: string): string[] {
core.debug(`Parsing input array: ${input}`);
try {
const json = JSON.parse(input);
if (json && Array.isArray(json) && json.every(e => typeof e === 'string')) {
core.debug(`Input parsed as JSON array of length ${json.length}`);
return json;
}
} catch {} // eslint-disable-line no-empty

try {
const yaml = YAML.load(input);
if (yaml && Array.isArray(yaml) && yaml.every(e => typeof e === 'string')) {
Expand Down
33 changes: 33 additions & 0 deletions test/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {parseInputArray} from '../src/util';

describe('parseInputArray', () => {
beforeAll(() => {
process.env.GITHUB_EVENT_PATH = 'a';
process.env.GITHUB_EVENT_NAME = 'b';
process.env.GITHUB_REF = 'c';
process.env.GITHUB_ACTOR = 'd';
});

afterAll(() => {
delete process.env.GITHUB_EVENT_PATH;
delete process.env.GITHUB_EVENT_NAME;
delete process.env.GITHUB_REF;
delete process.env.GITHUB_ACTOR;
});

it('parses string arrays', () => {
expect(parseInputArray('["a", "bcd"]')).toStrictEqual(['a', 'bcd']);
});

it('passes strings through', () => {
expect(parseInputArray('"hello"')).toStrictEqual(['"hello"']);
});

it('passes non-string elements through', () => {
expect(parseInputArray('[42]')).toStrictEqual(['[42]']);
});

it('ignores failures', () => {
expect(parseInputArray('"')).toStrictEqual(['"']);
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"rootDir": "./src",
"rootDir": ".",
"outDir": "./lib",
"composite": false,
"declaration": false
Expand Down
Loading