From 5dea0a94996fdbf4551a6fef7497b49b6c0be03e Mon Sep 17 00:00:00 2001
From: Kieran <kieran.brahney@gmail.com>
Date: Sat, 11 Apr 2020 07:40:19 +0100
Subject: [PATCH] feat(composer): ignorePlatformReqs (#5937)

---
 docs/usage/self-hosted-configuration.md       |  4 ++++
 lib/config/__snapshots__/index.spec.ts.snap   |  3 +++
 lib/config/definitions.ts                     |  8 +++++++
 lib/manager/common.ts                         |  1 +
 .../__snapshots__/artifacts.spec.ts.snap      | 23 +++++++++++++++++++
 lib/manager/composer/artifacts.spec.ts        | 21 +++++++++++++++++
 lib/manager/composer/artifacts.ts             |  5 +++-
 7 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/docs/usage/self-hosted-configuration.md b/docs/usage/self-hosted-configuration.md
index bde2700322..0afac48edc 100644
--- a/docs/usage/self-hosted-configuration.md
+++ b/docs/usage/self-hosted-configuration.md
@@ -40,6 +40,10 @@ Set this to `docker` instead to use docker-based binaries.
 
 Configure this directory if you want to change which directory Renovate uses for storing cache data. If left unconfigured, it will typically be a temporary directory like `/tmp/renovate/cache/`. If you configure this to be different to the `baseDir`, it means you can have one location for repo data and another for cache data.
 
+## composerIgnorePlatformReqs
+
+Set to `false` to prevent usage of `--ignore-platform-reqs` in the composer package manager.
+
 ## dockerMapDotfiles
 
 This is used if you want to map "dotfiles" from your host computer home directory to containers that Renovate creates, e.g. for updating lock files. Currently applicable to `.npmrc` only.
diff --git a/lib/config/__snapshots__/index.spec.ts.snap b/lib/config/__snapshots__/index.spec.ts.snap
index caa3805ac0..14a9feb09b 100644
--- a/lib/config/__snapshots__/index.spec.ts.snap
+++ b/lib/config/__snapshots__/index.spec.ts.snap
@@ -29,6 +29,7 @@ Object {
   "commitMessageSuffix": null,
   "commitMessageTopic": "dependency {{depName}}",
   "compatibility": Object {},
+  "composerIgnorePlatformReqs": true,
   "configWarningReuseIssue": true,
   "dockerMapDotfiles": false,
   "dockerUser": null,
@@ -161,6 +162,7 @@ Object {
   "commitMessageSuffix": null,
   "commitMessageTopic": "dependency {{depName}}",
   "compatibility": Object {},
+  "composerIgnorePlatformReqs": true,
   "configWarningReuseIssue": true,
   "description": Array [],
   "digest": Object {
@@ -389,6 +391,7 @@ Object {
   "commitMessageSuffix": null,
   "commitMessageTopic": "dependency {{depName}}",
   "compatibility": Object {},
+  "composerIgnorePlatformReqs": true,
   "configWarningReuseIssue": true,
   "description": Array [],
   "digest": Object {
diff --git a/lib/config/definitions.ts b/lib/config/definitions.ts
index bbde257d65..6aa2dd7bfc 100644
--- a/lib/config/definitions.ts
+++ b/lib/config/definitions.ts
@@ -270,6 +270,14 @@ const options: RenovateOptions[] = [
     admin: true,
     type: 'string',
   },
+  {
+    name: 'composerIgnorePlatformReqs',
+    description:
+      'Enable / disable use of --ignore-platform-reqs in the composer package manager.',
+    type: 'boolean',
+    default: true,
+    admin: true,
+  },
   // Log options
   {
     name: 'logLevel',
diff --git a/lib/manager/common.ts b/lib/manager/common.ts
index 6869a3da2e..8bdb65973b 100644
--- a/lib/manager/common.ts
+++ b/lib/manager/common.ts
@@ -39,6 +39,7 @@ export interface UpdateArtifactsConfig extends ManagerConfig {
   isLockFileMaintenance?: boolean;
   compatibility?: Record<string, string>;
   cacheDir?: string;
+  composerIgnorePlatformReqs?: boolean;
   postUpdateOptions?: string[];
   ignoreScripts?: boolean;
 
diff --git a/lib/manager/composer/__snapshots__/artifacts.spec.ts.snap b/lib/manager/composer/__snapshots__/artifacts.spec.ts.snap
index a747946623..36ff55166f 100644
--- a/lib/manager/composer/__snapshots__/artifacts.spec.ts.snap
+++ b/lib/manager/composer/__snapshots__/artifacts.spec.ts.snap
@@ -22,6 +22,29 @@ Array [
 ]
 `;
 
+exports[`.updateArtifacts() disables ignorePlatformReqs 1`] = `
+Array [
+  Object {
+    "cmd": "composer update --with-dependencies --no-ansi --no-interaction --no-scripts --no-autoloader",
+    "options": Object {
+      "cwd": "/tmp/github/some/repo",
+      "encoding": "utf-8",
+      "env": Object {
+        "COMPOSER_CACHE_DIR": "/tmp/renovate/cache/others/composer",
+        "HOME": "/home/user",
+        "HTTPS_PROXY": "https://example.com",
+        "HTTP_PROXY": "http://example.com",
+        "LANG": "en_US.UTF-8",
+        "LC_ALL": "en_US",
+        "NO_PROXY": "localhost",
+        "PATH": "/tmp/path",
+      },
+      "timeout": 900000,
+    },
+  },
+]
+`;
+
 exports[`.updateArtifacts() performs lockFileMaintenance 1`] = `
 Array [
   Object {
diff --git a/lib/manager/composer/artifacts.spec.ts b/lib/manager/composer/artifacts.spec.ts
index 531da69608..78c53d7f78 100644
--- a/lib/manager/composer/artifacts.spec.ts
+++ b/lib/manager/composer/artifacts.spec.ts
@@ -33,6 +33,7 @@ const config = {
   localDir: join('/tmp/github/some/repo'),
   cacheDir: join('/tmp/renovate/cache'),
   dockerUser: 'foobar',
+  composerIgnorePlatformReqs: true,
 };
 
 describe('.updateArtifacts()', () => {
@@ -208,4 +209,24 @@ describe('.updateArtifacts()', () => {
       })
     ).rejects.toThrow();
   });
+  it('disables ignorePlatformReqs', async () => {
+    platform.getFile.mockResolvedValueOnce('Current composer.lock');
+    const execSnapshots = mockExecAll(exec);
+    fs.readFile.mockReturnValueOnce('New composer.lock' as any);
+    platform.getRepoStatus.mockResolvedValue({
+      modified: ['composer.lock'],
+    } as StatusResult);
+    expect(
+      await composer.updateArtifacts({
+        packageFileName: 'composer.json',
+        updatedDeps: [],
+        newPackageFileContent: '{}',
+        config: {
+          ...config,
+          composerIgnorePlatformReqs: false,
+        },
+      })
+    ).not.toBeNull();
+    expect(execSnapshots).toMatchSnapshot();
+  });
 });
diff --git a/lib/manager/composer/artifacts.ts b/lib/manager/composer/artifacts.ts
index bd67233582..9af135a478 100644
--- a/lib/manager/composer/artifacts.ts
+++ b/lib/manager/composer/artifacts.ts
@@ -117,7 +117,10 @@ export async function updateArtifacts({
       args =
         ('update ' + updatedDeps.join(' ')).trim() + ' --with-dependencies';
     }
-    args += ' --ignore-platform-reqs --no-ansi --no-interaction';
+    if (config.composerIgnorePlatformReqs) {
+      args += ' --ignore-platform-reqs';
+    }
+    args += ' --no-ansi --no-interaction';
     if (global.trustLevel !== 'high' || config.ignoreScripts) {
       args += ' --no-scripts --no-autoloader';
     }
-- 
GitLab