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

refactor(platform): Make git to be class-less #6635

Merged
merged 10 commits into from Jul 1, 2020
2 changes: 1 addition & 1 deletion lib/manager/composer/artifacts.spec.ts
Expand Up @@ -2,11 +2,11 @@ import { exec as _exec } from 'child_process';
import { join } from 'upath';
import { envMock, mockExecAll } from '../../../test/execUtil';
import { mocked, platform } from '../../../test/util';
import { StatusResult } from '../../platform/git';
import { setUtilConfig } from '../../util';
import { BinarySource } from '../../util/exec/common';
import * as docker from '../../util/exec/docker';
import * as _env from '../../util/exec/env';
import { StatusResult } from '../../util/gitfs';
import * as _gitfs from '../../util/gitfs';
import * as composer from './artifacts';

Expand Down
2 changes: 1 addition & 1 deletion lib/manager/gomod/artifacts.spec.ts
Expand Up @@ -3,11 +3,11 @@ import _fs from 'fs-extra';
import { join } from 'upath';
import { envMock, mockExecAll } from '../../../test/execUtil';
import { mocked, platform } from '../../../test/util';
import { StatusResult } from '../../platform/git';
import { setUtilConfig } from '../../util';
import { BinarySource } from '../../util/exec/common';
import * as docker from '../../util/exec/docker';
import * as _env from '../../util/exec/env';
import { StatusResult } from '../../util/gitfs';
import * as _hostRules from '../../util/host-rules';
import * as gomod from './artifacts';

Expand Down
2 changes: 1 addition & 1 deletion lib/manager/pipenv/artifacts.spec.ts
Expand Up @@ -3,11 +3,11 @@ import _fs from 'fs-extra';
import { join } from 'upath';
import { envMock, mockExecAll } from '../../../test/execUtil';
import { mocked, platform } from '../../../test/util';
import { StatusResult } from '../../platform/git';
import { setUtilConfig } from '../../util';
import { BinarySource } from '../../util/exec/common';
import * as docker from '../../util/exec/docker';
import * as _env from '../../util/exec/env';
import { StatusResult } from '../../util/gitfs';
import * as pipenv from './artifacts';

jest.mock('fs-extra');
Expand Down
25 changes: 6 additions & 19 deletions lib/platform/azure/index.spec.ts
@@ -1,6 +1,7 @@
import is from '@sindresorhus/is';
import { REPOSITORY_DISABLED } from '../../constants/error-messages';
import { BranchStatus } from '../../types';
import * as _gitfs from '../../util/gitfs';
import * as _hostRules from '../../util/host-rules';
import { Platform, RepoParams } from '../common';

Expand All @@ -9,36 +10,22 @@ describe('platform/azure', () => {
let azure: Platform;
let azureApi: jest.Mocked<typeof import('./azure-got-wrapper')>;
let azureHelper: jest.Mocked<typeof import('./azure-helper')>;
let GitStorage;
let gitfs: jest.Mocked<typeof _gitfs>;
beforeEach(async () => {
// reset module
jest.resetModules();
jest.mock('./azure-got-wrapper');
jest.mock('./azure-helper');
jest.mock('../git');
jest.mock('../../util/gitfs');
jest.mock('../../util/host-rules');
hostRules = require('../../util/host-rules');
require('../../util/sanitize').sanitize = jest.fn((input) => input);
azure = await import('.');
azureApi = require('./azure-got-wrapper');
azureHelper = require('./azure-helper');
GitStorage = require('../git').Storage;
GitStorage.mockImplementation(() => ({
initRepo: jest.fn(),
cleanRepo: jest.fn(),
getFileList: jest.fn(),
branchExists: jest.fn(() => true),
isBranchStale: jest.fn(() => false),
setBaseBranch: jest.fn(),
getBranchLastCommitTime: jest.fn(),
getAllRenovateBranches: jest.fn(),
getCommitMessages: jest.fn(),
getFile: jest.fn(),
commitFiles: jest.fn(),
mergeBranch: jest.fn(),
deleteBranch: jest.fn(),
getRepoStatus: jest.fn(),
}));
gitfs = require('../../util/gitfs');
gitfs.branchExists.mockResolvedValue(true);
gitfs.isBranchStale.mockResolvedValue(false);
hostRules.find.mockReturnValue({
token: 'token',
});
Expand Down
42 changes: 20 additions & 22 deletions lib/platform/azure/index.ts
Expand Up @@ -13,6 +13,7 @@ import {
} from '../../constants/pull-requests';
import { logger } from '../../logger';
import { BranchStatus } from '../../types';
import * as gitfs from '../../util/gitfs';
import * as hostRules from '../../util/host-rules';
import { sanitize } from '../../util/sanitize';
import { ensureTrailingSlash } from '../../util/url';
Expand All @@ -31,14 +32,12 @@ import {
RepoParams,
VulnerabilityAlert,
} from '../common';
import GitStorage, { StatusResult } from '../git';
import { smartTruncate } from '../utils/pr-body';
import * as azureApi from './azure-got-wrapper';
import * as azureHelper from './azure-helper';
import { AzurePr } from './types';

interface Config {
storage: GitStorage;
repoForceRebase: boolean;
mergeMethod: GitPullRequestMergeStrategy;
baseCommitSHA: string | undefined;
Expand Down Expand Up @@ -139,6 +138,7 @@ export async function initRepo({
interface RenovateConfig {
enabled: boolean;
}

let renovateConfig: RenovateConfig;
try {
const json = await azureHelper.getFile(
Expand All @@ -155,7 +155,6 @@ export async function initRepo({
}
}

config.storage = new GitStorage();
const [projectName, repoName] = repository.split('/');
const opts = hostRules.find({
hostType: defaults.hostType,
Expand All @@ -164,7 +163,7 @@ export async function initRepo({
const url =
defaults.endpoint +
`${encodeURIComponent(projectName)}/_git/${encodeURIComponent(repoName)}`;
await config.storage.initRepo({
await gitfs.initRepo({
...config,
localDir,
url,
Expand All @@ -186,7 +185,7 @@ export function getRepoForceRebase(): Promise<boolean> {
// Search

export /* istanbul ignore next */ function getFileList(): Promise<string[]> {
return config.storage.getFileList();
return gitfs.getFileList();
}

export /* istanbul ignore next */ async function setBaseBranch(
Expand All @@ -195,41 +194,41 @@ export /* istanbul ignore next */ async function setBaseBranch(
logger.debug(`Setting baseBranch to ${branchName}`);
config.baseBranch = branchName;
delete config.baseCommitSHA;
const baseBranchSha = await config.storage.setBaseBranch(branchName);
const baseBranchSha = await gitfs.setBaseBranch(branchName);
return baseBranchSha;
}

export /* istanbul ignore next */ function setBranchPrefix(
branchPrefix: string
): Promise<void> {
return config.storage.setBranchPrefix(branchPrefix);
return gitfs.setBranchPrefix(branchPrefix);
}

// Branch

export /* istanbul ignore next */ function branchExists(
branchName: string
): Promise<boolean> {
return config.storage.branchExists(branchName);
return gitfs.branchExists(branchName);
}

export /* istanbul ignore next */ function getAllRenovateBranches(
branchPrefix: string
): Promise<string[]> {
return config.storage.getAllRenovateBranches(branchPrefix);
return gitfs.getAllRenovateBranches(branchPrefix);
}

export /* istanbul ignore next */ function isBranchStale(
branchName: string
): Promise<boolean> {
return config.storage.isBranchStale(branchName);
return gitfs.isBranchStale(branchName);
}

export /* istanbul ignore next */ function getFile(
filePath: string,
branchName: string
): Promise<string> {
return config.storage.getFile(filePath, branchName);
return gitfs.getFile(filePath, branchName);
}

// istanbul ignore next
Expand Down Expand Up @@ -273,7 +272,7 @@ export async function getPrList(): Promise<AzurePr[]> {

/* istanbul ignore next */
export async function getPrFiles(pr: Pr): Promise<string[]> {
return config.storage.getBranchFiles(pr.branchName, pr.targetBranch);
return gitfs.getBranchFiles(pr.branchName, pr.targetBranch);
}

export async function getPr(pullRequestId: number): Promise<Pr | null> {
Expand Down Expand Up @@ -309,6 +308,7 @@ export async function getPr(pullRequestId: number): Promise<Pr | null> {

return azurePr;
}

export async function findPr({
branchName,
prTitle,
Expand Down Expand Up @@ -361,7 +361,7 @@ export /* istanbul ignore next */ async function deleteBranch(
branchName: string,
abandonAssociatedPr = false
): Promise<void> {
await config.storage.deleteBranch(branchName);
await gitfs.deleteBranch(branchName);
if (abandonAssociatedPr) {
const pr = await getBranchPr(branchName);
await abandonPr(pr.number);
Expand All @@ -371,27 +371,27 @@ export /* istanbul ignore next */ async function deleteBranch(
export /* istanbul ignore next */ function getBranchLastCommitTime(
branchName: string
): Promise<Date> {
return config.storage.getBranchLastCommitTime(branchName);
return gitfs.getBranchLastCommitTime(branchName);
}

export /* istanbul ignore next */ function getRepoStatus(): Promise<
StatusResult
gitfs.StatusResult
> {
return config.storage.getRepoStatus();
return gitfs.getRepoStatus();
}

export /* istanbul ignore next */ function mergeBranch(
branchName: string
): Promise<void> {
return config.storage.mergeBranch(branchName);
return gitfs.mergeBranch(branchName);
}

export /* istanbul ignore next */ function commitFiles({
branchName,
files,
message,
}: CommitFilesConfig): Promise<string | null> {
return config.storage.commitFiles({
return gitfs.commitFiles({
branchName,
files,
message,
Expand All @@ -401,7 +401,7 @@ export /* istanbul ignore next */ function commitFiles({
export /* istanbul ignore next */ function getCommitMessages(): Promise<
string[]
> {
return config.storage.getCommitMessages();
return gitfs.getCommitMessages();
}

export async function getBranchStatusCheck(
Expand Down Expand Up @@ -770,9 +770,7 @@ export function getVulnerabilityAlerts(): Promise<VulnerabilityAlert[]> {

export function cleanRepo(): Promise<void> {
// istanbul ignore if
rarkins marked this conversation as resolved.
Show resolved Hide resolved
if (config.storage && config.storage.cleanRepo) {
config.storage.cleanRepo();
}
gitfs.cleanRepo();
config = {} as any;
return Promise.resolve();
}
45 changes: 9 additions & 36 deletions lib/platform/bitbucket-server/index.spec.ts
Expand Up @@ -7,8 +7,8 @@ import {
} from '../../constants/error-messages';
import { PR_STATE_CLOSED, PR_STATE_OPEN } from '../../constants/pull-requests';
import { BranchStatus } from '../../types';
import * as _gitfs from '../../util/gitfs';
import { Platform } from '../common';
import { Storage } from '../git';

function repoMock(
endpoint: URL | string,
Expand Down Expand Up @@ -143,9 +143,7 @@ describe('platform/bitbucket-server', () => {
describe(scenarioName, () => {
let bitbucket: Platform;
let hostRules: jest.Mocked<typeof import('../../util/host-rules')>;
let GitStorage: jest.Mock<Storage> & {
getUrl: jest.MockInstance<any, any>;
};
let gitfs: jest.Mocked<typeof _gitfs>;

async function initRepo(config = {}): Promise<nock.Scope> {
const scope = httpMock
Expand Down Expand Up @@ -174,32 +172,15 @@ describe('platform/bitbucket-server', () => {
httpMock.reset();
httpMock.setup();
jest.mock('delay');
jest.mock('../git');
jest.mock('../../util/gitfs');
jest.mock('../../util/host-rules');
hostRules = require('../../util/host-rules');
bitbucket = await import('.');
GitStorage = require('../git').Storage;
GitStorage.mockImplementation(
() =>
({
initRepo: jest.fn(),
cleanRepo: jest.fn(),
getFileList: jest.fn(),
branchExists: jest.fn(() => true),
isBranchStale: jest.fn(() => false),
setBaseBranch: jest.fn(),
getBranchLastCommitTime: jest.fn(),
getAllRenovateBranches: jest.fn(),
getCommitMessages: jest.fn(),
getFile: jest.fn(),
commitFiles: jest.fn(),
mergeBranch: jest.fn(),
deleteBranch: jest.fn(),
getRepoStatus: jest.fn(),
getBranchCommit: jest.fn(
() => '0d9c7726c3d628b7e28af234595cfd20febdbf8e'
),
} as any)
gitfs = require('../../util/gitfs');
gitfs.branchExists.mockResolvedValue(true);
gitfs.isBranchStale.mockResolvedValue(false);
gitfs.getBranchCommit.mockResolvedValue(
'0d9c7726c3d628b7e28af234595cfd20febdbf8e'
);
const endpoint =
scenarioName === 'endpoint with path'
Expand Down Expand Up @@ -1807,15 +1788,7 @@ Followed by some information.
});

it('throws repository-changed', async () => {
GitStorage.mockImplementationOnce(
() =>
({
initRepo: jest.fn(),
branchExists: jest.fn(() => Promise.resolve(false)),
cleanRepo: jest.fn(),
} as any)
);

gitfs.branchExists.mockResolvedValue(false);
await initRepo();
await expect(
bitbucket.getBranchStatus('somebranch', [])
Expand Down