Skip to content

Commit

Permalink
refactor(manager): strict null checks (#15151)
Browse files Browse the repository at this point in the history
* refactor: strict null checks for util

* chore: fix type

* Update tsconfig.strict.json

* Update lib/util/package-rules.ts

* Update lib/util/package-rules.ts

* chore: fix test and coverage

* chore: fix package rules

* refactor(manager): strict null checks

* chore: revert config changes
  • Loading branch information
viceice committed Apr 17, 2022
1 parent 4f0459d commit da6ba64
Show file tree
Hide file tree
Showing 32 changed files with 191 additions and 218 deletions.
2 changes: 1 addition & 1 deletion lib/modules/manager/ansible-galaxy/collections-metadata.ts
Expand Up @@ -16,7 +16,7 @@ export function extractCollectionsMetaDataFile(
} else if (foundDependencyBlock) {
// expects a line like this ` ansible.windows: "1.4.0"`
const galaxyRegExResult = galaxyRegEx.exec(line);
if (galaxyRegExResult) {
if (galaxyRegExResult?.groups) {
const dep: PackageDependency = {
depType: 'galaxy-collection',
datasource: GalaxyCollectionDatasource.id,
Expand Down
20 changes: 10 additions & 10 deletions lib/modules/manager/ansible-galaxy/collections.ts
Expand Up @@ -3,6 +3,7 @@ import { GalaxyCollectionDatasource } from '../../datasource/galaxy-collection';
import { GitTagsDatasource } from '../../datasource/git-tags';
import { GithubTagsDatasource } from '../../datasource/github-tags';
import type { PackageDependency } from '../types';
import type { AnsibleGalaxyPackageDependency } from './types';
import {
blockLineRegEx,
galaxyDepRegex,
Expand All @@ -12,8 +13,7 @@ import {

function interpretLine(
lineMatch: RegExpMatchArray,
lineNumber: number,
dependency: PackageDependency
dependency: AnsibleGalaxyPackageDependency
): void {
const localDependency = dependency;
const key = lineMatch[2];
Expand Down Expand Up @@ -45,12 +45,12 @@ function interpretLine(
}

function handleGitDep(
dep: PackageDependency,
nameMatch: RegExpExecArray
dep: AnsibleGalaxyPackageDependency,
nameMatch: RegExpExecArray | null
): void {
dep.datasource = GitTagsDatasource.id;

if (nameMatch) {
if (nameMatch?.groups) {
// if a github.com repository is referenced use github-tags instead of git-tags
if (nameMatch.groups.hostname === 'github.com') {
dep.datasource = GithubTagsDatasource.id;
Expand All @@ -76,14 +76,14 @@ function handleGitDep(
}
}

function handleGalaxyDep(dep: PackageDependency): void {
function handleGalaxyDep(dep: AnsibleGalaxyPackageDependency): void {
dep.datasource = GalaxyCollectionDatasource.id;
dep.depName = dep.managerData.name;
dep.registryUrls = dep.managerData.source ? [dep.managerData.source] : [];
dep.currentValue = dep.managerData.version;
}

function finalize(dependency: PackageDependency): boolean {
function finalize(dependency: AnsibleGalaxyPackageDependency): boolean {
const dep = dependency;
dep.depName = dep.managerData.name;

Expand Down Expand Up @@ -131,7 +131,7 @@ export function extractCollections(lines: string[]): PackageDependency[] {
for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
let lineMatch = newBlockRegEx.exec(lines[lineNumber]);
if (lineMatch) {
const dep: PackageDependency = {
const dep: AnsibleGalaxyPackageDependency = {
depType: 'galaxy-collection',
managerData: {
name: null,
Expand All @@ -141,7 +141,7 @@ export function extractCollections(lines: string[]): PackageDependency[] {
},
};
do {
interpretLine(lineMatch, lineNumber, dep);
interpretLine(lineMatch, dep);
const line = lines[lineNumber + 1];

if (!line) {
Expand All @@ -153,7 +153,7 @@ export function extractCollections(lines: string[]): PackageDependency[] {
}
} while (lineMatch);
if (finalize(dep)) {
delete dep.managerData;
delete (dep as PackageDependency).managerData;
deps.push(dep);
}
}
Expand Down
28 changes: 14 additions & 14 deletions lib/modules/manager/ansible-galaxy/roles.ts
Expand Up @@ -2,6 +2,7 @@ import { regEx } from '../../../util/regex';
import { GalaxyDatasource } from '../../datasource/galaxy';
import { GitTagsDatasource } from '../../datasource/git-tags';
import type { PackageDependency } from '../types';
import type { AnsibleGalaxyPackageDependency } from './types';
import {
blockLineRegEx,
galaxyDepRegex,
Expand All @@ -12,9 +13,9 @@ import {
function interpretLine(
lineMatch: RegExpMatchArray,
lineNumber: number,
dependency: PackageDependency
): PackageDependency {
const localDependency: PackageDependency = dependency;
dependency: AnsibleGalaxyPackageDependency
): AnsibleGalaxyPackageDependency | null {
const localDependency = dependency;
const key = lineMatch[2];
const value = lineMatch[3].replace(regEx(/["']/g), '');
switch (key) {
Expand Down Expand Up @@ -43,28 +44,27 @@ function interpretLine(
return localDependency;
}

function finalize(dependency: PackageDependency): boolean {
function finalize(dependency: AnsibleGalaxyPackageDependency): boolean {
const dep = dependency;
if (dependency.managerData.version === null) {
dep.skipReason = 'no-version';
return false;
}

const source: string = dep.managerData.src;
const source = dep.managerData.src ?? '';
const sourceMatch = nameMatchRegex.exec(source);
if (sourceMatch) {
if (sourceMatch?.groups) {
dep.datasource = GitTagsDatasource.id;
dep.depName = sourceMatch.groups.depName.replace(regEx(/.git$/), '');
// remove leading `git+` from URLs like `git+https://...`
dep.packageName = source.replace(regEx(/git\+/), '');
} else if (galaxyDepRegex.exec(source)) {
dep.datasource = GalaxyDatasource.id;
dep.depName = dep.managerData.src;
dep.packageName = dep.managerData.src;
} else if (galaxyDepRegex.exec(dep.managerData.name)) {
dep.depName = source;
dep.packageName = source;
} else if (galaxyDepRegex.exec(dep.managerData.name ?? '')) {
dep.datasource = GalaxyDatasource.id;
dep.depName = dep.managerData.name;
dep.packageName = dep.managerData.name;
dep.depName = dep.managerData.name!;
dep.packageName = dep.managerData.name!;
} else {
dep.skipReason = 'no-source-match';
return false;
Expand All @@ -82,7 +82,7 @@ export function extractRoles(lines: string[]): PackageDependency[] {
for (let lineNumber = 0; lineNumber < lines.length; lineNumber += 1) {
let lineMatch = newBlockRegEx.exec(lines[lineNumber]);
if (lineMatch) {
const dep: PackageDependency = {
const dep: AnsibleGalaxyPackageDependency = {
depType: 'role',
managerData: {
name: null,
Expand All @@ -107,7 +107,7 @@ export function extractRoles(lines: string[]): PackageDependency[] {
}
} while (lineMatch);
if (finalize(dep)) {
delete dep.managerData;
delete (dep as PackageDependency).managerData;
deps.push(dep);
}
}
Expand Down
7 changes: 7 additions & 0 deletions lib/modules/manager/ansible-galaxy/types.ts
@@ -0,0 +1,7 @@
import type { PackageDependency } from '../types';

export type AnsibleGalaxyPackageDependency = Omit<
PackageDependency,
'managerData'
> &
Required<Pick<PackageDependency, 'managerData'>>;
8 changes: 4 additions & 4 deletions lib/modules/manager/argocd/extract.ts
Expand Up @@ -8,7 +8,7 @@ import { fileTestRegex } from './util';

function createDependency(
definition: ApplicationDefinition
): PackageDependency {
): PackageDependency | null {
let source: ApplicationSource;
switch (definition.kind) {
case 'Application':
Expand Down Expand Up @@ -45,8 +45,8 @@ function createDependency(

export function extractPackageFile(
content: string,
fileName: string,
config?: ExtractConfig
_fileName: string,
_config?: ExtractConfig
): PackageFile | null {
// check for argo reference. API version for the kind attribute is used
if (fileTestRegex.test(content) === false) {
Expand All @@ -57,7 +57,7 @@ export function extractPackageFile(

const deps = definitions
.map((definition) => createDependency(definition))
.filter(Boolean);
.filter(is.truthy);

return deps.length ? { deps } : null;
}
4 changes: 2 additions & 2 deletions lib/modules/manager/azure-pipelines/extract.ts
Expand Up @@ -52,9 +52,9 @@ export function parseAzurePipelines(
content: string,
filename: string
): AzurePipelines | null {
let pkg = null;
let pkg: AzurePipelines | null = null;
try {
pkg = load(content, { json: true });
pkg = load(content, { json: true }) as AzurePipelines;
} catch (err) /* istanbul ignore next */ {
logger.info({ filename, err }, 'Error parsing azure-pipelines content');
return null;
Expand Down
3 changes: 2 additions & 1 deletion lib/modules/manager/batect-wrapper/artifacts.ts
Expand Up @@ -34,7 +34,8 @@ export async function updateArtifacts({
packageFileName,
config,
}: UpdateArtifact): Promise<UpdateArtifactsResult[] | null> {
const version = config.newVersion;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const version = config.newVersion!;

logger.debug({ version, packageFileName }, 'Updating Batect wrapper scripts');

Expand Down
5 changes: 3 additions & 2 deletions lib/modules/manager/batect/extract.ts
@@ -1,3 +1,4 @@
import is from '@sindresorhus/is';
import { load } from 'js-yaml';
import upath from 'upath';
import { logger } from '../../../logger';
Expand Down Expand Up @@ -33,8 +34,8 @@ function extractImages(config: BatectConfig): string[] {
}

return Object.values(config.containers)
.filter((container) => container.image !== undefined)
.map((container) => container.image);
.map((container) => container.image)
.filter(is.string);
}

function createImageDependency(tag: string): PackageDependency {
Expand Down
38 changes: 22 additions & 16 deletions lib/modules/manager/bazel/extract.ts
Expand Up @@ -12,19 +12,21 @@ import * as dockerVersioning from '../../versioning/docker';
import type { PackageDependency, PackageFile } from '../types';
import type { UrlParsedResult } from './types';

function parseUrl(urlString: string): UrlParsedResult | null {
function parseUrl(
urlString: string | undefined | null
): UrlParsedResult | null {
// istanbul ignore if
if (!urlString) {
return null;
}
const url = _parse(urlString);
if (url.host !== 'github.com') {
if (url.host !== 'github.com' || !url.path) {
return null;
}
const path = url.path.split('/').slice(1);
const repo = path[0] + '/' + path[1];
let datasource: string;
let currentValue: string = null;
let datasource = '';
let currentValue: string | null = null;
if (path[2] === 'releases' && path[3] === 'download') {
datasource = GithubReleasesDatasource.id;
currentValue = path[4];
Expand Down Expand Up @@ -168,16 +170,16 @@ export function extractPackageFile(
logger.debug({ def }, 'Checking bazel definition');
const [depType] = def.split('(', 1);
const dep: PackageDependency = { depType, managerData: { def } };
let depName: string;
let importpath: string;
let remote: string;
let currentValue: string;
let commit: string;
let url: string;
let sha256: string;
let digest: string;
let repository: string;
let registry: string;
let depName: string | undefined;
let importpath: string | undefined;
let remote: string | undefined;
let currentValue: string | undefined;
let commit: string | undefined;
let url: string | undefined;
let sha256: string | undefined;
let digest: string | undefined;
let repository: string | undefined;
let registry: string | undefined;
let match = regEx(/name\s*=\s*"([^"]+)"/).exec(def);
if (match) {
[, depName] = match;
Expand Down Expand Up @@ -252,7 +254,7 @@ export function extractPackageFile(
(currentValue || commit)
) {
dep.depName = depName;
dep.currentValue = currentValue || commit.substr(0, 7);
dep.currentValue = currentValue || commit?.substring(0, 7);
dep.datasource = GoDatasource.id;
dep.packageName = importpath;
if (remote) {
Expand All @@ -268,7 +270,7 @@ export function extractPackageFile(
if (commit) {
dep.currentValue = 'v0.0.0';
dep.currentDigest = commit;
dep.currentDigestShort = commit.substr(0, 7);
dep.currentDigestShort = commit.substring(0, 7);
dep.digestOneAndOnly = true;
}
deps.push(dep);
Expand All @@ -279,6 +281,10 @@ export function extractPackageFile(
sha256
) {
const parsedUrl = parseUrl(url);
// istanbul ignore if: needs test
if (!parsedUrl) {
return;
}
dep.depName = depName;
dep.repo = parsedUrl.repo;
if (regEx(/^[a-f0-9]{40}$/i).test(parsedUrl.currentValue)) {
Expand Down
4 changes: 4 additions & 0 deletions lib/modules/manager/bazel/types.ts
Expand Up @@ -3,3 +3,7 @@ export interface UrlParsedResult {
repo: string;
currentValue: string;
}

export interface BazelManagerData {
def: string;
}
30 changes: 20 additions & 10 deletions lib/modules/manager/bazel/update.ts
Expand Up @@ -4,6 +4,7 @@ import * as packageCache from '../../../util/cache/package';
import { Http } from '../../../util/http';
import { regEx } from '../../../util/regex';
import type { UpdateDependencyConfig } from '../types';
import type { BazelManagerData } from './types';

const http = new Http('bazel');

Expand Down Expand Up @@ -89,20 +90,21 @@ function setNewHash(content: string, hash: string): string {
export async function updateDependency({
fileContent,
upgrade,
}: UpdateDependencyConfig): Promise<string | null> {
}: UpdateDependencyConfig<BazelManagerData>): Promise<string | null> {
try {
logger.debug(
`bazel.updateDependency(): ${upgrade.newValue || upgrade.newDigest}`
);
let newDef: string;
if (upgrade.depType === 'container_pull') {
let newDef: string | undefined;
if (upgrade.depType === 'container_pull' && upgrade.managerData?.def) {
newDef = upgrade.managerData.def
.replace(regEx(/(tag\s*=\s*)"[^"]+"/), `$1"${upgrade.newValue}"`)
.replace(regEx(/(digest\s*=\s*)"[^"]+"/), `$1"${upgrade.newDigest}"`);
}
if (
upgrade.depType === 'git_repository' ||
upgrade.depType === 'go_repository'
(upgrade.depType === 'git_repository' ||
upgrade.depType === 'go_repository') &&
upgrade.managerData?.def
) {
newDef = upgrade.managerData.def
.replace(regEx(/(tag\s*=\s*)"[^"]+"/), `$1"${upgrade.newValue}"`)
Expand All @@ -114,13 +116,15 @@ export async function updateDependency({
);
}
} else if (
upgrade.depType === 'http_archive' ||
upgrade.depType === 'http_file'
(upgrade.depType === 'http_archive' || upgrade.depType === 'http_file') &&
upgrade.managerData?.def &&
(upgrade.currentValue || upgrade.currentDigest) &&
(upgrade.newValue ?? upgrade.newDigest)
) {
newDef = updateWithNewVersion(
upgrade.managerData.def,
upgrade.currentValue || upgrade.currentDigest,
upgrade.newValue || upgrade.newDigest
(upgrade.currentValue ?? upgrade.currentDigest)!,
(upgrade.newValue ?? upgrade.newDigest)!
);
const massages = {
'bazel-skylib.': 'bazel_skylib-',
Expand All @@ -145,7 +149,13 @@ export async function updateDependency({
logger.debug({ hash }, 'Calculated hash');
newDef = setNewHash(newDef, hash);
}
logger.debug({ oldDef: upgrade.managerData.def, newDef });
logger.debug({ oldDef: upgrade.managerData?.def, newDef });

// istanbul ignore if: needs test
if (!newDef) {
return null;
}

let existingRegExStr = `${upgrade.depType}\\([^\\)]+name\\s*=\\s*"${upgrade.depName}"(.*\\n)+?\\s*\\)`;
if (newDef.endsWith('\n')) {
existingRegExStr += '\n';
Expand Down

0 comments on commit da6ba64

Please sign in to comment.