Skip to content

Commit

Permalink
refactor: dryRun admin config
Browse files Browse the repository at this point in the history
  • Loading branch information
rarkins committed Feb 5, 2021
1 parent 979e921 commit 6fa3729
Show file tree
Hide file tree
Showing 22 changed files with 87 additions and 59 deletions.
1 change: 1 addition & 0 deletions lib/config/admin.ts
Expand Up @@ -8,6 +8,7 @@ export const repoAdminOptions = [
'allowedPostUpgradeCommands',
'dockerImagePrefix',
'dockerUser',
'dryRun',
'privateKey',
'trustLevel',
];
Expand Down
3 changes: 1 addition & 2 deletions lib/config/common.ts
Expand Up @@ -86,6 +86,7 @@ export interface RepoAdminConfig {
allowedPostUpgradeCommands?: string[];
dockerImagePrefix?: string;
dockerUser?: string;
dryRun?: boolean;
privateKey?: string | Buffer;
trustLevel?: 'low' | 'high';
}
Expand All @@ -97,8 +98,6 @@ export interface RenovateAdminConfig {

customEnvVariables?: Record<string, string>;

dryRun?: boolean;

endpoint?: string;

localDir?: string;
Expand Down
4 changes: 3 additions & 1 deletion lib/workers/branch/automerge.spec.ts
@@ -1,5 +1,6 @@
import { defaultConfig, git, platform } from '../../../test/util';
import { RenovateConfig } from '../../config';
import { setAdminConfig } from '../../config/admin';
import { BranchStatus } from '../../types';
import { tryBranchAutomerge } from './automerge';

Expand All @@ -12,6 +13,7 @@ describe('workers/branch/automerge', () => {
config = {
...defaultConfig,
};
setAdminConfig();
});
it('returns false if not configured for automerge', async () => {
config.automerge = false;
Expand Down Expand Up @@ -61,7 +63,7 @@ describe('workers/branch/automerge', () => {
it('returns true if automerge succeeds (dry-run)', async () => {
config.automerge = true;
config.automergeType = 'branch';
config.dryRun = true;
setAdminConfig({ dryRun: true });
platform.getBranchStatus.mockResolvedValueOnce(BranchStatus.green);
expect(await tryBranchAutomerge(config)).toBe('automerged');
});
Expand Down
3 changes: 2 additions & 1 deletion lib/workers/branch/automerge.ts
@@ -1,4 +1,5 @@
import { RenovateConfig } from '../../config';
import { getAdminConfig } from '../../config/admin';
import { logger } from '../../logger';
import { platform } from '../../platform';
import { BranchStatus } from '../../types';
Expand Down Expand Up @@ -30,7 +31,7 @@ export async function tryBranchAutomerge(
if (branchStatus === BranchStatus.green) {
logger.debug(`Automerging branch`);
try {
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info('DRY-RUN: Would automerge branch' + config.branchName);
} else {
await mergeBranch(config.branchName);
Expand Down
4 changes: 3 additions & 1 deletion lib/workers/branch/commit.spec.ts
@@ -1,4 +1,5 @@
import { defaultConfig, git, partial } from '../../../test/util';
import { setAdminConfig } from '../../config/admin';
import { BranchConfig } from '../common';
import { commitFilesToBranch } from './commit';

Expand All @@ -20,6 +21,7 @@ describe('workers/branch/automerge', () => {
});
jest.resetAllMocks();
git.commitFiles.mockResolvedValueOnce('abc123');
setAdminConfig();
});
it('handles empty files', async () => {
await commitFilesToBranch(config);
Expand All @@ -35,7 +37,7 @@ describe('workers/branch/automerge', () => {
expect(git.commitFiles.mock.calls).toMatchSnapshot();
});
it('dry runs', async () => {
config.dryRun = true;
setAdminConfig({ dryRun: true });
config.updatedPackageFiles.push({
name: 'package.json',
contents: 'some contents',
Expand Down
3 changes: 2 additions & 1 deletion lib/workers/branch/commit.ts
@@ -1,5 +1,6 @@
import is from '@sindresorhus/is';
import minimatch from 'minimatch';
import { getAdminConfig } from '../../config/admin';
import { CONFIG_SECRETS_EXPOSED } from '../../constants/error-messages';
import { logger } from '../../logger';
import { commitFiles } from '../../util/git';
Expand Down Expand Up @@ -31,7 +32,7 @@ export function commitFilesToBranch(
const fileLength = [...new Set(updatedFiles.map((file) => file.name))].length;
logger.debug(`${fileLength} file(s) to commit`);
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info('DRY-RUN: Would commit files to branch ' + config.branchName);
return null;
}
Expand Down
22 changes: 12 additions & 10 deletions lib/workers/branch/index.spec.ts
Expand Up @@ -324,7 +324,8 @@ describe('workers/branch', () => {
git.branchExists.mockReturnValueOnce(true);
commit.commitFilesToBranch.mockResolvedValueOnce(null);
automerge.tryBranchAutomerge.mockResolvedValueOnce('automerged');
await branchWorker.processBranch({ ...config, dryRun: true });
setAdminConfig({ dryRun: true });
await branchWorker.processBranch(config);
expect(automerge.tryBranchAutomerge).toHaveBeenCalledTimes(1);
expect(prWorker.ensurePr).toHaveBeenCalledTimes(0);
});
Expand Down Expand Up @@ -553,9 +554,10 @@ describe('workers/branch', () => {
checkExisting.prAlreadyExisted.mockResolvedValueOnce({
state: PrState.Closed,
} as never);
expect(
await branchWorker.processBranch({ ...config, dryRun: true })
).toEqual(ProcessBranchResult.AlreadyExisted);
setAdminConfig({ dryRun: true });
expect(await branchWorker.processBranch(config)).toEqual(
ProcessBranchResult.AlreadyExisted
);
});

it('branch pr no rebase (dry run)', async () => {
Expand All @@ -564,9 +566,10 @@ describe('workers/branch', () => {
state: PrState.Open,
} as never);
git.isBranchModified.mockResolvedValueOnce(true);
expect(
await branchWorker.processBranch({ ...config, dryRun: true })
).toEqual(ProcessBranchResult.PrEdited);
setAdminConfig({ dryRun: true });
expect(await branchWorker.processBranch(config)).toEqual(
ProcessBranchResult.PrEdited
);
});

it('branch pr no schedule lockfile (dry run)', async () => {
Expand All @@ -587,11 +590,10 @@ describe('workers/branch', () => {
git.isBranchModified.mockResolvedValueOnce(true);
schedule.isScheduledNow.mockReturnValueOnce(false);
commit.commitFilesToBranch.mockResolvedValueOnce(null);

setAdminConfig({ dryRun: true });
expect(
await branchWorker.processBranch({
...config,
dryRun: true,
updateType: 'lockFileMaintenance',
reuseExistingBranch: false,
updatedArtifacts: [{ name: '|delete|', contents: 'dummy' }],
Expand Down Expand Up @@ -621,10 +623,10 @@ describe('workers/branch', () => {
pr: {},
} as never);
commit.commitFilesToBranch.mockResolvedValueOnce(null);
setAdminConfig({ dryRun: true });
expect(
await branchWorker.processBranch({
...config,
dryRun: true,
artifactErrors: [{}],
})
).toEqual(ProcessBranchResult.Done);
Expand Down
10 changes: 5 additions & 5 deletions lib/workers/branch/index.ts
Expand Up @@ -116,7 +116,7 @@ export async function processBranch(
content +=
'\n\nIf this PR was closed by mistake or you changed your mind, you can simply rename this PR and you will soon get a fresh replacement PR opened.';
if (!config.suppressNotifications.includes('prIgnoreNotification')) {
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(
`DRY-RUN: Would ensure closed PR comment in PR #${existingPr.number}`
);
Expand All @@ -129,7 +129,7 @@ export async function processBranch(
}
}
if (branchExists) {
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info('DRY-RUN: Would delete branch ' + config.branchName);
} else {
await deleteBranch(config.branchName);
Expand Down Expand Up @@ -644,7 +644,7 @@ export async function processBranch(
config.suppressNotifications.includes('lockFileErrors')
)
) {
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(
`DRY-RUN: Would ensure lock file error comment in PR #${pr.number}`
);
Expand All @@ -666,7 +666,7 @@ export async function processBranch(
// Check if state needs setting
if (existingState !== state) {
logger.debug(`Updating status check state to failed`);
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(
'DRY-RUN: Would set branch status in ' + config.branchName
);
Expand All @@ -682,7 +682,7 @@ export async function processBranch(
} else {
if (config.updatedArtifacts?.length) {
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(
`DRY-RUN: Would ensure comment removal in PR #${pr.number}`
);
Expand Down
3 changes: 2 additions & 1 deletion lib/workers/branch/reuse.ts
@@ -1,4 +1,5 @@
import { RenovateConfig } from '../../config';
import { getAdminConfig } from '../../config/admin';
import { logger } from '../../logger';
import { platform } from '../../platform';
import { branchExists, isBranchModified, isBranchStale } from '../../util/git';
Expand Down Expand Up @@ -34,7 +35,7 @@ export async function shouldReuseExistingBranch(
if (pr.labels?.includes(config.rebaseLabel)) {
logger.debug(`Manual rebase requested via PR labels for #${pr.number}`);
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(
`DRY-RUN: Would delete label ${config.rebaseLabel} from #${pr.number}`
);
Expand Down
17 changes: 9 additions & 8 deletions lib/workers/pr/index.ts
@@ -1,4 +1,5 @@
import { RenovateConfig } from '../../config';
import { getAdminConfig } from '../../config/admin';
import {
PLATFORM_INTEGRATION_UNAUTHORIZED,
PLATFORM_RATE_LIMIT_EXCEEDED,
Expand Down Expand Up @@ -52,7 +53,7 @@ export async function addAssigneesReviewers(
}
if (assignees.length > 0) {
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(`DRY-RUN: Would add assignees to PR #${pr.number}`);
} else {
await platform.addAssignees(pr.number, assignees);
Expand Down Expand Up @@ -81,7 +82,7 @@ export async function addAssigneesReviewers(
}
if (reviewers.length > 0) {
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(`DRY-RUN: Would add reviewers to PR #${pr.number}`);
} else {
await platform.addReviewers(pr.number, reviewers);
Expand Down Expand Up @@ -354,7 +355,7 @@ export async function ensurePr(
);
}
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(`DRY-RUN: Would update PR #${existingPr.number}`);
} else {
await platform.updatePr({
Expand All @@ -375,7 +376,7 @@ export async function ensurePr(
let pr: Pr;
try {
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info('DRY-RUN: Would create PR: ' + prTitle);
pr = { number: 0, displayNumber: 'Dry run PR' } as never;
} else {
Expand Down Expand Up @@ -418,7 +419,7 @@ export async function ensurePr(
{ branch: branchName },
'Deleting branch due to server error'
);
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info('DRY-RUN: Would delete branch: ' + config.branchName);
} else {
await deleteBranch(branchName);
Expand All @@ -439,7 +440,7 @@ export async function ensurePr(
content = platform.getPrBody(content);
logger.debug('Adding branch automerge failure message to PR');
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(`DRY-RUN: Would add comment to PR #${pr.number}`);
} else {
await platform.ensureComment({
Expand Down Expand Up @@ -529,7 +530,7 @@ export async function checkAutoMerge(
if (automergeType === 'pr-comment') {
logger.debug(`Applying automerge comment: ${automergeComment}`);
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(
`DRY-RUN: Would add PR automerge comment to PR #${pr.number}`
);
Expand All @@ -550,7 +551,7 @@ export async function checkAutoMerge(
// Let's merge this
logger.debug(`Automerging #${pr.number}`);
// istanbul ignore if
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(`DRY-RUN: Would merge PR #${pr.number}`);
return false;
}
Expand Down
6 changes: 5 additions & 1 deletion lib/workers/repository/dependency-dashboard.spec.ts
Expand Up @@ -7,6 +7,7 @@ import {
logger,
platform,
} from '../../../test/util';
import { setAdminConfig } from '../../config/admin';
import { PLATFORM_TYPE_GITHUB } from '../../constants/platforms';
import { Platform, Pr } from '../../platform';
import { PrState } from '../../types';
Expand Down Expand Up @@ -38,7 +39,7 @@ async function dryRun(
findPrCalls = 0
) {
jest.clearAllMocks();
config.dryRun = true;
setAdminConfig({ dryRun: true });
await dependencyDashboard.ensureMasterIssue(config, branches);
expect(platform.ensureIssueClosing).toHaveBeenCalledTimes(
ensureIssueClosingCalls
Expand All @@ -50,6 +51,9 @@ async function dryRun(

describe('workers/repository/master-issue', () => {
describe('ensureMasterIssue()', () => {
beforeEach(() => {
setAdminConfig();
});
it('do nothing if masterissue is disable', async () => {
const branches: BranchConfig[] = [];
await dependencyDashboard.ensureMasterIssue(config, branches);
Expand Down
5 changes: 3 additions & 2 deletions lib/workers/repository/dependency-dashboard.ts
@@ -1,6 +1,7 @@
import is from '@sindresorhus/is';
import { nameFromLevel } from 'bunyan';
import { RenovateConfig } from '../../config';
import { getAdminConfig } from '../../config/admin';
import { getProblems, logger } from '../../logger';
import { Pr, platform } from '../../platform';
import { PrState } from '../../types';
Expand Down Expand Up @@ -76,7 +77,7 @@ export async function ensureMasterIssue(
is.nonEmptyArray(branches) &&
branches.some((branch) => branch.res !== ProcessBranchResult.Automerged);
if (config.dependencyDashboardAutoclose && !hasBranches) {
if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(
'DRY-RUN: Would close Dependency Dashboard ' +
config.dependencyDashboardTitle
Expand Down Expand Up @@ -239,7 +240,7 @@ export async function ensureMasterIssue(
issueBody += `---\n${config.dependencyDashboardFooter}\n`;
}

if (config.dryRun) {
if (getAdminConfig().dryRun) {
logger.info(
'DRY-RUN: Would ensure Dependency Dashboard ' +
config.dependencyDashboardTitle
Expand Down
16 changes: 8 additions & 8 deletions lib/workers/repository/error-config.spec.ts
@@ -1,5 +1,6 @@
import { mock } from 'jest-mock-extended';
import { RenovateConfig, getConfig, platform } from '../../../test/util';
import { setAdminConfig } from '../../config/admin';
import { CONFIG_VALIDATION } from '../../constants/error-messages';
import { Pr } from '../../platform';
import { PrState } from '../../types';
Expand All @@ -15,6 +16,9 @@ beforeEach(() => {

describe('workers/repository/error-config', () => {
describe('raiseConfigWarningIssue()', () => {
beforeEach(() => {
setAdminConfig();
});
it('creates issues', async () => {
const error = new Error(CONFIG_VALIDATION);
error.configFile = 'package.json';
Expand All @@ -28,10 +32,8 @@ describe('workers/repository/error-config', () => {
error.configFile = 'package.json';
error.validationMessage = 'some-message';
platform.ensureIssue.mockResolvedValueOnce('created');
const res = await raiseConfigWarningIssue(
{ ...config, dryRun: true },
error
);
setAdminConfig({ dryRun: true });
const res = await raiseConfigWarningIssue(config, error);
expect(res).toBeUndefined();
});
it('handles onboarding', async () => {
Expand All @@ -55,10 +57,8 @@ describe('workers/repository/error-config', () => {
number: 1,
state: PrState.Open,
});
const res = await raiseConfigWarningIssue(
{ ...config, dryRun: true },
error
);
setAdminConfig({ dryRun: true });
const res = await raiseConfigWarningIssue(config, error);
expect(res).toBeUndefined();
});
});
Expand Down

0 comments on commit 6fa3729

Please sign in to comment.