diff --git a/lib/platform/azure/index.ts b/lib/platform/azure/index.ts index 28af621be1b710ed6c189454a962819ed1d84489..8ad3fd9805c15cf73645e47f778659fe538d1427 100644 --- a/lib/platform/azure/index.ts +++ b/lib/platform/azure/index.ts @@ -4,7 +4,7 @@ import * as hostRules from '../../util/host-rules'; import { appSlug } from '../../config/app-strings'; import GitStorage from '../git/storage'; import { logger } from '../../logger'; -import { InitRepoConfig, PlatformConfig } from '../common'; +import { PlatformConfig, RepoParams, RepoConfig } from '../common'; interface Config { storage: GitStorage; @@ -46,7 +46,10 @@ export function initPlatform({ }; defaults.endpoint = res.endpoint; azureApi.setEndpoint(res.endpoint); - return res; + const platformConfig: PlatformConfig = { + endpoint: defaults.endpoint, + }; + return platformConfig; } export async function getRepos() { @@ -61,7 +64,7 @@ export async function initRepo({ localDir, azureWorkItemId, optimizeForDisabled, -}: InitRepoConfig) { +}: RepoParams) { logger.debug(`initRepo("${repository}")`); config = { repository, azureWorkItemId } as any; const azureApiGit = await azureApi.gitApi(); @@ -118,11 +121,11 @@ export async function initRepo({ localDir, url, }); - const platformConfig: PlatformConfig = { + const repoConfig: RepoConfig = { privateRepo: true, isFork: false, }; - return platformConfig; + return repoConfig; } export function getRepoForceRebase() { diff --git a/lib/platform/bitbucket-server/index.ts b/lib/platform/bitbucket-server/index.ts index 60317ff8dccb18f2b0e0c3badd7fe28f91b7225c..9bf3b8249a3027838a3a38fc785a69e93e36fad5 100644 --- a/lib/platform/bitbucket-server/index.ts +++ b/lib/platform/bitbucket-server/index.ts @@ -6,7 +6,7 @@ import * as utils from './utils'; import * as hostRules from '../../util/host-rules'; import GitStorage from '../git/storage'; import { logger } from '../../logger'; -import { InitRepoConfig, PlatformConfig } from '../common'; +import { PlatformConfig, RepoParams, RepoConfig } from '../common'; /* * Version: 5.3 (EOL Date: 15 Aug 2019) @@ -67,12 +67,12 @@ export function initPlatform({ ); } // TODO: Add a connection check that endpoint/username/password combination are valid - const res = { - endpoint: endpoint.replace(/\/?$/, '/'), // always add a trailing slash + defaults.endpoint = endpoint.replace(/\/?$/, '/'); // always add a trailing slash + api.setBaseUrl(defaults.endpoint); + const platformConfig: PlatformConfig = { + endpoint: defaults.endpoint, }; - api.setBaseUrl(res.endpoint); - defaults.endpoint = res.endpoint; - return res; + return platformConfig; } // Get all repositories that the user has access to @@ -109,7 +109,7 @@ export async function initRepo({ localDir, optimizeForDisabled, bbUseDefaultReviewers, -}: InitRepoConfig) { +}: RepoParams) { logger.debug( `initRepo("${JSON.stringify({ repository, localDir }, null, 2)}")` ); @@ -180,15 +180,14 @@ export async function initRepo({ url: gitUrl, }); - const platformConfig: PlatformConfig = {} as any; + const repoConfig: RepoConfig = {} as any; try { const info = (await api.get( `./rest/api/1.0/projects/${config.projectKey}/repos/${config.repositorySlug}` )).body; - platformConfig.privateRepo = info.is_private; - platformConfig.isFork = !!info.parent; - platformConfig.repoFullName = info.name; + repoConfig.privateRepo = info.is_private; + repoConfig.isFork = !!info.parent; config.owner = info.project.key; logger.debug(`${repository} owner = ${config.owner}`); config.defaultBranch = (await api.get( @@ -205,10 +204,10 @@ export async function initRepo({ throw err; } logger.debug( - { platformConfig }, - `platformConfig for ${config.projectKey}/${config.repositorySlug}` + { repoConfig }, + `repoConfig for ${config.projectKey}/${config.repositorySlug}` ); - return platformConfig; + return repoConfig; } export function getRepoForceRebase() { diff --git a/lib/platform/bitbucket/index.ts b/lib/platform/bitbucket/index.ts index fdba82bddec6227bfe0a122e37fd4c4046b61906..48502dd9ff8c93fbb2e688c4bdbcdc24eb1faac6 100644 --- a/lib/platform/bitbucket/index.ts +++ b/lib/platform/bitbucket/index.ts @@ -8,7 +8,7 @@ import GitStorage from '../git/storage'; import { readOnlyIssueBody } from '../utils/read-only-issue-body'; import { appSlug } from '../../config/app-strings'; import * as comments from './comments'; -import { InitRepoConfig, PlatformConfig } from '../common'; +import { PlatformConfig, RepoParams, RepoConfig } from '../common'; let config: utils.Config = {} as any; @@ -32,11 +32,10 @@ export function initPlatform({ ); } // TODO: Add a connection check that endpoint/username/password combination are valid - const res = { + const platformConfig: PlatformConfig = { endpoint: 'https://api.bitbucket.org/', }; - logger.info('Using default Bitbucket Cloud endpoint: ' + res.endpoint); - return res; + return platformConfig; } // Get all repositories that the user has access to @@ -58,7 +57,7 @@ export async function initRepo({ repository, localDir, optimizeForDisabled, -}: InitRepoConfig) { +}: RepoParams) { logger.debug(`initRepo("${repository}")`); const opts = hostRules.find({ hostType: 'bitbucket', @@ -70,7 +69,7 @@ export async function initRepo({ } as any; // TODO: get in touch with @rarkins about lifting up the caching into the app layer - const platformConfig: PlatformConfig = {} as any; + const repoConfig: RepoConfig = {} as any; try { const info = utils.repoInfoTransformer( @@ -95,9 +94,8 @@ export async function initRepo({ } } - platformConfig.privateRepo = info.privateRepo; - platformConfig.isFork = info.isFork; - platformConfig.repoFullName = info.repoFullName; + repoConfig.privateRepo = info.privateRepo; + repoConfig.isFork = info.isFork; Object.assign(config, { owner: info.owner, @@ -131,7 +129,7 @@ export async function initRepo({ }); await Promise.all([getPrList(), getFileList()]); - return platformConfig; + return repoConfig; } // Returns true if repository has rule enforcing PRs are up-to-date with base branch before merging diff --git a/lib/platform/bitbucket/utils.ts b/lib/platform/bitbucket/utils.ts index 4b087591b50273701adc02c50a8ab9a6a9288690..710164f66c6a5f10546d863185978d39c8abd6d8 100644 --- a/lib/platform/bitbucket/utils.ts +++ b/lib/platform/bitbucket/utils.ts @@ -29,7 +29,6 @@ export function repoInfoTransformer(repoInfoBody: any) { return { privateRepo: repoInfoBody.is_private, isFork: !!repoInfoBody.parent, - repoFullName: repoInfoBody.full_name, owner: repoInfoBody.owner.username, mainbranch: repoInfoBody.mainbranch.name, mergeMethod: 'merge', diff --git a/lib/platform/common.ts b/lib/platform/common.ts index 714f6d4f10f59626b9f17965c758059b90ed9919..3deb3174f0d943dae63793c113419f4461bd749b 100644 --- a/lib/platform/common.ts +++ b/lib/platform/common.ts @@ -40,20 +40,29 @@ export interface GotApi<TOptions extends object = any> { } export interface PlatformConfig { + endpoint: string; + renovateUsername?: any; + gitAuthor?: any; +} + +export interface RepoConfig { endpoint?: string; renovateUsername?: any; gitAuthor?: any; isFork: boolean; privateRepo: boolean; - // do we need this? - repoFullName?: string; } -export interface InitRepoConfig { - azureWorkItemId?: number; - bbUseDefaultReviewers?: boolean; +export interface RepoParams { + azureWorkItemId?: number; // shouldn't this be configurable within a renovate.json? + bbUseDefaultReviewers?: boolean; // shouldn't this be configurable within a renovate.json? gitPrivateKey?: string; localDir: string; - optimizeForDisabled?: boolean; + optimizeForDisabled: boolean; repository: string; + endpoint?: string; + forkMode?: string; + forkToken?: string; + includeForks?: boolean; + renovateUsername?: string; } diff --git a/lib/platform/github/index.ts b/lib/platform/github/index.ts index 1fd5ee06b80c4144dd9c06d555782a0f2f60d293..af9ceb0c3c645bd8134c07e65fb0504c236cc069 100644 --- a/lib/platform/github/index.ts +++ b/lib/platform/github/index.ts @@ -7,7 +7,7 @@ import { logger } from '../../logger'; import { api } from './gh-got-wrapper'; import * as hostRules from '../../util/host-rules'; import GitStorage from '../git/storage'; -import { PlatformConfig } from '../common'; +import { PlatformConfig, RepoParams, RepoConfig } from '../common'; import { appName, @@ -39,7 +39,7 @@ interface Pr { canRebase: boolean; } -interface InitRepoConfig { +interface LocalRepoConfig { repositoryName: string; pushProtection: boolean; prReviewsRequired: boolean; @@ -64,7 +64,7 @@ interface InitRepoConfig { renovateUsername: string; } -let config: InitRepoConfig = {} as any; +let config: LocalRepoConfig = {} as any; const defaults = { hostType: 'github', @@ -82,42 +82,47 @@ export async function initPlatform({ throw new Error('Init: You must configure a GitHub personal access token'); } - const res: PlatformConfig = {} as any; if (endpoint) { defaults.endpoint = endpoint.replace(/\/?$/, '/'); // always add a trailing slash api.setBaseUrl(defaults.endpoint); } else { logger.info('Using default github endpoint: ' + defaults.endpoint); } - res.endpoint = defaults.endpoint; + let gitAuthor: string; + let renovateUsername: string; try { - const userData = (await api.get(res.endpoint + 'user', { + const userData = (await api.get(defaults.endpoint + 'user', { token, })).body; - res.renovateUsername = userData.login; - res.gitAuthor = userData.name; + renovateUsername = userData.login; + gitAuthor = userData.name; } catch (err) { logger.debug({ err }, 'Error authenticating with GitHub'); throw new Error('Init: Authentication failure'); } try { - const userEmail = (await api.get(res.endpoint + 'user/emails', { + const userEmail = (await api.get(defaults.endpoint + 'user/emails', { token, })).body; if (userEmail.length && userEmail[0].email) { - res.gitAuthor += ` <${userEmail[0].email}>`; + gitAuthor += ` <${userEmail[0].email}>`; } else { logger.debug('Cannot find an email address for Renovate user'); - delete res.gitAuthor; + gitAuthor = undefined; } } catch (err) { logger.debug( 'Cannot read user/emails endpoint on GitHub to retrieve gitAuthor' ); - delete res.gitAuthor; + gitAuthor = undefined; } - logger.info('Authenticated as GitHub user: ' + res.renovateUsername); - return res; + logger.info('Authenticated as GitHub user: ' + renovateUsername); + const platformConfig: PlatformConfig = { + endpoint: defaults.endpoint, + gitAuthor, + renovateUsername, + }; + return platformConfig; } // Get all repositories that the user has access to @@ -152,17 +157,7 @@ export async function initRepo({ includeForks, renovateUsername, optimizeForDisabled, -}: { - endpoint: string; - repository: string; - forkMode?: boolean; - forkToken?: string; - gitPrivateKey?: string; - localDir: string; - includeForks: boolean; - renovateUsername: string; - optimizeForDisabled: boolean; -}) { +}: RepoParams) { logger.debug(`initRepo("${repository}")`); logger.info('Authenticated as user: ' + renovateUsername); logger.info('Using renovate version: ' + global.renovateVersion); @@ -185,8 +180,8 @@ export async function initRepo({ config.repository = repository; [config.repositoryOwner, config.repositoryName] = repository.split('/'); config.gitPrivateKey = gitPrivateKey; - // platformConfig is passed back to the app layer and contains info about the platform they require - const platformConfig: PlatformConfig = {} as any; + // repoConfig is passed back to the app layer and contains info about the platform they require + const repoConfig: RepoConfig = {} as any; let res; try { res = await api.get(`repos/${repository}`); @@ -242,8 +237,8 @@ export async function initRepo({ throw new Error('disabled'); } } - platformConfig.privateRepo = res.body.private === true; - platformConfig.isFork = res.body.fork === true; + repoConfig.privateRepo = res.body.private === true; + repoConfig.isFork = res.body.fork === true; const owner = res.body.owner.login; logger.debug(`${repository} owner = ${owner}`); // Use default branch as PR target unless later overridden. @@ -383,7 +378,7 @@ export async function initRepo({ url, }); - return platformConfig; + return repoConfig; } export async function getRepoForceRebase() { diff --git a/lib/platform/gitlab/index.ts b/lib/platform/gitlab/index.ts index fc572b2bb1083ea2e2cc1d86db96df470d91af07..4139d264d52646272504a9489a3cba4467e4cd1c 100644 --- a/lib/platform/gitlab/index.ts +++ b/lib/platform/gitlab/index.ts @@ -4,7 +4,7 @@ import is from '@sindresorhus/is'; import { api } from './gl-got-wrapper'; import * as hostRules from '../../util/host-rules'; import GitStorage from '../git/storage'; -import { PlatformConfig } from '../common'; +import { PlatformConfig, RepoParams, RepoConfig } from '../common'; import { configFileNames } from '../../config/app-strings'; import { logger } from '../../logger'; @@ -38,18 +38,16 @@ export async function initPlatform({ if (!token) { throw new Error('Init: You must configure a GitLab personal access token'); } - const res = {} as any; if (endpoint) { - res.endpoint = endpoint.replace(/\/?$/, '/'); // always add a trailing slash - api.setBaseUrl(res.endpoint); - defaults.endpoint = res.endpoint; + defaults.endpoint = endpoint.replace(/\/?$/, '/'); // always add a trailing slash + api.setBaseUrl(defaults.endpoint); } else { - res.endpoint = defaults.endpoint; - logger.info('Using default GitLab endpoint: ' + res.endpoint); + logger.info('Using default GitLab endpoint: ' + defaults.endpoint); } + let gitAuthor: string; try { const user = (await api.get(`user`, { token })).body; - res.gitAuthor = user.email; + gitAuthor = user.email; authorId = user.id; } catch (err) { logger.info( @@ -58,7 +56,11 @@ export async function initPlatform({ ); throw new Error('Init: Authentication failure'); } - return res; + const platformConfig: PlatformConfig = { + endpoint: defaults.endpoint, + gitAuthor, + }; + return platformConfig; } // Get all repositories that the user has access to @@ -95,16 +97,12 @@ export async function initRepo({ repository, localDir, optimizeForDisabled, -}: { - repository: string; - localDir: string; - optimizeForDisabled: boolean; -}) { +}: RepoParams) { config = {} as any; config.repository = urlEscape(repository); config.localDir = localDir; let res; - const platformConfig: PlatformConfig = {} as any; + const repoConfig: RepoConfig = {} as any; try { res = await api.get(`projects/${config.repository}`); if (res.body.archived) { @@ -142,7 +140,7 @@ export async function initRepo({ } config.defaultBranch = res.body.default_branch; config.baseBranch = config.defaultBranch; - platformConfig.isFork = !!res.body.forked_from_project; + repoConfig.isFork = !!res.body.forked_from_project; logger.debug(`${repository} default branch = ${config.baseBranch}`); // Discover our user email config.email = (await api.get(`user`)).body.email; @@ -194,7 +192,7 @@ export async function initRepo({ logger.info({ err }, 'Unknown GitLab initRepo error'); throw err; } - return platformConfig; + return repoConfig; } export function getRepoForceRebase() { diff --git a/lib/platform/index.ts b/lib/platform/index.ts index 485b6554be7b39391d69bf2032a1aabc339aa17a..544e4f206f9221308b4f1ba22d4329750e354fba 100644 --- a/lib/platform/index.ts +++ b/lib/platform/index.ts @@ -30,7 +30,7 @@ export async function initPlatform(config: any) { // TODO: types const platformInfo: any = await global.platform.initPlatform(config); const returnConfig = { ...config, ...platformInfo }; - let gitAuthor; + let gitAuthor: string; if (config && config.gitAuthor) { logger.info(`Using configured gitAuthor (${config.gitAuthor})`); gitAuthor = config.gitAuthor; diff --git a/test/platform/azure/index.spec.ts b/test/platform/azure/index.spec.ts index 47d4c1f5b1c2959c38f8357f3081716540bcad21..7c7ad7c8f1d9a31633aba9a5e015a582d4022ba7 100644 --- a/test/platform/azure/index.spec.ts +++ b/test/platform/azure/index.spec.ts @@ -1,6 +1,6 @@ import is from '@sindresorhus/is'; import * as _hostRules from '../../../lib/util/host-rules'; -import { InitRepoConfig } from '../../../lib/platform/common'; +import { RepoParams } from '../../../lib/platform/common'; describe('platform/azure', () => { let hostRules: jest.Mocked<typeof _hostRules>; @@ -124,7 +124,7 @@ describe('platform/azure', () => { azure.cleanRepo(); }); }); - function initRepo(args?: Partial<InitRepoConfig> | string) { + function initRepo(args?: Partial<RepoParams> | string) { azureApi.gitApi.mockImplementationOnce( () => ({ diff --git a/test/platform/bitbucket-server/__snapshots__/index.spec.ts.snap b/test/platform/bitbucket-server/__snapshots__/index.spec.ts.snap index 06a44efa2da87a9f7a6792d935176b506b2f5866..33177c798ea6919ce9dad0589f15980b14daade8 100644 --- a/test/platform/bitbucket-server/__snapshots__/index.spec.ts.snap +++ b/test/platform/bitbucket-server/__snapshots__/index.spec.ts.snap @@ -986,7 +986,6 @@ exports[`platform/bitbucket-server endpoint with no path initRepo() does not thr Object { "isFork": false, "privateRepo": undefined, - "repoFullName": "repo", } `; @@ -994,7 +993,6 @@ exports[`platform/bitbucket-server endpoint with no path initRepo() works 1`] = Object { "isFork": false, "privateRepo": undefined, - "repoFullName": "repo", } `; @@ -2448,7 +2446,6 @@ exports[`platform/bitbucket-server endpoint with path initRepo() does not throw Object { "isFork": false, "privateRepo": undefined, - "repoFullName": "repo", } `; @@ -2456,7 +2453,6 @@ exports[`platform/bitbucket-server endpoint with path initRepo() works 1`] = ` Object { "isFork": false, "privateRepo": undefined, - "repoFullName": "repo", } `; diff --git a/test/platform/bitbucket-server/index.spec.ts b/test/platform/bitbucket-server/index.spec.ts index f4bd31b0e0869d48d6c6218b982300d3612dc293..7935bc6edf29fe54baf3041586546a04d4567055 100644 --- a/test/platform/bitbucket-server/index.spec.ts +++ b/test/platform/bitbucket-server/index.spec.ts @@ -1,5 +1,5 @@ import responses from './_fixtures/responses'; -import { GotApi, InitRepoConfig } from '../../../lib/platform/common'; +import { GotApi, RepoParams } from '../../../lib/platform/common'; import { Storage } from '../../../lib/platform/git/storage'; type BbsApi = typeof import('../../../lib/platform/bitbucket-server'); @@ -83,7 +83,7 @@ describe('platform/bitbucket-server', () => { bitbucket.cleanRepo(); }); - function initRepo(config?: Partial<InitRepoConfig>) { + function initRepo(config?: Partial<RepoParams>) { return bitbucket.initRepo({ endpoint: 'https://stash.renovatebot.com/vcs/', repository: 'SOME/repo', diff --git a/test/platform/bitbucket/__snapshots__/index.spec.ts.snap b/test/platform/bitbucket/__snapshots__/index.spec.ts.snap index 1e20083001e74612a80290e2a62161e2b3970691..6e3c4958fa3f517cef229a892aeb28d7ac07e102 100644 --- a/test/platform/bitbucket/__snapshots__/index.spec.ts.snap +++ b/test/platform/bitbucket/__snapshots__/index.spec.ts.snap @@ -281,7 +281,6 @@ exports[`platform/bitbucket initRepo() works 1`] = ` Object { "isFork": false, "privateRepo": false, - "repoFullName": "some/repo", } `; diff --git a/test/platform/bitbucket/index.spec.ts b/test/platform/bitbucket/index.spec.ts index 8f67a8b0b72f8d74dd5975e98a20060f00840dab..a097eeaf568174c02be7563e9ecc61894d481282 100644 --- a/test/platform/bitbucket/index.spec.ts +++ b/test/platform/bitbucket/index.spec.ts @@ -1,6 +1,6 @@ import URL from 'url'; import responses from './_fixtures/responses'; -import { GotApi, InitRepoConfig } from '../../../lib/platform/common'; +import { GotApi, RepoParams } from '../../../lib/platform/common'; describe('platform/bitbucket', () => { let bitbucket: typeof import('../../../lib/platform/bitbucket'); @@ -71,11 +71,12 @@ describe('platform/bitbucket', () => { return (...args: any) => mocked(() => (bitbucket as any)[prop](...args)); } - function initRepo(config?: Partial<InitRepoConfig>) { + function initRepo(config?: Partial<RepoParams>) { return mocked(() => bitbucket.initRepo({ repository: 'some/repo', localDir: '', + optimizeForDisabled: false, ...config, }) ); @@ -289,6 +290,7 @@ describe('platform/bitbucket', () => { await bitbucket.initRepo({ repository: 'some/empty', localDir: '', + optimizeForDisabled: false, }); expect(await bitbucket.findIssue('title')).toBeNull(); }); @@ -308,6 +310,7 @@ describe('platform/bitbucket', () => { await bitbucket.initRepo({ repository: 'some/empty', localDir: '', + optimizeForDisabled: false, }); await bitbucket.ensureIssue('title', 'body'); expect(api.get.mock.calls).toMatchSnapshot(); diff --git a/test/platform/github/__snapshots__/index.spec.ts.snap b/test/platform/github/__snapshots__/index.spec.ts.snap index b5b5875919c9b01e59e4253ea508f08169a6ab2d..d1efba10c72c090f8747ab881bf388815bec8fc1 100644 --- a/test/platform/github/__snapshots__/index.spec.ts.snap +++ b/test/platform/github/__snapshots__/index.spec.ts.snap @@ -383,6 +383,7 @@ Array [ exports[`platform/github initPlatform() should support custom endpoint 1`] = ` Object { "endpoint": "https://ghe.renovatebot.com/", + "gitAuthor": undefined, "renovateUsername": "renovate-bot", } `; @@ -390,6 +391,7 @@ Object { exports[`platform/github initPlatform() should support default endpoint no email access 1`] = ` Object { "endpoint": "https://api.github.com/", + "gitAuthor": undefined, "renovateUsername": "renovate-bot", } `; @@ -397,6 +399,7 @@ Object { exports[`platform/github initPlatform() should support default endpoint no email result 1`] = ` Object { "endpoint": "https://api.github.com/", + "gitAuthor": undefined, "renovateUsername": "renovate-bot", } `;