Skip to content

Commit

Permalink
refactor(platform:bitbucket): strict null checks (#15196)
Browse files Browse the repository at this point in the history
* refactor(platform:bitbucket): strict null checks

* refactor: wrong change
  • Loading branch information
viceice committed Apr 20, 2022
1 parent 62694ef commit e8808f3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
30 changes: 18 additions & 12 deletions lib/modules/platform/bitbucket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const defaults = { endpoint: BITBUCKET_PROD_ENDPOINT };

const pathSeparator = '/';

let renovateUserUuid: string;
let renovateUserUuid: string | null = null;

export async function initPlatform({
endpoint,
Expand Down Expand Up @@ -136,7 +136,8 @@ export async function getJsonFile(
repoName?: string,
branchOrTag?: string
): Promise<any | null> {
const raw = await getRawFile(fileName, repoName, branchOrTag);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const raw = (await getRawFile(fileName, repoName, branchOrTag)) as string;
if (fileName.endsWith('.json5')) {
return JSON5.parse(raw);
}
Expand Down Expand Up @@ -191,7 +192,8 @@ export async function initRepo({
// Converts API hostnames to their respective HTTP git hosts:
// `api.bitbucket.org` to `bitbucket.org`
// `api-staging.<host>` to `staging.<host>`
const hostnameWithoutApiPrefix = regEx(/api[.|-](.+)/).exec(hostname)[1];
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const hostnameWithoutApiPrefix = regEx(/api[.|-](.+)/).exec(hostname!)?.[1];

const url = git.getUrl({
protocol: 'https',
Expand Down Expand Up @@ -261,7 +263,7 @@ export async function findPr({
if (pr) {
logger.debug(`Found PR #${pr.number}`);
}
return pr;
return pr ?? null;
}

// Gets details for a PR
Expand Down Expand Up @@ -297,7 +299,9 @@ interface BranchResponse {
}

// Return the commit SHA for a branch
async function getBranchCommit(branchName: string): Promise<string | null> {
async function getBranchCommit(
branchName: string
): Promise<string | undefined> {
try {
const branch = (
await bitbucketHttp.getJson<BranchResponse>(
Expand All @@ -309,7 +313,7 @@ async function getBranchCommit(branchName: string): Promise<string | null> {
return branch.target.hash;
} catch (err) /* istanbul ignore next */ {
logger.debug({ err }, `getBranchCommit('${branchName}') failed'`);
return null;
return undefined;
}
}

Expand Down Expand Up @@ -373,7 +377,8 @@ export async function getBranchStatusCheck(
): Promise<BranchStatus | null> {
const statuses = await getStatus(branchName);
const bbState = statuses.find((status) => status.key === context)?.state;
return bbToRenovateStatusMapping[bbState] || null;
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
return bbToRenovateStatusMapping[bbState!] || null;
}

export async function setBranchStatus({
Expand Down Expand Up @@ -428,7 +433,7 @@ async function findOpenIssues(title: string): Promise<BbIssue[]> {
}
}

export async function findIssue(title: string): Promise<Issue> {
export async function findIssue(title: string): Promise<Issue | null> {
logger.debug(`findIssue(${title})`);

/* istanbul ignore if */
Expand Down Expand Up @@ -485,7 +490,7 @@ export async function ensureIssue({
}
try {
let issues = await findOpenIssues(title);
if (!issues.length) {
if (!issues.length && reuseTitle) {
issues = await findOpenIssues(reuseTitle);
}
if (issues.length) {
Expand All @@ -496,7 +501,7 @@ export async function ensureIssue({
const [issue] = issues;
if (
issue.title !== title ||
String(issue.content.raw).trim() !== description.trim()
String(issue.content?.raw).trim() !== description.trim()
) {
logger.debug('Issue updated');
await bitbucketHttp.putJson(
Expand Down Expand Up @@ -593,7 +598,8 @@ export async function addReviewers(
): Promise<void> {
logger.debug(`Adding reviewers '${reviewers.join(', ')}' to #${prId}`);

const { title } = await getPr(prId);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
const { title } = (await getPr(prId)) as Pr;

const body = {
title,
Expand Down Expand Up @@ -636,7 +642,7 @@ export function ensureCommentRemoval(
async function sanitizeReviewers(
reviewers: Account[],
err: any
): Promise<Account[]> {
): Promise<Account[] | undefined> {
if (err.statusCode === 400 && err.body?.error?.fields?.reviewers) {
const sanitizedReviewers: Account[] = [];

Expand Down
4 changes: 2 additions & 2 deletions lib/modules/platform/bitbucket/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ const bitbucketMergeStrategies: Map<MergeStrategy, BitbucketMergeStrategy> =
]);

export function mergeBodyTransformer(
mergeStrategy: MergeStrategy
mergeStrategy: MergeStrategy | undefined
): MergeRequestBody {
const body: MergeRequestBody = {
close_source_branch: true,
};

// The `auto` strategy will use the strategy configured inside Bitbucket.
if (mergeStrategy !== 'auto') {
if (mergeStrategy && mergeStrategy !== 'auto') {
body.merge_strategy = bitbucketMergeStrategies.get(mergeStrategy);
}

Expand Down
9 changes: 8 additions & 1 deletion lib/util/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ export const redactedFields = [
'password',
];

export function sanitize(input: string): string {
// TODO: returns null or undefined only when input is null or undefined.
export function sanitize(input: string): string;
export function sanitize(
input: string | null | undefined
): string | null | undefined;
export function sanitize(
input: string | null | undefined
): string | null | undefined {
if (!input) {
return input;
}
Expand Down
1 change: 0 additions & 1 deletion tsconfig.strict.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"lib/config/validation.ts",
"lib/modules/datasource/github-releases/test/index.ts",
"lib/modules/platform/api.ts",
"lib/modules/platform/bitbucket/index.ts",
"lib/modules/platform/bitbucket-server/index.ts",
"lib/modules/platform/bitbucket-server/utils.ts",
"lib/modules/platform/comment.ts",
Expand Down

0 comments on commit e8808f3

Please sign in to comment.