Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove any usage #23

Merged
merged 1 commit into from
Jun 12, 2024
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
5 changes: 1 addition & 4 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ export default tseslint.config(
ecmaVersion: 2022,
sourceType: 'module',
},
rules: {
// We should not use `any` in our codebase, but there are quite a few violations today.
'@typescript-eslint/no-explicit-any': 'warn',
},
rules: {},
files: ['src/**/*.ts'],
},
{
Expand Down
18 changes: 13 additions & 5 deletions src/adapters/humctl/HumctlAdapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { execFile } from 'child_process';
import { execFile, ExecFileException } from 'child_process';
import * as util from 'util';

import { HumctlResult } from './HumctlResult';
Expand Down Expand Up @@ -61,10 +61,18 @@ export class HumctlAdapter implements IHumctlAdapter {
result = await exec(humctlFilePath, command, {
env: await this.prepareEnvVars(),
});
} catch (error: any) {
statusCode = error.code;
result.stderr = error.stderr;
result.stdout = error.stdout;
} catch (error: unknown) {
const execErr = error as ExecFileException;

if (execErr.code) {
if (typeof execErr.code === 'string') {
statusCode = parseInt(execErr.code, 10);
} else {
statusCode = execErr.code;
}
}
result.stderr = execErr.stderr || '';
result.stdout = execErr.stdout || '';
}

// Ensure stderr and stdout is not undefined before its processing
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/ValidateScoreFileController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ export class ValidateScoreFileController {

private isScoreFile(textDocument: TextDocument): boolean {
try {
const loadedYamlDocument: any = yaml.load(textDocument.getText());
const loadedYamlDocument: unknown = yaml.load(textDocument.getText());
if (!(loadedYamlDocument instanceof Object)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/errors/IHumanitecExtensionError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface IHumanitecExtensionError {
}

export function isHumanitecExtensionError(
error: any
error: unknown
): error is IHumanitecExtensionError {
const isObject = error !== null && typeof error === 'object';
if (isObject) {
Expand Down
2 changes: 1 addition & 1 deletion src/errors/UnexpectedEmptyOutputError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export class UnexpectedEmptyOutputError implements IHumanitecExtensionError {
constructor(
private binaryPath: string,
private command: string[],
private options: any
private options: unknown
) {}

message(): string {
Expand Down
11 changes: 10 additions & 1 deletion src/repos/ApplicationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ export interface IApplicationRepository {
getFrom(organizationId: string): Promise<Application[]>;
}

interface RawApplication {
metadata: {
id: string;
};
entity: {
name: string;
};
}

export class ApplicationRepository implements IApplicationRepository {
constructor(private humctl: IHumctlAdapter) {}

Expand All @@ -18,7 +27,7 @@ export class ApplicationRepository implements IApplicationRepository {
const applications: Application[] = [];

const rawApplications = JSON.parse(result.stdout);
rawApplications.forEach((rawApplication: any) => {
rawApplications.forEach((rawApplication: RawApplication) => {
const application = new Application(
rawApplication['metadata']['id'],
rawApplication['entity']['name'],
Expand Down
11 changes: 10 additions & 1 deletion src/repos/EnvironmentRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ export interface IEnvironmentRepository {
): Promise<Environment[]>;
}

interface RawEnvironment {
metadata: {
id: string;
};
entity: {
name: string;
};
}

export class EnvironmentRepository implements IEnvironmentRepository {
constructor(private humctl: IHumctlAdapter) {}

Expand All @@ -26,7 +35,7 @@ export class EnvironmentRepository implements IEnvironmentRepository {
const environments: Environment[] = [];

const rawEnvironments = JSON.parse(result.stdout);
rawEnvironments.forEach((rawEnvironment: any) => {
rawEnvironments.forEach((rawEnvironment: RawEnvironment) => {
const environment = new Environment(
rawEnvironment['metadata']['id'],
rawEnvironment['entity']['name'],
Expand Down
11 changes: 10 additions & 1 deletion src/repos/OrganizationRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ export interface IOrganizationRepository {
getAll(): Promise<Organization[]>;
}

interface RawOrganization {
metadata: {
id: string;
};
entity: {
name: string;
};
}

export class OrganizationRepository implements IOrganizationRepository {
constructor(private humctl: IHumctlAdapter) {}

Expand All @@ -13,7 +22,7 @@ export class OrganizationRepository implements IOrganizationRepository {
const organizations: Organization[] = [];

const rawOrganizations = JSON.parse(result.stdout);
rawOrganizations.forEach((rawOrganization: any) => {
rawOrganizations.forEach((rawOrganization: RawOrganization) => {
const organization = new Organization(
rawOrganization['metadata']['id'],
rawOrganization['entity']['name']
Expand Down
89 changes: 58 additions & 31 deletions src/repos/ResourceTypeRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,31 @@ export interface IResourceTypeRepository {
get(name: string): Promise<ResourceType>;
}

interface Properties {
properties: {
[key: string]: {
description: string;
title: string;
type: string;
};
};
required: string[];
}

interface Schema {
properties: {
values?: Properties;
secrets?: Properties;
};
}

interface AvailableResourceTypeOutput {
Name: string;
Type: string;
Category: string;
InputsSchema: any;
OutputsSchema: any;
// Casting JSON to Map<string, string> doesn't work as expected, that's why it has to be any
Classes: any;
InputsSchema: Schema;
OutputsSchema: Schema;
Classes: { [key: string]: string };
}

export class ResourceTypeRepository implements IResourceTypeRepository {
Expand Down Expand Up @@ -51,8 +68,8 @@ export class ResourceTypeRepository implements IResourceTypeRepository {
resourceType.Category,
resourceType.Name,
resourceType.Type,
this.resolveVariables(resourceType.InputsSchema),
this.resolveVariables(resourceType.OutputsSchema),
this.resolveSchema(resourceType.InputsSchema),
this.resolveSchema(resourceType.OutputsSchema),
resourceTypeClasses
);
}
Expand All @@ -79,56 +96,66 @@ export class ResourceTypeRepository implements IResourceTypeRepository {
availableResourceType.Category,
availableResourceType.Name,
availableResourceType.Type,
this.resolveVariables(availableResourceType.InputsSchema),
this.resolveVariables(availableResourceType.OutputsSchema),
this.resolveSchema(availableResourceType.InputsSchema),
this.resolveSchema(availableResourceType.OutputsSchema),
resourceTypeClasses
);
resourceTypes.push(resourceType);
});
return resourceTypes;
}

private resolveVariables(
rawVariables: any
private resolveSchema(
rawSchema: Schema | null
): Map<string, ResourceTypeVariable> {
const result = new Map<string, ResourceTypeVariable>();
if (rawVariables === null) {
if (rawSchema === null) {
return result;
}
const properties = rawVariables['properties'];
const properties = rawSchema['properties'];
if (properties === undefined) {
return result;
}

if ('values' in properties) {
const values = this.resolveVariables(properties['values']);
if (properties.values) {
const values = this.resolveProperties(properties['values']);
values.forEach((value: ResourceTypeVariable, key: string) => {
result.set(key, value);
});
}
if ('secrets' in properties) {
const secrets = this.resolveVariables(properties['secrets']);
if (properties.secrets) {
const secrets = this.resolveProperties(properties['secrets']);
secrets.forEach((value: ResourceTypeVariable, key: string) => {
result.set(key, value);
});
}

if (!('values' in properties || 'secrets' in properties)) {
let requiredProperties: string[] = rawVariables['required'];
if (requiredProperties === undefined) {
requiredProperties = [];
}
return result;
}

let property: keyof typeof properties;
for (property in properties) {
const variable = new ResourceTypeVariable(
properties[property]['description'],
properties[property]['title'],
properties[property]['type'],
requiredProperties.includes(property)
);
result.set(property, variable);
}
private resolveProperties(
rawVariables: Properties
): Map<string, ResourceTypeVariable> {
const result = new Map<string, ResourceTypeVariable>();
const properties = rawVariables['properties'];
if (properties === undefined) {
return result;
}

let requiredProperties: string[] = rawVariables['required'];
if (requiredProperties === undefined) {
requiredProperties = [];
}

let property: keyof typeof properties;
for (property in properties) {
const variable = new ResourceTypeVariable(
properties[property]['description'],
properties[property]['title'],
properties[property]['type'],
requiredProperties.includes(property)
);
result.set(property, variable);
}

return result;
Expand Down
18 changes: 13 additions & 5 deletions src/repos/SecretRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ export class SecretRepository implements ISecretRepository {
let configFile: Buffer = Buffer.from([]);
try {
configFile = readFileSync(configPath);
} catch (error: any) {
if (error.code && error.code === 'ENOENT') {
console.log(error);
} catch (error: unknown) {
if (
error instanceof Error &&
'code' in error &&
error.code === 'ENOENT'
) {
// Ignore
} else {
throw error;
}
Expand All @@ -51,8 +55,12 @@ export class SecretRepository implements ISecretRepository {
value = '';
}
return value;
} catch (error: any) {
if (error.code && error.code === 'ENOENT') {
} catch (error: unknown) {
if (
error instanceof Error &&
'code' in error &&
error.code === 'ENOENT'
) {
return '';
} else {
throw error;
Expand Down
14 changes: 12 additions & 2 deletions src/services/LoginService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export interface ILoginService {
confirmDeviceAuthorization(info: DeviceAuthorizationInfo): Promise<string>;
}

interface DeviceResponse {
security_code: string;
verification_url: string;
}

interface DevicePollResponse {
accepted: boolean;
access_token: string;
}

export class LoginService implements ILoginService {
constructor() {}
async initDeviceAuthorization(): Promise<DeviceAuthorizationInfo> {
Expand All @@ -22,7 +32,7 @@ export class LoginService implements ILoginService {
if (response.status !== 200) {
throw new AuthorizationError(await response.text());
}
const body: any = await response.json();
const body = (await response.json()) as DeviceResponse;
if (!body.security_code || !body.verification_url) {
throw new AuthorizationError(await response.text());
}
Expand All @@ -47,7 +57,7 @@ export class LoginService implements ILoginService {
}
);
if (response.status === 200) {
const body: any = await response.json();
const body = (await response.json()) as DevicePollResponse;
if (!body.accepted) {
throw new AuthorizationError(await response.text());
}
Expand Down
8 changes: 7 additions & 1 deletion src/services/ScoreValidationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export class ValidationError {
}
}

interface RawValidationError {
Level: string;
Location: string;
Message: string;
}

export class ScoreValidationService implements IScoreValidationService {
constructor(private humctl: IHumctlAdapter) {}

Expand All @@ -28,7 +34,7 @@ export class ScoreValidationService implements IScoreValidationService {
return validationErrors;
}
const validationRawErrors = JSON.parse(result.stdout);
validationRawErrors.forEach((validationRawError: any) => {
validationRawErrors.forEach((validationRawError: RawValidationError) => {
validationErrors.push(
new ValidationError(
validationRawError.Level,
Expand Down