Skip to content

Commit

Permalink
refactor(platform): move optimizeForDisabled, isFork logic to worker …
Browse files Browse the repository at this point in the history
…layer (#7379)

Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
Co-authored-by: Rhys Arkins <rhys@arkins.net>
  • Loading branch information
3 people committed Oct 2, 2020
1 parent 765786a commit 517f5ec
Show file tree
Hide file tree
Showing 19 changed files with 686 additions and 400 deletions.
28 changes: 19 additions & 9 deletions lib/platform/azure/index.spec.ts
@@ -1,6 +1,5 @@
import is from '@sindresorhus/is';
import { PullRequestStatus } from 'azure-devops-node-api/interfaces/GitInterfaces';
import { REPOSITORY_DISABLED } from '../../constants/error-messages';
import { BranchStatus, PrState } from '../../types';
import * as _git from '../../util/git';
import * as _hostRules from '../../util/host-rules';
Expand Down Expand Up @@ -162,14 +161,6 @@ describe('platform/azure', () => {
expect(azureApi.gitApi.mock.calls).toMatchSnapshot();
expect(config).toMatchSnapshot();
});

it('throws disabled', async () => {
expect.assertions(1);
azureHelper.getFile.mockResolvedValueOnce('{ "enabled": false }');
await expect(
initRepo({ repository: 'some-repo', optimizeForDisabled: true })
).rejects.toThrow(REPOSITORY_DISABLED);
});
});

describe('getRepoForceRebase', () => {
Expand Down Expand Up @@ -816,4 +807,23 @@ describe('platform/azure', () => {
expect(azureApi.gitApi.mock.calls).toMatchSnapshot();
});
});
describe('getJsonFile()', () => {
it('returns file content', async () => {
const data = { foo: 'bar' };
azureHelper.getFile.mockResolvedValueOnce(JSON.stringify(data));
await initRepo({
repository: 'some-repo',
});
const res = await azure.getJsonFile('file.json');
expect(res).toEqual(data);
});
it('returns null on errors', async () => {
azureHelper.getFile.mockRejectedValueOnce('some error');
await initRepo({
repository: 'some-repo',
});
const res = await azure.getJsonFile('file.json');
expect(res).toBeNull();
});
});
});
19 changes: 2 additions & 17 deletions lib/platform/azure/index.ts
Expand Up @@ -5,10 +5,7 @@ import {
GitPullRequestMergeStrategy,
PullRequestStatus,
} from 'azure-devops-node-api/interfaces/GitInterfaces';
import {
REPOSITORY_DISABLED,
REPOSITORY_EMPTY,
} from '../../constants/error-messages';
import { REPOSITORY_EMPTY } from '../../constants/error-messages';
import { PLATFORM_TYPE_AZURE } from '../../constants/platforms';
import { logger } from '../../logger';
import { BranchStatus, PrState } from '../../types';
Expand Down Expand Up @@ -109,15 +106,14 @@ export async function getJsonFile(fileName: string): Promise<any | null> {
config.defaultBranch
);
return JSON.parse(json);
} catch (err) /* istanbul ignore next */ {
} catch (err) {
return null;
}
}

export async function initRepo({
repository,
localDir,
optimizeForDisabled,
}: RepoParams): Promise<RepoResult> {
logger.debug(`initRepo("${repository}")`);
config = { repository } as Config;
Expand Down Expand Up @@ -145,17 +141,6 @@ export async function initRepo({
config.mergeMethod = await azureHelper.getMergeMethod(repo.id, names.project);
config.repoForceRebase = false;

if (optimizeForDisabled) {
interface RenovateConfig {
enabled: boolean;
}

const renovateConfig: RenovateConfig = await getJsonFile('renovate.json');
if (renovateConfig && renovateConfig.enabled === false) {
throw new Error(REPOSITORY_DISABLED);
}
}

const [projectName, repoName] = repository.split('/');
const opts = hostRules.find({
hostType: defaults.hostType,
Expand Down

0 comments on commit 517f5ec

Please sign in to comment.