Skip to content

Commit

Permalink
fix: Only run postUpgradeTasks if package files are updated and… (#5988)
Browse files Browse the repository at this point in the history
  • Loading branch information
daekene committed Apr 22, 2020
1 parent 18b05fb commit 313846c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
15 changes: 13 additions & 2 deletions lib/workers/branch/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
PR_STATE_OPEN,
} from '../../constants/pull-requests';
import { StatusResult } from '../../platform/git/storage';
import { File } from '../../platform';

jest.mock('./get-updated');
jest.mock('./schedule');
Expand Down Expand Up @@ -613,13 +614,22 @@ describe('workers/branch', () => {
});

it('executes post-upgrade tasks if trust is high', async () => {
const updatedPackageFile: File = {
name: 'pom.xml',
contents: 'pom.xml file contents',
};
getUpdated.getUpdatedPackageFiles.mockResolvedValueOnce({
updatedPackageFiles: [{}],
updatedPackageFiles: [updatedPackageFile],
artifactErrors: [],
} as never);
npmPostExtract.getAdditionalFiles.mockResolvedValueOnce({
artifactErrors: [],
updatedArtifacts: [{}],
updatedArtifacts: [
{
name: 'yarn.lock',
contents: Buffer.from([1, 2, 3]) /* Binary content */,
},
],
} as never);
platform.branchExists.mockResolvedValueOnce(true);
platform.getBranchPr.mockResolvedValueOnce({
Expand All @@ -635,6 +645,7 @@ describe('workers/branch', () => {
} as StatusResult);
global.trustLevel = 'high';

fs.outputFile.mockReturnValue();
fs.readFile.mockResolvedValueOnce(Buffer.from('modified file content'));

schedule.isScheduledNow.mockReturnValueOnce(false);
Expand Down
24 changes: 23 additions & 1 deletion lib/workers/branch/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { DateTime } from 'luxon';
import is from '@sindresorhus/is';
import minimatch from 'minimatch';
import { join } from 'path';
import { concat } from 'lodash';
import { logger } from '../../logger';
import { isScheduledNow } from './schedule';
import { getUpdatedPackageFiles } from './get-updated';
Expand Down Expand Up @@ -38,7 +40,7 @@ import {
import { BranchStatus } from '../../types';
import { exec } from '../../util/exec';
import { regEx } from '../../util/regex';
import { readLocalFile } from '../../util/fs';
import { readLocalFile, writeLocalFile } from '../../util/fs';

// TODO: proper typings
function rebaseCheck(config: RenovateConfig, branchPr: any): boolean {
Expand Down Expand Up @@ -319,6 +321,10 @@ export async function processBranch(
}

if (
/* Only run post-upgrade tasks if there are changes to package files... */
(config.updatedPackageFiles?.length > 0 ||
/* ... or changes to artifacts */
config.updatedArtifacts?.length > 0) &&
global.trustLevel === 'high' &&
is.nonEmptyArray(config.allowedPostUpgradeCommands)
) {
Expand All @@ -333,6 +339,22 @@ export async function processBranch(
const fileFilters = config.postUpgradeTasks.fileFilters || [];

if (is.nonEmptyArray(commands)) {
// Persist updated files in file system so any executed commands can see them
for (const file of concat(
config.updatedPackageFiles,
config.updatedArtifacts
)) {
if (file.name !== '|delete|') {
let contents;
if (typeof file.contents === 'string') {
contents = Buffer.from(file.contents);
} else {
contents = file.contents;
}
await writeLocalFile(join(config.localDir, file.name), contents);
}
}

for (const cmd of commands) {
if (
!config.allowedPostUpgradeCommands.some((pattern) =>
Expand Down

0 comments on commit 313846c

Please sign in to comment.