Skip to content

Commit

Permalink
refactor(dashboard): move logic into common file (#10454)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
rarkins and viceice committed Jun 16, 2021
1 parent 2776db6 commit 7109030
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ This repository currently has no open or pending branches.
And this is a footer
"
`;

exports[`workers/repository/dependency-dashboard readDashboardBody() reads dashboard body 1`] = `
Object {
"dependencyDashboardChecks": Object {
"branchName1": "approve",
},
"dependencyDashboardIssue": 1,
"dependencyDashboardRebaseAllOpen": true,
"dependencyDashboardTitle": "Dependency Dashboard",
"prCreation": "approval",
}
`;
16 changes: 16 additions & 0 deletions lib/workers/repository/dependency-dashboard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@ async function dryRun(
}

describe(getName(), () => {
describe('readDashboardBody()', () => {
it('reads dashboard body', async () => {
const conf: RenovateConfig = {};
conf.prCreation = 'approval';
platform.findIssue.mockResolvedValueOnce({
title: '',
number: 1,
body:
loadFixture('master-issue_with_8_PR.txt').replace('- [ ]', '- [x]') +
'\n\n - [x] <!-- rebase-all-open-prs -->',
});
await dependencyDashboard.readDashboardBody(conf);
expect(conf).toMatchSnapshot();
});
});

describe('ensureDependencyDashboard()', () => {
beforeEach(() => {
setAdminConfig();
Expand Down
47 changes: 47 additions & 0 deletions lib/workers/repository/dependency-dashboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,53 @@ import { getProblems, logger } from '../../logger';
import { platform } from '../../platform';
import { BranchConfig, BranchResult } from '../types';

interface DependencyDashboard {
dependencyDashboardChecks: Record<string, string>;
dependencyDashboardRebaseAllOpen: boolean;
}

function parseDashboardIssue(issueBody: string): DependencyDashboard {
const checkMatch = ' - \\[x\\] <!-- ([a-zA-Z]+)-branch=([^\\s]+) -->';
const checked = issueBody.match(new RegExp(checkMatch, 'g'));
const dependencyDashboardChecks: Record<string, string> = {};
if (checked?.length) {
const re = new RegExp(checkMatch);
checked.forEach((check) => {
const [, type, branchName] = re.exec(check);
dependencyDashboardChecks[branchName] = type;
});
}
const checkedRebaseAll = issueBody.includes(
' - [x] <!-- rebase-all-open-prs -->'
);
let dependencyDashboardRebaseAllOpen = false;
if (checkedRebaseAll) {
dependencyDashboardRebaseAllOpen = true;
/* eslint-enable no-param-reassign */
}
return { dependencyDashboardChecks, dependencyDashboardRebaseAllOpen };
}

export async function readDashboardBody(config: RenovateConfig): Promise<void> {
/* eslint-disable no-param-reassign */
config.dependencyDashboardChecks = {};
const stringifiedConfig = JSON.stringify(config);
if (
config.dependencyDashboard ||
stringifiedConfig.includes('"dependencyDashboardApproval":true') ||
stringifiedConfig.includes('"prCreation":"approval"')
) {
config.dependencyDashboardTitle =
config.dependencyDashboardTitle || `Dependency Dashboard`;
const issue = await platform.findIssue(config.dependencyDashboardTitle);
if (issue) {
config.dependencyDashboardIssue = issue.number;
Object.assign(config, parseDashboardIssue(issue.body));
}
}
/* eslint-enable no-param-reassign */
}

function getListItem(branch: BranchConfig, type: string): string {
let item = ' - [ ] ';
item += `<!-- ${type}-branch=${branch.branchName} -->`;
Expand Down
35 changes: 2 additions & 33 deletions lib/workers/repository/process/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { mergeChildConfig } from '../../../config';
import type { RenovateConfig } from '../../../config/types';
import { logger } from '../../../logger';
import type { PackageFile } from '../../../manager/types';
import { platform } from '../../../platform';
import { branchExists } from '../../../util/git';
import { addSplit } from '../../../util/split';
import type { BranchConfig } from '../../types';
import { readDashboardBody } from '../dependency-dashboard';
import { ExtractResult, extract, lookup, update } from './extract-update';
import type { WriteUpdateResult } from './write';

Expand All @@ -25,38 +25,7 @@ function getBaseBranchConfig(
export async function extractDependencies(
config: RenovateConfig
): Promise<ExtractResult> {
logger.debug('processRepo()');
/* eslint-disable no-param-reassign */
config.dependencyDashboardChecks = {};
const stringifiedConfig = JSON.stringify(config);
// istanbul ignore next
if (
config.dependencyDashboard ||
stringifiedConfig.includes('"dependencyDashboardApproval":true') ||
stringifiedConfig.includes('"prCreation":"approval"')
) {
config.dependencyDashboardTitle =
config.dependencyDashboardTitle || `Dependency Dashboard`;
const issue = await platform.findIssue(config.dependencyDashboardTitle);
if (issue) {
const checkMatch = ' - \\[x\\] <!-- ([a-zA-Z]+)-branch=([^\\s]+) -->';
const checked = issue.body.match(new RegExp(checkMatch, 'g'));
if (checked?.length) {
const re = new RegExp(checkMatch);
checked.forEach((check) => {
const [, type, branchName] = re.exec(check);
config.dependencyDashboardChecks[branchName] = type;
});
}
const checkedRebaseAll = issue.body.includes(
' - [x] <!-- rebase-all-open-prs -->'
);
if (checkedRebaseAll) {
config.dependencyDashboardRebaseAllOpen = true;
/* eslint-enable no-param-reassign */
}
}
}
await readDashboardBody(config);
let res: ExtractResult = {
branches: [],
branchList: [],
Expand Down

0 comments on commit 7109030

Please sign in to comment.