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
12 changes: 6 additions & 6 deletions __mocks__/@actions/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
// https://jestjs.io/docs/manual-mocks#mocking-node-modules), so no explicit
// jest.mock('@actions/core') call is required.
module.exports = {
debug: jest.fn(),
error: jest.fn(),
warning: jest.fn(),
info: jest.fn(),
setFailed: jest.fn(),
getInput: jest.fn(),
debug: jest.fn(),
error: jest.fn(),
warning: jest.fn(),
info: jest.fn(),
setFailed: jest.fn(),
getInput: jest.fn(),
};
2 changes: 1 addition & 1 deletion __mocks__/@actions/tool-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
// Jest applies manual mocks for node modules automatically (see
// https://jestjs.io/docs/manual-mocks#mocking-node-modules).
module.exports = {
downloadTool: jest.fn(),
downloadTool: jest.fn(),
};
6 changes: 3 additions & 3 deletions __tests__/argocd/AppCollection.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect, test} from '@jest/globals';
import { expect, test } from '@jest/globals';

import {type App} from '../../src/argocd/App.js';
import {AppCollection} from '../../src/argocd/AppCollection.js';
import { type App } from '../../src/argocd/App.js';
import { AppCollection } from '../../src/argocd/AppCollection.js';

test('filterByRepo removes apps from other repos', () => {
expect(appCollection().filterByRepo('argocd-diff-action/app-one')).toStrictEqual(
Expand Down
10 changes: 5 additions & 5 deletions __tests__/argocd/ArgoCDServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ describe('ArgoCDServer tests', function () {
});

test('ArgoCDServer throws on 401', async () => {
fetchMock.get('https://argocd.example/api/v1/applications', {status: 401, body: '{"error":"no session information","code":16,"message":"no session information"}'});
fetchMock.get('https://argocd.example/api/v1/applications', { status: 401, body: '{"error":"no session information","code":16,"message":"no session information"}' });

expect(argocdServer().getAppCollection()).rejects.toThrow();
});

test('ArgoCDServer throws on 500', async () => {
fetchMock.get('https://argocd.example/api/v1/applications', {status: 500, body: ''});
fetchMock.get('https://argocd.example/api/v1/applications', { status: 500, body: '' });

expect(argocdServer().getAppCollection()).rejects.toThrow();
});

test('ArgoCDServer uses http when argocd-server-tls is false', async () => {
fetchMock.get('http://argocd.example/api/v1/applications', { response: { status: 200, body: '{}' }});
fetchMock.get('http://argocd.example/api/v1/applications', { response: { status: 200, body: '{}' } });

// mock response from fetch used in getServerVersion.
fetchMock.anyOnce(
Expand All @@ -66,7 +66,7 @@ describe('ArgoCDServer tests', function () {
);
mockedDownloadTool.mockReturnValueOnce(Promise.resolve('/path/to/tool'));

await argocdServer({ protocol: 'http', uri: 'http://argocd.example', fqdn: 'argocd.example'}).installArgoCDCommand('');
await argocdServer({ protocol: 'http', uri: 'http://argocd.example', fqdn: 'argocd.example' }).installArgoCDCommand('');

expect(mockedDownloadTool).toHaveBeenCalledWith(
'https://github.com/argoproj/argo-cd/releases/download/v2.4.0/argocd-linux-amd64',
Expand All @@ -75,7 +75,7 @@ describe('ArgoCDServer tests', function () {
});

test('ArgoCDServer installArgoCDCommand defaults to server version & calls downloadTool', async () => {
fetchMock.get('https://argocd.example/api/v1/applications', { response: { status: 200, body: '{}' }});
fetchMock.get('https://argocd.example/api/v1/applications', { response: { status: 200, body: '{}' } });

// mock response from fetch used in getServerVersion.
fetchMock.anyOnce(
Expand Down
14 changes: 7 additions & 7 deletions __tests__/parseHeaders.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
import {expect, test} from '@jest/globals';
import {parseHeaders} from '../src/getActionInput.js';
import { expect, test } from '@jest/globals';
import { parseHeaders } from '../src/getActionInput.js';

test('parseHeaders with a single header', () => {
const headers = parseHeaders('Authorization: Bearer super-secret-bearer-token');
expect(headers).toStrictEqual(new Map<string, string>(Object.entries({
'Authorization': 'Bearer super-secret-bearer-token',
})))
Authorization: 'Bearer super-secret-bearer-token',
})));
});

test('parseHeaders with multiple headers', () => {
const headers = parseHeaders('Authorization: Bearer super-secret-bearer-token, X-Example: example-value');
expect(headers).toStrictEqual(new Map<string, string>(Object.entries({
'Authorization': 'Bearer super-secret-bearer-token',
'X-Example': 'example-value',
})))
})));
});

test('parseHeaders with a bad header', () => {
expect(() => {
parseHeaders('Authorization')
parseHeaders('Authorization');
}).toThrow();
});

test('parseHeaders with empty string', () => {
const headers = parseHeaders('');
expect(headers).toStrictEqual(new Map<string, string>());
});
});
10 changes: 9 additions & 1 deletion eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default tseslint.config(
},
],

'@stylistic/func-call-spacing': ['error', 'never'],
'@stylistic/function-call-spacing': ['error', 'never'],
'@typescript-eslint/no-array-constructor': 'error',
'@typescript-eslint/no-empty-interface': 'error',
'@typescript-eslint/no-explicit-any': 'error',
Expand All @@ -77,4 +77,12 @@ export default tseslint.config(
'@stylistic/type-annotation-spacing': 'error',
'@typescript-eslint/unbound-method': 'error',
},
},
{
files: ['__tests__/**', '__mocks__/**'],
languageOptions: {
globals: {
...globals.jest,
},
},
});
9 changes: 1 addition & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,5 @@
"typescript": "^5.9.2",
"typescript-eslint": "^8.59.4"
},
"packageManager": "[email protected]+sha512.cdf928fca20832cd59ec53826492b7dc25dc524d4370b6b4adbf65803d32efaa6c1c88147c0ae4e8d579a6c9eec715757b50d4fa35eea179d868eada4ed043af",
"pnpm": {
"onlyBuiltDependencies": [
"pre-commit",
"spawn-sync",
"unrs-resolver"
]
}
"packageManager": "[email protected]+sha512.36e6621fad506178936455e70247b8808ef4ec25797a9f437a93281a020484e2607f6a469a22e982987c3dbb8866e3071514ab10a4a1749e06edcd1ec118436f"
}
4 changes: 4 additions & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
allowBuilds:
pre-commit: true
spawn-sync: true
unrs-resolver: true
42 changes: 24 additions & 18 deletions src/argocd/ArgoCDServer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {chmodSync} from 'fs';
import { chmodSync } from 'fs';
import * as core from '@actions/core';
import {downloadTool} from '@actions/tool-cache';
import { downloadTool } from '@actions/tool-cache';

import {type App} from './App.js';
import {AppCollection} from './AppCollection.js';
import {type AppTargetRevision} from './AppTargetRevision.js';
import {type Diff} from '../Diff.js';
import {type ExecResult, execCommand, scrubSecrets} from '../lib.js';
import {ActionInput} from '../getActionInput.js';
import {URL} from "node:url";
import { type App } from './App.js';
import { AppCollection } from './AppCollection.js';
import { type AppTargetRevision } from './AppTargetRevision.js';
import { type Diff } from '../Diff.js';
import { type ExecResult, execCommand, scrubSecrets } from '../lib.js';
import { ActionInput } from '../getActionInput.js';
import { URL } from 'node:url';

export class ArgoCDServer {
binaryPath = 'bin/argo';
Expand Down Expand Up @@ -56,10 +56,10 @@
}

async getAppLocalDiff(app: App): Promise<Diff> {
if (app.spec.source?.path === undefined) {
core.error(`Cannot diff ${app.metadata.name}, no source.path`);

return {app, diff: ''} as Diff;
return { app, diff: '' };

Check warning on line 62 in src/argocd/ArgoCDServer.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}

Check warning on line 63 in src/argocd/ArgoCDServer.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🌿 Branch is not covered

Warning! Not covered branch

return this.getAppDiff(app, [`--local=${app.spec.source.path}`]);
Expand All @@ -77,20 +77,21 @@
);
core.debug(`stdout: ${res.stdout}`);
core.debug(`stderr: ${res.stderr}`);
return {app, diff: res.stdout} as Diff;
} catch (e) {
return { app, diff: res.stdout };
}
catch (e) {
res = e as ExecResult;
core.error('Unexpected error when fetching app diff:');
core.error(`${res.err}`);
return {app, diff: '', error: res} as Diff;
return { app, diff: '', error: res };
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
async api(endpoint: string, params: { [key: string]: string } = {}, method = 'GET'): Promise<any> {
const url = new URL(`${this.uri}/api/${endpoint}`);

for (let paramsKey in params) {
for (const paramsKey in params) {
if (params[paramsKey]) {
url.searchParams.append(paramsKey, params[paramsKey]);
}
Expand All @@ -101,6 +102,7 @@
// response.json() returns `any`.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let responseText: any;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let responseJson: any;

try {
Expand All @@ -120,7 +122,8 @@
}

responseJson = JSON.parse(responseText);
} catch (err) {
}
catch (err) {
if (err instanceof Error) {
core.error(`Failed to fetch ${endpoint} from ${this.uri}.`);
core.error(`Response Text: ${responseText}`);
Expand Down Expand Up @@ -170,7 +173,8 @@
appCollectionDiffPromises.push(
this.getAppRevisionDiff(app, appTargetRevision.targetRevision),
);
} else {
}
else {
core.warning(
`Could not find Application '${appTargetRevision.appName}' in AppCollection for revision diffs.`,
);
Expand All @@ -191,10 +195,12 @@
if (appDiff.error) {
core.setFailed(`ArgoCD diff failed for Application '${appDiff.app.metadata.name}'`);
diffs.push(appDiff); // Surface the error to the PR comment.
} else if (appDiff.diff != '') {
}
else if (appDiff.diff != '') {
core.info(`Found diff for Application '${appDiff.app.metadata.name}'.`);
diffs.push(appDiff);
} else {
}
else {
core.debug(`No diff found for Application '${appDiff.app.metadata.name}'.`);
}
});
Expand Down
3 changes: 1 addition & 2 deletions src/getActionInput.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as core from '@actions/core';
import assert from 'node:assert/strict';

export interface ActionInput {
arch: string;
Expand All @@ -26,7 +25,7 @@ export function parseHeaders(input: string): Map<string, string> {
continue;
}

let [header, value] = item.split(':').map(s => s.trim());
const [header, value] = item.split(':').map(s => s.trim());

if (!header || header === '') {
throw new Error(`Header name cannot be empty: ${item}`);
Expand Down
Loading