refactor(platform): Fix lint warnings (#7113)

This commit is contained in:
Sergio Zharinov 2020-08-27 11:05:31 +04:00 committed by GitHub
parent 60b101d22c
commit 28d16d17bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 89 additions and 43 deletions

View file

@ -16,6 +16,7 @@ function repoMock(
projectKey: string,
repositorySlug: string
) {
const endpointStr = endpoint.toString();
const projectKeyLower = projectKey.toLowerCase();
return {
slug: repositorySlug,
@ -41,7 +42,7 @@ function repoMock(
links: {
clone: [
{
href: `${endpoint}/scm/${projectKeyLower}/${repositorySlug}.git`,
href: `${endpointStr}/scm/${projectKeyLower}/${repositorySlug}.git`,
name: 'http',
},
{
@ -51,14 +52,19 @@ function repoMock(
],
self: [
{
href: `${endpoint}/projects/${projectKey}/repos/${repositorySlug}/browse`,
href: `${endpointStr}/projects/${projectKey}/repos/${repositorySlug}/browse`,
},
],
},
};
}
function prMock(endpoint, projectKey, repositorySlug) {
function prMock(
endpoint: URL | string,
projectKey: string,
repositorySlug: string
) {
const endpointStr = endpoint.toString();
return {
id: 5,
version: 1,
@ -94,7 +100,7 @@ function prMock(endpoint, projectKey, repositorySlug) {
slug: 'userName1',
type: 'NORMAL',
links: {
self: [{ href: `${endpoint}/users/userName1` }],
self: [{ href: `${endpointStr}/users/userName1` }],
},
},
role: 'AUTHOR',
@ -112,7 +118,7 @@ function prMock(endpoint, projectKey, repositorySlug) {
slug: 'userName2',
type: 'NORMAL',
links: {
self: [{ href: `${endpoint}/users/userName2` }],
self: [{ href: `${endpointStr}/users/userName2` }],
},
},
role: 'REVIEWER',
@ -124,7 +130,7 @@ function prMock(endpoint, projectKey, repositorySlug) {
links: {
self: [
{
href: `${endpoint}/projects/${projectKey}/repos/${repositorySlug}/pull-requests/5`,
href: `${endpointStr}/projects/${projectKey}/repos/${repositorySlug}/pull-requests/5`,
},
],
},

View file

@ -1,7 +1,7 @@
// SEE for the reference https://github.com/renovatebot/renovate/blob/c3e9e572b225085448d94aa121c7ec81c14d3955/lib/platform/bitbucket/utils.js
import url from 'url';
import { PrState } from '../../types';
import { HttpResponse } from '../../util/http';
import { HttpOptions, HttpPostOptions, HttpResponse } from '../../util/http';
import { BitbucketServerHttp } from '../../util/http/bitbucket-server';
import { BbbsRestPr, BbsPr } from './types';
@ -39,20 +39,29 @@ const addMaxLength = (inputUrl: string, limit = 100): string => {
function callApi<T>(
apiUrl: string,
method: string,
options?: any
options?: HttpOptions | HttpPostOptions
): Promise<HttpResponse<T>> {
/* istanbul ignore next */
switch (method.toLowerCase()) {
case 'post':
return bitbucketServerHttp.postJson<T>(apiUrl, options);
return bitbucketServerHttp.postJson<T>(
apiUrl,
options as HttpPostOptions
);
case 'put':
return bitbucketServerHttp.putJson<T>(apiUrl, options);
return bitbucketServerHttp.putJson<T>(apiUrl, options as HttpPostOptions);
case 'patch':
return bitbucketServerHttp.patchJson<T>(apiUrl, options);
return bitbucketServerHttp.patchJson<T>(
apiUrl,
options as HttpPostOptions
);
case 'head':
return bitbucketServerHttp.headJson<T>(apiUrl, options);
case 'delete':
return bitbucketServerHttp.deleteJson<T>(apiUrl, options);
return bitbucketServerHttp.deleteJson<T>(
apiUrl,
options as HttpPostOptions
);
case 'get':
default:
return bitbucketServerHttp.getJson<T>(apiUrl, options);
@ -62,7 +71,7 @@ function callApi<T>(
export async function accumulateValues<T = any>(
reqUrl: string,
method = 'get',
options?: any,
options?: HttpOptions | HttpPostOptions,
limit?: number
): Promise<T[]> {
let accumulator: T[] = [];

View file

@ -33,6 +33,7 @@ import { smartTruncate } from '../utils/pr-body';
import { readOnlyIssueBody } from '../utils/read-only-issue-body';
import * as comments from './comments';
import * as utils from './utils';
import { PrResponse, RepoInfoBody } from './utils';
const bitbucketHttp = new BitbucketHttp();
@ -100,7 +101,11 @@ export async function initRepo({
let info: utils.RepoInfo;
try {
info = utils.repoInfoTransformer(
(await bitbucketHttp.getJson(`/2.0/repositories/${repository}`)).body
(
await bitbucketHttp.getJson<RepoInfoBody>(
`/2.0/repositories/${repository}`
)
).body
);
if (optimizeForDisabled) {
@ -230,22 +235,6 @@ async function isPrConflicted(prNo: number): Promise<boolean> {
return utils.isConflicted(parseDiff(diff));
}
interface PrResponse {
id: string;
state: string;
links: {
commits: {
href: string;
};
};
source: {
branch: {
name: string;
};
};
reviewers: Array<any>;
}
// Gets details for a PR
export async function getPr(prNo: number): Promise<Pr | null> {
const pr = (

View file

@ -1,6 +1,6 @@
import url from 'url';
import { BranchStatus, PrState } from '../../types';
import { HttpResponse } from '../../util/http';
import { HttpOptions, HttpPostOptions, HttpResponse } from '../../util/http';
import { BitbucketHttp } from '../../util/http/bitbucket';
import { Pr } from '../common';
@ -39,7 +39,14 @@ export interface BitbucketStatus {
state: BitbucketBranchState;
}
export function repoInfoTransformer(repoInfoBody: any): RepoInfo {
export interface RepoInfoBody {
parent?: any;
owner: { username: string };
mainbranch: { name: string };
has_issues: boolean;
}
export function repoInfoTransformer(repoInfoBody: RepoInfoBody): RepoInfo {
return {
isFork: !!repoInfoBody.parent,
owner: repoInfoBody.owner.username,
@ -75,20 +82,20 @@ const addMaxLength = (inputUrl: string, pagelen = 100): string => {
function callApi<T>(
apiUrl: string,
method: string,
options?: any
options?: HttpOptions | HttpPostOptions
): Promise<HttpResponse<T>> {
/* istanbul ignore next */
switch (method.toLowerCase()) {
case 'post':
return bitbucketHttp.postJson<T>(apiUrl, options);
return bitbucketHttp.postJson<T>(apiUrl, options as HttpPostOptions);
case 'put':
return bitbucketHttp.putJson<T>(apiUrl, options);
return bitbucketHttp.putJson<T>(apiUrl, options as HttpPostOptions);
case 'patch':
return bitbucketHttp.patchJson<T>(apiUrl, options);
return bitbucketHttp.patchJson<T>(apiUrl, options as HttpPostOptions);
case 'head':
return bitbucketHttp.headJson<T>(apiUrl, options);
case 'delete':
return bitbucketHttp.deleteJson<T>(apiUrl, options);
return bitbucketHttp.deleteJson<T>(apiUrl, options as HttpPostOptions);
case 'get':
default:
return bitbucketHttp.getJson<T>(apiUrl, options);
@ -98,7 +105,7 @@ function callApi<T>(
export async function accumulateValues<T = any>(
reqUrl: string,
method = 'get',
options?: any,
options?: HttpOptions | HttpPostOptions,
pagelen?: number
): Promise<T[]> {
let accumulator: T[] = [];
@ -117,7 +124,17 @@ export async function accumulateValues<T = any>(
return accumulator;
}
export /* istanbul ignore next */ function isConflicted(files: any): boolean {
interface Files {
chunks: {
changes: {
content: string;
}[];
}[];
}
export /* istanbul ignore next */ function isConflicted(
files: Files[]
): boolean {
for (const file of files) {
for (const chunk of file.chunks) {
for (const change of chunk.changes) {
@ -130,7 +147,31 @@ export /* istanbul ignore next */ function isConflicted(files: any): boolean {
return false;
}
export function prInfo(pr: any): Pr {
export interface PrResponse {
id: number;
title: string;
state: string;
links: {
commits: {
href: string;
};
};
summary?: { raw: string };
source: {
branch: {
name: string;
};
};
destination: {
branch: {
name: string;
};
};
reviewers: Array<any>;
created_on: string;
}
export function prInfo(pr: PrResponse): Pr {
return {
number: pr.id,
body: pr.summary ? pr.summary.raw : /* istanbul ignore next */ undefined,

View file

@ -677,12 +677,13 @@ describe('platform/gitea/gitea-helper', () => {
{ ...mockCommitStatus, status: 'unknown' },
];
for (const { status, created_at, expected } of statuses) {
for (const statusElem of statuses) {
const { status, expected } = statusElem;
// Add current status ot list of commit statuses, then mock the API to return the whole list
commitStatuses.push({
...mockCommitStatus,
status,
created_at,
created_at: statusElem.created_at,
});
httpMock
.scope(baseUrl)

View file

@ -98,7 +98,7 @@ export async function initPlatform(
);
gitAuthor = 'Renovate Bot <renovate@whitesourcesoftware.com>';
} /* istanbul ignore next */ else {
logger.debug('Using platform gitAuthor: ' + platformInfo.gitAuthor);
logger.debug(`Using platform gitAuthor: ${String(platformInfo.gitAuthor)}`);
gitAuthor = platformInfo.gitAuthor;
}
const gitAuthorParsed = parseGitAuthor(gitAuthor);